【问题标题】:selecting value not index from drop down c#从下拉C#中选择值而不是索引
【发布时间】: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


【解决方案1】:
//On form load
public Form1(){
         List<Values> reasons = new List<Values> { 
            new Values("0", 0), 
            new Values("1", 1),
            new Values("2", 2), 
            new Values("3", 3), 
            new Values("4", 4), 
            new Values("5", 5) };
comboBox1.DataSource = reasons;
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
reason = Convert.ToInt32(comboBox1.SelectedValue);
}

这里的默认值为 0,因此您的错误在任何时候都不会发生。我希望您的组合框数据源默认值有",这是导致此错误的原因。

或确保值部分不是字母/其他字符,而不是数据源中的数字。

【讨论】:

    【解决方案2】:

    试试这个

      private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string endword = "";          
            int chrnumber = int.Parse(this.comboBox1.GetItemText(this.comboBox1.SelectedItem).ToString());// change line
            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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      相关资源
      最近更新 更多