如何将组合框项从一种形式添加到另一种形式的组合框中 [英] How to add combobox item from one form to another form combobox

查看:66
本文介绍了如何将组合框项从一种形式添加到另一种形式的组合框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有1个主要形式,我有一个名为Select Treatment的组合框没有任何项目,即它是空的,下面有一个名为Start new treatment的按钮。在按钮上单击,将出现一个新表单,在该表单中,我有一个组合框,其中包含一些项目和一个保存按钮。问题是当我点击保存按钮时,组合框中的所选项目必须转移到主表单组合框。我该怎么办 ?请帮助!



我尝试过:



开始新的治疗主表单代码上的按钮:



public void button1_Click(object sender,EventArgs e)

{

Form2 f2 = new Form2();

f2。显示();

}



点击按钮点击新表单中出现带有一些项目的组合框和一个保存按钮。

保存按钮代码:



private void button1_Click(object sender,EventArgs e)

{

TreatmentInformation tn = new TreatmentInformation();

tn.comboBox2.Items.Add(comboBox1.SelectedItem);



}

解决方案

这是针对您的问题的两种解决方案。

-1 -Sol 1:为所有非主表单添加parentForm引用。

=>当您创建新表单时,您将初始化您的Parentform支柱:

 Form2 f2 =  new  Form2() ; 
f2.ParentForm = myParentForm;
f2。显示();



当您要使用f2_Closing事件关闭Form2时,您将调用填充Combobox的父方法。

-2-Sol 2:在主表单中订阅f2_closing事件:

 Form2 f2 = new Form2(); 
f2.FormClosed + = new FormClosedEventHandler(f2_Closed);



当form2关闭时,

 f2_Closed 

将执行事件,您可以初始化组合框。


Program.cs 中创建一个公共变量,例如 comboVar

 public static class Program 
{
public static string comboVar;



并在您的表格中访问它:

 tn.comboBox2.Items.Add( Program.comboVar); 


I have 1 main form in that form i have one combobox named Select Treatment with no items in it i.e it is empty and below there is a button named Start new treatment. On button click a new form appears and in that form i have one combobox with some items in it and a save button. the problem is when i clicked the save button the selected item from combobox must transfer to the main form combobox . how do i do ? please help !

What I have tried:

Start new treatment Button on Main form code:

public void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2 .Show ();
}

On button click new form appears with combobox with some items in it and a save button.
Save Button Code:

private void button1_Click(object sender, EventArgs e)
{
TreatmentInformation tn = new TreatmentInformation();
tn.comboBox2.Items.Add (comboBox1.SelectedItem);

}

解决方案

Here is 2 solutions for your problem.
-1-Sol 1 : Add a parentForm refernce for all non main form.
=> when you create a new form, you init your Parentform prop :

Form2 f2 = new Form2();
f2.ParentForm = myParentForm;
f2 .Show ();


When you are about to close the Form2 using f2_Closing event, you call your parent method that fills the Combobox.
-2-Sol 2 : Subscribe to f2_closing event in your main form:

Form2 f2 = new Form2(); 
f2.FormClosed += new FormClosedEventHandler(f2_Closed);


When the form2 is closed the

f2_Closed

event will be executed and you can init your combobox.


In Program.cs create a public variable e.g. comboVar:

public static class Program
{
    public static string comboVar;


And access it in your forms like this:

tn.comboBox2.Items.Add (Program.comboVar);


这篇关于如何将组合框项从一种形式添加到另一种形式的组合框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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