如何使用form1组合框填充form2中的组合框中的项目 [英] How can I populate items in combobox in form2 with form1 combobox

查看:57
本文介绍了如何使用form1组合框填充form2中的组合框中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式> form1和form2> form1 hs一个文本框和2个按钮>单击button1时,它将文本框值保存在组合框中。当你点击button2时,它会显示form2,它有一个acombobox,它从form1>中的combobox1中检索数据。但它不工作



我尝试过:



多数民众赞成我做了什么>。

in form1

公共部分类Form1:表格

{

public Form1( )

{

InitializeComponent();

}



private void button1_Click(object sender,EventArgs e)

{

Form2 frm2 = new Form2(comboBox1.Text);

frm2.Show();

}

private void button2_Click(对象发送者) ,EventArgs e)

{

comboBox1.Items.Add(textBox1.Text);

}



和形式2

公共部分类Form2:表格

{



public Form2(字符串消息)

{

InitializeComponent();

comboBox1.Text = message;

}



private void Form2_Load(object sender,EventArgs e)

{

Form1 frm1 = new Form1()

}

}

i have 2 forms > form1 and form2> form1 hs one textbox and 2 buttons >when you click button1 it save the textbox value in the combobox. when you click button2 it show form2 which has acombobox that retrieves data from combobox1 in form1> but it is not working

What I have tried:

thats what i have done>.
in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(comboBox1.Text);
frm2.Show();
}
private void button2_Click(object sender, EventArgs e)
{
comboBox1.Items.Add(textBox1.Text);
}

and this in form2
public partial class Form2 : Form
{

public Form2(string message)
{
InitializeComponent();
comboBox1.Text = message;
}

private void Form2_Load(object sender, EventArgs e)
{
Form1 frm1 = new Form1()
}
}

推荐答案

首先看看你在做什么:你的按钮1点击e vent打开一个Form2的实例,在它的load事件中创建一个Form1的新实例 - 但是它没有做任何事情......这很奇怪,有点暗示你不确定你应该做什么。



你要做的事情非常简单:在两种表格之间传递信息,第1部分:父母对儿童 [ ^ ]显示您要执行的操作,但使用文本框而不是组合。看看,稍微玩一下它的代码 - 我知道它有效!
Start by looking at what you are doing: your button1 Click event opens an instance of Form2, which in it's load event creates a new instance of Form1 - but doesn't do anything with it ... which is odd, and kinda implies you aren't sure what you are supposed to be doing.

What you are trying to do is pretty simple: Transferring information between two forms, Part 1: Parent to Child[^] shows exactly what you are trying to do, but with a textbox instead of a combo. Have a look, play with it's code a little - I know that works!


这篇关于如何使用form1组合框填充form2中的组合框中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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