Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi friends
I have 2 forms from 1 and form 2 in c#. form 1 contains a button and when that button click form 2 open. If form 2 is already open make bring to front.
At the situation when form 2 is already open then make a data grid view cell double click event disable and bring to front.
How can I do it?? please help me.

my code is like this. but it shows error

C#
private void btn_addgroup_Click(object sender, EventArgs e)
       {
          
           form_2 myfrm = Application.OpenForms["form_2"] as form_2 ;
           if (myfrm != null)
           {
               
               if (myfrm.WindowState == FormWindowState.Minimized)
               {
                   myfrm.WindowState = FormWindowState.Normal;
               }
               myfrm.Activate();
               myfrm.BringToFront();
               myfrm.Activated += new EventHandler(myfrm_Activated);
              

               return;
           }
           else
           {
               myfrm = new form_2(this);
               myfrm.MdiParent =this.ParentForm ;
               myfrm.Show();
           }
       }

       void myfrm_Activated(object sender, EventArgs e)
       {
           dgv_product.CellDoubleClick -= new DataGridViewCellEventHandler(dgv_product.CellDoubleClick);
       }
Posted
Updated 16-Jan-16 0:21am
v4
Comments
Richard Deeming 16-Jan-16 6:35am    
Are we supposed to guess what the error is?

Click "Improve question" and update your question with the full details of the error. Include the error message and the stack trace, and don't forget to indicate which line of your code the error is thrown from.
BillWoodruff 16-Jan-16 8:05am    
"when form 2 is already open then make a data grid view cell double click event disable and bring to front."

Why would you want to disable the DataGridView Cell DoubleClick Event ?

The double click in the DataGridView cell on Form2 hides Form2 ?
Member 12197595 17-Jan-16 22:45pm    
show error in this line
dgv_product.CellDoubleClick -= new DataGridViewCellEventHandler(dgv_product.CellDoubleClick);

error is,
The event 'System.Windows.Forms.DataGridView.CellDoubleClick' can only appear on the left hand side of += or -=


When form_2 is activate by clicking the button. then I want to disable the event..
Richard Deeming 18-Jan-16 7:18am    
You are trying to remove the event from itself, which will not work. You need to remove the previously subscribed event handler.

Something like this:
dgv_product.CellDoubleClick -= this.dgv_product_CellDoubleClick;

1 solution

If I understand the question correctly, you try to manipulate objects in Form2 from outside the form. If this the case, I would suggest not doing so.

Instead create a new method to Form2, for example DisableProductClick. This method should contain the relevant logic that changes how objects in Form2 behave. From the form1 when you find existing form2, just like you call BringToFront, you would call your new method. So the code in form1 would be something like
C#
form 2 myfrm = Application.OpenForms["form2"] as form2 ;
           if (myfrm != null)
           {
               
               if (myfrm.WindowState == FormWindowState.Minimized)
               {
                   myfrm.WindowState = FormWindowState.Normal;
               }
               myfrm.Activate();
               myfrm.BringToFront();
               myfrm.DisableProductClick();
...

Also note that there probably isn't a space in the form name so that should be corrected
 
Share this answer
 
Comments
Member 12197595 16-Jan-16 6:25am    
Thank you sir. But I need the code for disable the datagridview celldoubleclick event.
How can I do it?

my code show error in this line
dgv_product.CellDoubleClick -= new DataGridViewCellEventHandler(dgv_product.CellDoubleClick);
Wendelius 16-Jan-16 6:30am    
And what is the error?
Member 12197595 17-Jan-16 22:39pm    
The event 'System.Windows.Forms.DataGridView.CellDoubleClick' can only appear on the left hand side of += or -=
this is the error..

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