【发布时间】:2018-11-23 10:49:12
【问题描述】:
原谅,C# 的新手一直是 PHP 人。 有一个表单来根据从下拉列表中选择的字符数为密码生成随机字符串。 从下拉列表中获取值时遇到问题(将对象转换为 int)。 代码:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string endword = "";
int chrnumber = Convert.ToInt16(comboBox1.SelectedValue);
string[] Nochars = { "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "#", "z", "x", "c", "v", "b", "n", "m", "/", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "{", "}", "A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "@", "~", "Z", "X", "C", "V", "B", "N", "M", "<", ">", "?", "!", "£", "$", "%", "^", "&", ".*", "(", ")", "_", "+", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=" };
Random rndchar = new Random();
for (int i = 0; i < chrnumber; i++)
{
int iSelect = rndchar.Next(0, Nochars.Length);
string word1 = Nochars[iSelect];
string word2 = word1;
if (i == 0) { endword = word1; } else { endword += "." + word2; }
}
pwd.Text = endword;
}
【问题讨论】:
-
你已经获得了值。
SelectedValue包含你放在那里的任何东西。我很确定它不是一个 16 位整数。整数是 32 位的。如果SelectedValue包含整数,一个简单的(int)(BomboBox1.SelectedValue)就足够了 -
使用组合框数据源更新您的问题,以便我们找到错误原因。
-
Having a problem有什么问题?你有例外吗?出乎意料的价值?
标签: c# dropdownbox selectedvalue