开发者

how to add item in form2 - and see hem in ListView in Form1?

开发者 https://www.devze.com 2022-12-20 16:27 出处:网络
I have 2 forms, in form 2 i have button that add开发者_Go百科s Fname and Lname items, when i press this button .I want to see those items in ListView that is in Form1

I have 2 forms, in form 2 i have button that add开发者_Go百科s Fname and Lname items, when i press this button . I want to see those items in ListView that is in Form1

thank's in advance


This sounds like a case where you'd want a presenter class which was responsible for populating and maintaining both forms with the same data. Then you could expose the button click event from your form, and hook your presenter up to handle it. From here you can choose to update whatever form you want, in whatever way you want.

For example:

public class MyForm1 : Form, IMyForm1
{
   ... // Bunch of other stuff
   public event<EventHandler> onButtonClick;
}

public class MyPresenter
{
   public static void Main()
   {
       ... // Other stuff
       myForm1.onButtonClick += new EventHandler<EventArgs>(ButtonHandler);
   }

   private void ButtonHandler(object sender, EventArgs e)
   {
       // Add item to form1
       ...
       // Add item to form2. Eg:
       form2.AddListItem(...);
   }
}

The idea of having a presenter is that you have a centralised location for logic relating to those forms, so the forms can be as thin as possible, and they don't even have to know about each other.

Hope that helps. Give me a shout if I've missed the point or need to clarify anything I've said.


You will need to either create a public method in Form1 to add the values, or expose the listview as public from Form1.

Maybe something like

public void AddToListView(string fname, string lname)
{
    //add values here to ListView
}
0

精彩评论

暂无评论...
验证码 换一张
取 消