[英]Trouble setting combobox selectedindex from another form
I am trying to set the selectedindex on another form (form2) based on the user selected index on the first form (form1). 我试图根据用户在第一个窗体(form1)上选择的索引来在另一个窗体(form2)上设置selectedindex。
I am getting the value with this code but it returns a negative number. 我正在用此代码获取值,但它返回一个负数。
public int SelectedComboIndex
{
get { return comboBox1.SelectedIndex; }
}
And I am trying to set the comboBox Index by 我正在尝试通过以下方式设置comboBox Index
comboBox1.SelectedIndex = form1.SelectedComboIndex;
Can anyone point me in the right direction as to how to do this? 谁能为我指出正确的方向?
Edit: More code for where its calling the code on form1 编辑:更多代码,用于调用form1上的代码
Form1 form1 = null;
public Form2(Form1 parentForm1) : this()
{
form1 = parentForm1;
}
A combobox returns a negative value (-1 normally) if no index is selected. 如果未选择索引,则组合框将返回负值(通常为-1)。
So I believe (I have not checked) that if you set a negative value for the SelectedIndex property, all you are going to do is clear the selection in your combobox. 因此,我相信(我尚未检查),如果您为SelectedIndex属性设置了负值,您要做的就是清除组合框中的选择。
Hope that helps. 希望能有所帮助。
Best practice is to usually leave any sort of UI alterations in the Load
method of the form, that way the form has a chance to initialize properly and all bindings are setup before you actually make changes. 最佳实践通常是在窗体的
Load
方法中保留任何类型的UI更改,这样,窗体就有机会正确初始化并且在实际进行更改之前已设置所有绑定。 The Constructors should only be used to set internal state. 构造函数仅应用于设置内部状态。
private Form1 _parentForm;
public Form2(Form1 parentForm) : this()
{
_parentForm = parentForm;
}
public Form2()
{
InitializeComponents();
}
private void Form2_Load(object sender, EventArgs e)
{
richTextBox1.Font = new Font("Times New Roman", 12f, FontStyle.Regular);
dropdown();
if(_parentForm != null)
comboBox1.SelectedIndex = _parentForm.SelectedComboIndex;
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
}
Try that out and see if it works. 试试看,看看是否可行。 Just be sure to add the Load handler to the form properly (either through the designer or in code via
this.Load += new EventHandler(Form2_Load)
只需确保将Load处理程序正确添加到表单即可(通过设计器或通过
this.Load += new EventHandler(Form2_Load)
在代码中this.Load += new EventHandler(Form2_Load)
Note: You should rename all your components to something more useful than controlType1, controlType2 etc. This is good for both you and us.
As other people said if form1.SelectedComboIndex
returns -1 (because form1.comboBox1
has nothing selected) then the line comboBox1.SelectedIndex = form1.SelectedComboIndex
will (correctly) set comboBox1
's value to nothing. 正如其他人所说,如果
form1.SelectedComboIndex
返回-1(因为form1.comboBox1
没有选择任何内容),那么comboBox1.SelectedIndex = form1.SelectedComboIndex
行将(正确)将comboBox1
的值设置comboBox1.SelectedIndex = form1.SelectedComboIndex
。
Additionally, just because there's text present in your ComboBox
doesn't mean it's got a selected value. 另外,仅因为
ComboBox
中存在文本并不意味着它具有选定的值。 Make sure you combo box has actually selected a value (not just changed its text). 确保您的组合框确实选择了一个值(而不只是更改了其文本)。 You can force picking a value by setting the
DropDownStyle
to DropDownList
. 您可以强制通过设置选择一个值
DropDownStyle
至DropDownList
。 Both other styles allow user entry of custom values. 其他两种样式均允许用户输入自定义值。
If you want users to be able to type, then consider setting AutoCompleteMode
to SuggestAppend
and AutoCompleteSource
to ListItems
. 如果希望用户能够键入,则可以考虑将
AutoCompleteMode
设置为SuggestAppend
,将AutoCompleteSource
为ListItems
。 This makes it easier to correctly select values from the combobox rather than just change the text (accidentally leaving it null). 这使得更容易从组合框中正确选择值,而不仅仅是更改文本(偶然将其保留为空)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.