Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I have created delegate and event and try to subscribe event on form1 but it dot get subscibe if i will move event subscribe code to form2 then there is not problem. can any one have look for me plz. thanks in advance::::


C#
_mainform(form1):

        namespace KasseDelegate
{

    public delegate void ListViewUpdatedEventHandler(object sender, ListViewUpdatedEventArgs e);
    public partial class Form1 : Form
    {
        private Form3 frm3;

        public Form1()
        {
            InitializeComponent();
            frm3 = new Form3();
            frm3.ListViewUpdated += new ListViewUpdatedEventHandler(Frm3_ListViewUpdated1);

        }

        private void Frm3_ListViewUpdated1(object sender, ListViewUpdatedEventArgs e)
        {
            MessageBox.Show("hi");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show();

        }

    }
}




_searchform(form2) :




C#
public partial class Form2 : Form
       {

           public Form2()
           {
               InitializeComponent();
           }

           private void button1_Click(object sender, EventArgs e)
           {
               using (SQLiteConnection con = new SQLiteConnection(Properties.Settings.Default.ConnectionString))
               {
                   con.Open();
                   SQLiteCommand cmd = new SQLiteCommand("select * from varer where varenummer=@Varenummer", con);
                   cmd.Parameters.AddWithValue("@Varenummer", "101");

                   SQLiteDataReader dr = cmd.ExecuteReader();
                   Form3 frm3 = new Form3(dr);
                   frm3.Show();
               }
           }
       }



Form 3 :
C#
namespace KasseDelegate
{

    public partial class Form3 : Form
    {
        public event ListViewUpdatedEventHandler ListViewUpdated;
        SQLiteDataReader dr1;
        public Form3()
        {
            InitializeComponent();

        }
        public Form3(SQLiteDataReader dr)
        {
            InitializeComponent();
            dr1 = dr;
        }

        private void Form3_Load(object sender, System.EventArgs e)
        {
            if (dr1 != null)
            {

                while (dr1.Read() == true)
                {
                    ListViewItem LVI = new ListViewItem();
                    LVI.SubItems.Add(dr1[0].ToString());
                    LVI.SubItems.Add(dr1[1].ToString());
                    LVI.SubItems.Add(dr1[2].ToString());
                    LVI.SubItems.Add(dr1[3].ToString());
                    LVI.SubItems.Add(dr1[4].ToString());
                    listView1.Items.Add(LVI);

                }
            }


What I have tried:

I have submitted the code and i have tried my best to find out what is problem in that nd not able to understand. plz plz help me out
Posted
Updated 7-Jul-16 23:18pm
v2
Comments
johannesnestler 8-Jul-16 5:12am    
ahhhh.... so what's the Problem again? Can ýou please (plz, plz) try to formulate your question/Problem a little better?

1 solution

Well...that's pretty simple to understand.
You create an instance of Form3 in Form1 and you attach a handler:
C#
frm3 = new Form3();
frm3.ListViewUpdated += new ListViewUpdatedEventHandler(Frm3_ListViewUpdated1);
But you never show teh form, so the actual Listview never gets updated.
Conversely, in Form2 you create an instance of Form3 and show it, but you never attach a handler:
C#
Form3 frm3 = new Form3(dr);
frm3.Show();

So that version gets it's ListView updated, and (presumably, you don't show it doing that) signals the event, but you don't have a handler so nothing happens.

Have a look at this: Transferring information between two forms, Part 2: Child to Parent[^]
The sample code should show you what to do.
 
Share this answer
 
Comments
Member 12545398 8-Jul-16 5:24am    
Thank for reply dear!

am developing window application POS. Requirement is: when user click on button 'seach item' on _mainform(form 1) then it open _Searchform(form2) and on the search form it display result in listview with select item button on form3 and close _searchform(form2). The item that we selected from listview , we add in _mainform(form1) listview.

I try to implement this functionality with delegate and event. On form3 (search result form) i have declared delegate and event and subscribe that even on form1(main form).


In that solution u provided is from child to parent , but there is 3 forms.
OriginalGriff 8-Jul-16 5:47am    
That's the wrong way to do it.
Form1 doesn't know about Form3 - and nor should it if Form2 opens the window.
And if Form2 closes... that's bad design!

Instead, create a Results event in Form2 which Form1 handles.
Form2 signals the event and either closes or not as it chooses.
The Form1 handles the event and in the handler retrieves the info from Form2, creates Form3 and passes it the data it needs.
That way, the Search form doesn't need to know what is done with the results, and the Main form doesn't need to know how the Results form is displaying it.
Member 12545398 8-Jul-16 6:05am    
So shall i create event in form2 rather then form3, and form1 handle that event means subscribe it. In that handle functionon (on form 1) i need to create form3 and display data and select after that>
OriginalGriff 8-Jul-16 6:14am    
Yes!
Member 12545398 8-Jul-16 6:43am    
But my requirement is from form1 i click search and it open form2 and from form2 when i enter item no it show result in form3 and i would like to select item from form3 to show in form1.

if i create event in form 2 and handle it in form1 how do i know which item user selected on form3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900