在dymanic control -combobox中获取所选项目 [英] To get selected item in dymanic control -combobox

查看:61
本文介绍了在dymanic control -combobox中获取所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态创建了comboBox。但我需要从SelectedIndexChanged中选择值。但是因为对象引用没有设置为对象的实例而得到错误。



I have created comboBox dynamically. But i need to select the value from " SelectedIndexChanged". But iam getting error as "Object reference not set to an instance of an object."

 private ComboBox TimeIntervel;
int Stimer; 
public void set control()
{
   ComboBox TimeIntervelCmb = new ComboBox();

 TimeIntervelCmb.Location = new Point(50,70);
            TimeIntervelCmb.Name = "ComboBoxTime";
            TimeIntervelCmb.Size = new Size(80, 100);
            TimeIntervelCmb.Items.Add("500");
            TimeIntervelCmb.Items.Add("1000");
            TimeIntervelCmb.Items.Add("2000");
            TimeIntervelCmb.Items.Add("3000");
            TimeIntervelCmb.Items.Add("4000");
            TimeIntervelCmb.Items.Add("5000");
            TimeIntervelCmb.Items.Add("6000");

TimeIntervelCmb.SelectedIndexChanged += new EventHandler(TimeIntervel_SelectedIndexChanged);
            this.Controls.Add(TimeIntervelCmb);
}
  private void TimeIntervel_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
               
                
                if (TimeIntervel.SelectedItem  != null)// Object reference not set to an instance of an object.-Error
                {
                    Stimer = int.Parse(TimeIntervel.SelectedItem.ToString());
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }





我的尝试:



请为我提供解决方案。



private void TimeIntervel_SelectedIndexChanged(object sender,EventArgs e)

{

试试

{





if(TimeIntervel.SelectedItem) != null)

{

Stimer = int.Parse(TimeIntervel.SelectedItem.ToString());

}

}

catch(例外情况)

{



MessageBox.Show(ex.Message) ;

}

}



What I have tried:

Kindly provide me solution for this.

private void TimeIntervel_SelectedIndexChanged(object sender, EventArgs e)
{
try
{


if (TimeIntervel.SelectedItem != null)
{
Stimer = int.Parse(TimeIntervel.SelectedItem.ToString());
}
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
}

推荐答案

您尝试处理
private ComboBox TimeIntervel;



但你创建了一个另一个,并且未填充 TimeIntervel 。将 TimeIntervel 重命名为 TimeIntervelCMB






but instead you have created another one, and the TimeIntervel is not populated. Rename the TimeIntervel to TimeIntervelCMB


private ComboBox TimeIntervelCMB;
int Stimer; 
public void set control()
{
   ComboBox TimeIntervelCmb = new ComboBox();
 
            TimeIntervelCmb.Location = new Point(50,70);
            TimeIntervelCmb.Name = "ComboBoxTime";
            TimeIntervelCmb.Size = new Size(80, 100);
            TimeIntervelCmb.Items.AddRange({500,1000,2000,3000,4000,5000,6000});


这篇关于在dymanic control -combobox中获取所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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