将组合框选择传递给另一个win窗体中的其他组合框c# [英] Pass combobox selection to other combo box in another win form c#

查看:98
本文介绍了将组合框选择传递给另一个win窗体中的其他组合框c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个窗体,他们都有相同的组合框和相同的可能选择。我想通过选择从一个组合框到另一个按钮点击。
我正在将值传递给文本框,但是当涉及到combobox时,我无法确定。



这里是代码示例:



表格2定义

  

私有字符串passCB;

public double passTxtValue
{
get {return passTxt; }
set {passTxt = value; }
}
public string passCbValue
{
get {return passCB; }
set {passCB = value; }
}

表格1发送

  private void btnPassValues_Click(object sender,EventArgs e)
{
Form2 form2 = new Form2();

form2.passTxtValue = variable1Form1;

form2.passCbValue = CBForm1.SelectedText;
form2.Show();
}

Form2 - 加载(打开)

  private void form2_Load(object sender,EventArgs e)
{
variable1Form2.Text = passTxt.ToString();
CBForm2.SelectedText = passCB;
}

当我用调试器检查时,错误在步骤2。当我尝试使用索引或值时,字符串()为 form2.passCbValue = CBValueForm1.SelectedText;



它需要强制转换,但是有一个错误,它不能被强制转换。有人可以告诉我我做错了什么?

解决方案

SelectedText 表示具有DropDown样式的ComboBox的TextBox区域中当前选定的文本(蓝色突出显示的文本)。每当组合框失去其焦点时,此选择将​​被清除(当您处于按钮点击事件时,焦点在按钮上)。



解决方法是传递form1上的组合的SelectedIndex(如果两个组合以相同的顺序填充相同的项目)

  private void btnPassValues_Click(object sender,EventArgs e)
{
Form2 form2 = new Form2
form2.passTxtValue = variable1Form1;
form2.passCbIndex = CBValueForm1.SelectedIndex;
form2.Show();
}

在form2中更改属性以获取整数而不是字符串... 。

  private int passCBIndex; 
public int passCbIndex
{
get {return passCBIndex; }
set {passCBIndex = value; }
}

并在您的form2 Load事件

  private void form2_Load(object sender,EventArgs e)
{
....
CBValueForm2.SelectedIndex = passCBIndex;
}


I have 2 windows forms, they both have same comboboxes with same possible selections. I want to pass selection from one combobox to another on button click. I am passing values successfully to textboxes but when it comes to combobox, I cant figure it out.

Here is the code example:

Form 2 definition

private double passTxt;

private string passCB;

public double passTxtValue
        {
            get { return passTxt; }
            set { passTxt = value; }
        }
public string passCbValue
        {
            get { return passCB; }
            set { passCB = value; }
        }

Form 1 send

 private void btnPassValues_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(); 

            form2.passTxtValue = variable1Form1;

            form2.passCbValue = CBForm1.SelectedText;
            form2.Show();
        }

Form2 - load (open)

private void form2_Load(object sender, EventArgs e)
        {
            variable1Form2.Text = passTxt.ToString(); 
            CBForm2.SelectedText = passCB;
        }

When I check with debugger, the error is in step 2. Form1 send empty string ("") for form2.passCbValue = CBValueForm1.SelectedText;

When I try to use index or value it require cast but then there is an error that it can't be casted. Can someone tell me what i do wrong?

解决方案

SelectedText represent the current selected text in the TextBox area of a ComboBox with DropDown style (the blue highlighted text). This selection is cleared every time the combobox looses its focus (and when you are in the button click event, the focus is on the button).

The workaround is to pass the SelectedIndex of the combo on form1 (if the two combos are filled with the same items in the same order)

private void btnPassValues_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2(); 
    form2.passTxtValue = variable1Form1;
    form2.passCbIndex = CBValueForm1.SelectedIndex;
    form2.Show();
}

In form2 change the property to get an integer instead of a string....

private int passCBIndex;
public int passCbIndex
{
    get { return passCBIndex; }
    set { passCBIndex = value; }
}

and in your form2 Load event

private void form2_Load(object sender, EventArgs e)
{
    ....
    CBValueForm2.SelectedIndex = passCBIndex;
}

这篇关于将组合框选择传递给另一个win窗体中的其他组合框c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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