列表框项目不显示 [英] Listbox Items not displaying
本文介绍了列表框项目不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的骰子滚动"程序未在列表框中显示结果,因此,非常感谢纠正此问题的任何建议,这是代码:
My Dice Rolling program is not displaying results in the listbox, any suggestions to remedy this issue much appreciated, here is the code:
{
public partial class FormDice : Form
{
int sides;
int answer;
public FormDice()
{
InitializeComponent();
}
private void exitButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void clearButton_Click(object sender, EventArgs e)
{
twoRadioButton.Checked = false;
tenRadioButton.Checked = false;
fiftyRadioButton.Checked = false;
resultsListBox.Items.Clear();
}
private void rollButton_Click(object sender, EventArgs e)
{
if(twoRadioButton.Checked == true)
{
sides = 2;
RollDice roll = new RollDice(sides);
answer = roll.Roll();
}
else if(tenRadioButton.Checked == true)
{
sides = 10;
RollDice roll = new RollDice(sides);
answer = roll.Roll();
}
else if(fiftyRadioButton.Checked == true)
{
sides = 50;
RollDice roll = new RollDice(sides);
answer = roll.Roll();
}
else
{
RollDice roll = new RollDice(sides);
answer = roll.Roll();
}
}
private void resultsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
resultsListBox.Items.Add(answer.ToString());
}
}
}
{
class RollDice
{
//variables
private int numberOfSides;
private Random randomNumberGenerator = new Random();
//constructor with no parameters
public RollDice()
{
numberOfSides = 6;
}
//constructor with parameter
public RollDice(int sides)
{
numberOfSides = sides;
}
//method to roll the dice by returning an integer value
public int Roll()
{
int randomNumber = randomNumberGenerator.Next(1, numberOfSides + 1);
return randomNumber;
}
}
}
推荐答案
原因是该项目正在问题
中给出的resultsListBox
事件的SelectedIndexChanged
事件中添加.
The reason is that the item is being added inSelectedIndexChanged
event ofresultsListBox
as given in the question
private void resultsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
resultsListBox.Items.Add(answer.ToString());
}
如果在单击rollButton
时需要添加结果,则添加项目
If the result is required to be added when the rollButton
is clicked, then add the item
resultsListBox.Items.Add(answer.ToString());
在rollButton
的Click
事件中,是rollButton_Click
事件.
in the Click
event of rollButton
, rollButton_Click
event.
这篇关于列表框项目不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文