TextBox 显示数字而不是 CheckedListBox 中的单词
TextBox shows numbers instead of words from CheckedListBox
首先,抱歉,如果这个 post 看起来很混乱,因为我的英语很糟糕。
如何让 CheckedListBox
上随机选择的项目显示在 TextBox
上?这是我的代码:
private void generateButton_Click(object sender, EventArgs e) {
textBox1.Clear();
Random random = new Random();
int randomtrait = random.Next(1, checkedListBox1.CheckedItems.Count);
checkedListBox1.SelectedItem = checkedListBox1.Items[randomtrait];
string data = randomtrait.ToString();
textBox1.Text = data; //but it shows a number rather than text
}
我仍然是一个初学者和自学成才的程序员。谢谢
我明白了,您的代码将随机特征分配给了您的 data
,这是您的随机数:
string data = randomtrait.ToString();
为了给 checkedListBox1 赋值,你的代码必须是这样的:
string data = checkedListBox1.SelectedItems[randomtrait].ToString();
正如我当前的评论所强调的那样,由于您显示的是随机特征,因此它是一个整数,因此您得到了一个数字。
我假设您打算执行的操作如下。您检查了包含多个项目的列表框。由于他们能够检查多个项目,因此在单击此 generateButton 时,您希望显示其中一个已检查的项目。如果那是你的意图,那么逻辑上可能存在一些缺陷:
private void generateButton_Click(object sender, EventArgs e) {
textBox1.Clear();
Random random = new Random();
// https://msdn.microsoft.com/en-us/library/2dx6wyd4.aspx
// random.Next is inclusive of lower bound and exclusive on upper bound
// the way you are accessing array, it is 0 based - thus you may not be able to picked up your first checked item
int randomtrait = random.Next(1, checkedListBox1.CheckedItems.Count);
// this set the 'selectedItem' to be something else from the whole list (rather than the checked items only).
// checkedListBox1.SelectedItem = checkedListBox1.Items[randomtrait];
// randomtrait is an integer, so data here would be numbers. This explains why next line displaying a number rather than text
//string data = randomtrait.ToString();
textBox1.Text = data;
}
可能是您想要的:
private void generateButton_Click(object sender, EventArgs e) {
textBox1.Clear();
Random random = new Random();
int randomtrait = random.Next(0, checkedListBox1.CheckedItems.Count);
textBox1.Text = checkedListBox1.CheckedItems[randomtrait].ToString();
}
首先,抱歉,如果这个 post 看起来很混乱,因为我的英语很糟糕。
如何让 CheckedListBox
上随机选择的项目显示在 TextBox
上?这是我的代码:
private void generateButton_Click(object sender, EventArgs e) {
textBox1.Clear();
Random random = new Random();
int randomtrait = random.Next(1, checkedListBox1.CheckedItems.Count);
checkedListBox1.SelectedItem = checkedListBox1.Items[randomtrait];
string data = randomtrait.ToString();
textBox1.Text = data; //but it shows a number rather than text
}
我仍然是一个初学者和自学成才的程序员。谢谢
我明白了,您的代码将随机特征分配给了您的 data
,这是您的随机数:
string data = randomtrait.ToString();
为了给 checkedListBox1 赋值,你的代码必须是这样的:
string data = checkedListBox1.SelectedItems[randomtrait].ToString();
正如我当前的评论所强调的那样,由于您显示的是随机特征,因此它是一个整数,因此您得到了一个数字。
我假设您打算执行的操作如下。您检查了包含多个项目的列表框。由于他们能够检查多个项目,因此在单击此 generateButton 时,您希望显示其中一个已检查的项目。如果那是你的意图,那么逻辑上可能存在一些缺陷:
private void generateButton_Click(object sender, EventArgs e) {
textBox1.Clear();
Random random = new Random();
// https://msdn.microsoft.com/en-us/library/2dx6wyd4.aspx
// random.Next is inclusive of lower bound and exclusive on upper bound
// the way you are accessing array, it is 0 based - thus you may not be able to picked up your first checked item
int randomtrait = random.Next(1, checkedListBox1.CheckedItems.Count);
// this set the 'selectedItem' to be something else from the whole list (rather than the checked items only).
// checkedListBox1.SelectedItem = checkedListBox1.Items[randomtrait];
// randomtrait is an integer, so data here would be numbers. This explains why next line displaying a number rather than text
//string data = randomtrait.ToString();
textBox1.Text = data;
}
可能是您想要的:
private void generateButton_Click(object sender, EventArgs e) {
textBox1.Clear();
Random random = new Random();
int randomtrait = random.Next(0, checkedListBox1.CheckedItems.Count);
textBox1.Text = checkedListBox1.CheckedItems[randomtrait].ToString();
}