尝试使用“自定义事件”从主窗体对两个不同的子窗体进行更改 [英] Trying to make changes on two different child forms from main form using Custom Events

查看:224
本文介绍了尝试使用“自定义事件”从主窗体对两个不同的子窗体进行更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我希望能够更新listview控件,当一个新的对象以独立的形式创建时,当所有三个显示它们。



MainForm - 包含实例化的List集合,两个实例化CarForm和ListViewCarForm的按钮。 b
$ b

  public static List< Cars> listOfCars =新列表< Cars>(); 

公开列表< Cars> CarList
{
get {return listOfCars; }
set {listOfCars = value; }
}
$ b private void displayListViewToolStripMenuItem_Click(object sender,EventArgs e)
{
ListViewCarForm lvcf = new ListViewCarForm();
lvcf.Show();
}

private void newCarFormButton_Click(object sender,EventArgs e)
{
CarForm cf = new CarForm();
cf.Show();

//将创建的新项目添加到listOfItems
cf.ObjectCreatedToList + = ObjectCreatedToListCollectionHandler;

CarForm - 包含用户输入的控件值,这些值存储在类成员变量中,该变量作为对象创建,然后添加到MainForm中的List集合中

  public EventHandler ObjectCreatedToList; 

//为成员变量赋值的类属性
public Cars信息
{
get
{
//实例化new Cars类,将成员变量赋值给控制值并返回新的Cars对象
}
set
{
//设置控制值给Cars成员变量
}
}
$ b $ private void addCarToolStripButton_Click(object sender,EventArgs e)
{
if(ObjectCreatedToList!= null)
{
ObjectCreatedToList(this,new EventArgs( ));
}
//在此处进行一些验证以防止控制值重置

//如果输入了所有控制值,则会清除控制
Info = new Cars ();

ListViewForm - 包含ListView,被添加到CarForm中的List Collection中,它也应该被添加到listview控件中。我遇到的问题是当所有三种形式都打开时,listview控件里面ListViewForm应该随着CarForm创建的新对象被更新,并被添加到MainForm内部的List集合中。



由于两种形式在不同的按钮中被实例化在MainForm中,我似乎无法弄清楚如何在listview控件中添加项目,而不会触及该方法或给我一个错误。



* This是我第一次使用Windows应用程序表单

解决方案为了保持这个简单,一个解决方案是将所需的变量传递给儿童形式。例如:假设主窗体包含一个 ListBox 项和一个打开 Controlsform 的按钮,那么当用户点击Open Control Form按钮... ControlsForm 打开,它有一个文本框和一个按钮。当用户在文本框中键入内容,然后单击添加到父列表按钮时,文本框中的文本应添加到父窗体的 ListBox 项目中。您可以将 ListBox List< Car> 或您需要的任何内容传递给子窗体。希望这是有道理的。





<$> MainForm 有一个 ListBox 和一个按钮。表单上的 ListBox 被命名为 Form1ListBoxControl 。当用户点击Open Control Form按钮时,变量 Form1ListBoxControl 被传递给新创建的 ControlsForm ,那么显示 ControlsForm ...与上面一样



主表单打开控制表单按钮事件...

pre $ code private void btnOpenControlsForm_Click(object sender,EventArgs e){
ControlsForm controlsForm = new ControlsForm(Form1ListBoxControl);
controlsForm.Show();

ControlsForm ...



要在 ControlsForm 中使用传递的 Form1ListBoxControl 变量,我们需要创建一个全局 ListBox 变量,因此表单中的其他方法可以使用父项 ListBox ...

  ListBox parentListBox; 

另外,我们需要为 ControlsForm创建一个新的构造函数 接受一个 ListBox 参数并使用此参数作为上面定义的全局变量...



<
InitializeComponent();
parentListBox = passedParentListBox;
}
public public ControlsForm(ListBox passedParentListBox)

最后, ControlsForm 中的按钮事件向父项添加新项目 ListBox ...

  private void btnAddToParentList_Click(object sender,EventArgs e){
parentListBox .Items.Add(textBox1.Text);
}


I have an application with three forms.

I want to be able to update the listview control when a new object is being created in a separate form when all three of them are displayed.

MainForm - Contains a List collection instantiated inside, two buttons where CarForm and ListViewCarForm are instantiated.

public static List<Cars> listOfCars = new List<Cars>();

public List<Cars> CarList
{
   get { return listOfCars; }
   set { listOfCars = value; }
}

private void displayListViewToolStripMenuItem_Click(object sender, EventArgs e)
{
   ListViewCarForm lvcf = new ListViewCarForm();
   lvcf.Show();
}

private void newCarFormButton_Click(object sender, EventArgs e)
{
   CarForm cf = new CarForm();
   cf.Show();

   //Adds new item created to listOfItems 
   cf.ObjectCreatedToList += ObjectCreatedToListCollectionHandler;
}

CarForm - Contains controls for the user to enter values, those values are stored inside a class member variable that is created as an object and is then added inside the List Collection in the MainForm

public EventHandler ObjectCreatedToList;

//Class Property that assigns values to member variables
public Cars Info
{
   get
   {
       //Instantiates new Cars class, assigns member variables to control values and returns new Cars object
   }
   set
   {
       //set control values to Cars member variables
   }  
}

private void addCarToolStripButton_Click(object sender, EventArgs e)
{
   if(ObjectCreatedToList != null)
   {
       ObjectCreatedToList(this, new EventArgs());
   }
   //Some Validation here to prevent control values to reset

   //If all control values are entered, this will clear the controls
   Info = new Cars();
}

ListViewForm - Contains a ListView where when items are being added into the List Collection in CarForm, it should also be added to the listview control

The problem I am having is when all three forms are opened, the listview control inside the ListViewForm should be updated as new objects are being created by the CarForm and added inside the List Collection inside the MainForm.

Since the two forms are being instantiated inside a different button in the MainForm, I can't seem to figure out how to add the items inside the listview control without it not hitting the method or giving me an error.

*This is my first time working with Windows Application Forms

解决方案

To keep this simple, one solution is to pass the variable you need to the child form. Example: assuming the main form contains a ListBox of items and a button to open a Controlsform, then, when the user clicks the "Open Control Form" button… the ControlsForm is open, it has a text box and a button. When the user types something into the text box and then clicks the "Add to Parent List" button, the text in the text box should be added to the parent form’s ListBox items. You can pass the ListBox or List<Car> or anything you need to the child form. Hope this makes sense.

The MainForm has a ListBox and a button. The ListBox on the form is named Form1ListBoxControl. When the user clicks the "Open Control Form" button, the variable Form1ListBoxControl is passed to the newly created ControlsForm then the ControlsForm is shown…As above

Main form "Open Control Form" button click event…

private void btnOpenControlsForm_Click(object sender, EventArgs e) {
  ControlsForm controlsForm = new ControlsForm(Form1ListBoxControl);
  controlsForm.Show();
}

ControlsForm...

To use the passed Form1ListBoxControl variable in the ControlsForm we need to make a global ListBox variable so the other methods in the form can use the parents ListBox

ListBox parentListBox;

In addition, we need to create a new "Constructor" for the ControlsForm to accept a ListBox parameter and use this parameter as the global variable defined above.…

public ControlsForm(ListBox passedParentListBox) {
  InitializeComponent();
  parentListBox = passedParentListBox;
}

Finally the button event in the ControlsForm to add new items to the parents ListBox

private void btnAddToParentList_Click(object sender, EventArgs e) {
  parentListBox.Items.Add(textBox1.Text);
}

这篇关于尝试使用“自定义事件”从主窗体对两个不同的子窗体进行更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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