且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#Windows窗体中执行奖励按钮

更新时间:2023-02-11 08:34:52

 index1.Visible =  false ; 



鉴于您已将 index1 声明为 int 您希望这行代码做什么?



您应该使用随机数生成值,然后您可以使用它来访问控件您的表单,您可以根据需要显示或隐藏。


这可能是因为您没有想过您正在尝试做什么,但只是猜到并希望它会起作用:计算机(或我所知道的任何其他领域)都不是一个成功的策略。



让我们循环解决最明显的问题 - 导致编译错误的问题:

1)没有静态方法Random.Next - 你需要一个Random类的实例才能使用Next方法。因此,您会收到错误:

非静态字段,方法或属性需要对象引用...

通过创建类级实例来修复它:

  private  Random rand =  new  Random(); 

并改为使用它。



2)c#中的语句全部终止用分号表示:这意味着你得到一个

; expected



通过添加分号修复它每个指令的结尾。



3)C#区分大小写,因此可见和可见不同 - 您需要使用正确的名称,这是可见的

通过将可见更改为可见来修复它



4)整数没有可见(或可见)属性,因为它们不是可显示的用户元素(或已知的控件)。 />
修复:复杂......



最大的问题是数字不是标签,即使它与标签的名称有某种关系 - 而且有很多方法可以获得标签的实际标签通过反思命名,这是高级的东西,远远超出你的技能水平。

相反,尝试使用简单的if语句:

  int  hideMe = rand.Next(numberOfLabelsLeft); 
if (hideMe == 0 )label1.Visible = 假跨度>;
else if (hideMe == 1 )label2.Visible = false ;
其他 如果(...

您明白了。 ..



你可以使用数组或列表来完成它,但是集合必须包含实际的对象(Label类实例)而不是字符串。


Hi!

I am making a small program kinda like "Who Wants To Be A Millionaire". There is an option when the player press the 50:50 image, two of the random label will be hidden and only one correct answer and wrong answer will be left. I have started programming the logic for it. But it does no seem to be working.

var label = new List<string> { "label1", "label2", "label3" };

int index1 = Random.Next(label.Count)
int index2 = Random.Next(label.Count)

//Make selected index of label be visible = false
index1.Visible = false;
index2.visible = false;



Thank you for the help.

FYr here is the logic:
1) put labels in an array.
2) random select 2 index form array. So that two wrong answers will be selected.
3) when selected, label will become invisible. Then only one wrong answer will be left.

index1.Visible = false;


Given that you have declared index1 as an int what do you expect this line of code to do?

You should be using the randomiser to generate values that you can then use to access the controls on your form, which you can show or hide as required.


That's probably because you haven't thought about what you are trying to do, but have just guessed and hoped it would work: that isn't a successful strategy in computer (or any other field I know of, either)

Let's loop at the most obvious problems - the ones that cause compilation errors:
1) There is no static method Random.Next - you need an instance of the Random class to use the Next method. As a result you get the error:
"An object reference is required for the non-static field, method, or property ..."

Fix it by creating a class level instance:

private Random rand = new Random();

and using that instead.

2) Statements in c# are all terminated by a semicolon: that means you get a

"; expected"


Fix it by adding a semicolon to the end of each instruction.

3) C# is case sensitive, so "Visible" and "visible" are different - you need to use exactly the right name, which is "Visible"
Fix it by changing "visible" to "Visible"

4) Integers do not have a Visible (or visible) property, because they are not displayable user elements (or "controls" as they are known).
Fix: complicated...

The big problem is that a number is not a label, even if it is in some way related to the name of the label - and while there are ways to get the actual label of that name via reflection, that's advanced stuff and far, far beyond your skill level.
Instead, try using a simple if statement:

int hideMe = rand.Next(numberOfLabelsLeft);
if (hideMe == 0) label1.Visible = false;
else if (hideMe == 1) label2.Visible = false;
else if (...

You get the idea...

You can do it using an array or a list, but the collection would have to contain the actual objects (Label class instances) rather than strings.