如何将Form1 Combox1值刷新到Form2 Combox1 [英] how to Form1 Combox1 value refresh to Form2 Combox1
问题描述
在我的c#应用程序中,有一个组合框,其中列出了客户名称,它放在MainForm中。有一个按钮用于在单击按钮时添加新客户,另一个表单打开,我们需要保存客户详细信息。在此之后我想重新加载组合框的'MainForm'以包含新名称或组合框以显示新名称。
AddForm frmAdd = new AddForm() ;
this.Hide();
frmAdd.ShowDialog();
尝试{
this.Show ();
}
然后在按钮上保存用户或返回MainForm:
MainForm frmMain = new MainForm();
this.Hide();
frmMain.ShowDialog();
try {
this.Show();
}
此代码也有帮助,但我希望旧形式刷新.. 。
没有新表格打开..
plzzzzzzzzzz帮帮我
In my c# application there is a combo box listing 'customer names' which is placed in the MainForm .There is a button for adding new customers when clicking on the button another form opens and we need to save customer details. After this i want to reload the 'MainForm' for the combobox to include the new name or the combobox to display the new name .
AddForm frmAdd = new AddForm();
this.Hide();
frmAdd.ShowDialog();
try{
this.Show();
}
Then on the button to save the user or return to the MainForm:
MainForm frmMain = new MainForm();
this.Hide();
frmMain.ShowDialog();
try{
this.Show();
}
this code is also help ful but iwant to old form to refresh ...
no new form open..
plzzzzzzzzzz help me
推荐答案
// Form1.cs
//Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Form2 objForm2 = new Form2();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.Items.Add("Customer1");
comboBox1.Items.Add("Customer2");
comboBox1.Items.Add("Customer3");
comboBox1.Items.Add("Customer4");
comboBox1.SelectedIndex = 0;
}
private void btnAddCustomer_Click(object sender, EventArgs e)
{
this.Hide();
objForm2.ShowDialog();
if(null != objForm2 && objForm2.ItemAdded != string.Empty)
comboBox1.Items.Add(objForm2.ItemAdded);
this.Show();
}
}
}
//Form2.cs
//Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string ItemAdded
{
get;
set;
}
private void btnSave_Click(object sender, EventArgs e)
{
ItemAdded = txtCustomerName.Text;
this.Close();
}
}
}
请参阅我的提示/技巧文章:< a href =http://www.codeproject.com/Tips/889332/Many-Questions-Answered-at-Once-Collaboration-betw>一次回答的许多问题 - Windows窗体或WPF Windows之间的协作 [ ^ ]。
-SA
Please see my Tips/Tricks article: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows[^].
—SA
这篇关于如何将Form1 Combox1值刷新到Form2 Combox1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!