如何在C#Windows窗体中执行奖励按钮 [英] How to do a bonus button in C# Windows forms

查看:67
本文介绍了如何在C#Windows窗体中执行奖励按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



我正在制作一个类似于谁想成为百万富翁的小程序。当玩家按下50:50图像时,有一个选项,随机标签中的两个将被隐藏,只剩下一个正确答案和错误答案。我已经开始为它编写逻辑。但它似乎没有用。



  var  label =  new 列表< string> {  label1  label2  label3}; 

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

// 选择索引label be visible = false
index1.Visible = false ;
index2.visible = false ;





谢谢你帮助。



这里的逻辑是:

1)将标签放入数组中。

2 )随机选择2索引形式数组。因此,将选择两个错误答案。

3)选中后,标签将变为不可见。然后只剩下一个错误的答案。

解决方案

 index1.Visible =  false ; 



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



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


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



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

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

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

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

  private  Random rand =  new  Random(); 

并改为使用它。



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

; expected



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



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

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



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



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

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

 < span class =code-keyword> 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.


这篇关于如何在C#Windows窗体中执行奖励按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆