Windows Develop Bookmark and Share   
 index > Windows Forms General > event +C#
 

event +C#


how can call a event from one Form to event in the another form..(note it is not mdi)...out these two forms one is mdichildform and other not>>
Regards,
ALGATES...
algates  Monday, March 24, 2008 11:19 AM

You could try something like this:
-
Code Snippet

---------------------------------------------------------------------------

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}


private void button1_Click(object sender, EventArgs e)

{

DoSomething();

}


public void DoSomething()

{

//Have your implementation here for e.g:

this.button1.Dock = DockStyle.Fill;

}

}

--------------------------------------------------------------------------

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}


private void button1_Click(object sender, EventArgs e)

{

Program.CallForm1DoSomething();

}

}

---------------------------------------------------------------------------

// In the Program class add this method:

public static void CallForm1DoSomething()

{

//If Form1 is already open

foreach (Form form in Application.OpenForms)

{

if (form is Form1)

{

((Form1)form).DoSomething();

}

}


// If Form1 is not already open create an instance and call DoSeomthing() on that
}
-----------------------------------------------------------------------------------------------------------------------------------------



Hope this helps. Let me know if this worked, otherwise we can try other things.

GoodLuck
Sai Gudigundla

SaiPrasad  Monday, March 24, 2008 4:46 PM

Hi algates,

In my point view, the event handler is actually a method, whose first parameter is the control which trigger this event and the second parameter is the event argument. You can call the event handler anywhere if you can get access to this handler. Thus you can add a reference to the form instance and call this event handler. Try something like this:

Code Snippet

public partial class Form2 : Form

{

public Form2( Form1 f)

{

InitializeComponent();

form = f;

}

Form1 form;

private void button2_Click(object sender, EventArgs e)

{

if (form != null)

{

form.button1_Click(null, null);

/* if your button1 in form1 click event hander has something to with button1 and EventArgs,

* you should try something like the following

form.button1_Click(form.button1, new EventArgs());

*/

}

}

}

But the above method doesn’t actually trigger the event; it just invoked the event handler. To trigger the event, you should send some windows message to the button1 in your form1.

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Friday, March 28, 2008 7:39 AM
hi



namespace myform
{

Class Form1:Form
{
public Button button1;
public Form1()
{
button1=new Button("Good morning");
this.button1.click+=new EventHandler(this.KickME);
--------
--------
----------
InitializeComponent()
}

public void KickME(object sender,EventArgs e)
{
this.button1.Dock=DockStyle.Left;
}
}
=====================
namespace myform
{
Class Form2:Form
{
public Button button3;
public Form2()
{
button3=new Button("Good night");
this.button3.click+=new EventHandler(this.
HIT_ME);
--------
--------
----------
InitializeComponent()
}

public void HIT_ME(object sender,EventArgs e)
{
///FROM HERE I WANT TO CALL FORM1 event
KickME without creating instance for Form1
}

}


this is my requirement ......help me..
CHEERS & REGARDS,

ALGATES.......

algates  Monday, March 24, 2008 12:40 PM

You could try something like this:
-
Code Snippet

---------------------------------------------------------------------------

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}


private void button1_Click(object sender, EventArgs e)

{

DoSomething();

}


public void DoSomething()

{

//Have your implementation here for e.g:

this.button1.Dock = DockStyle.Fill;

}

}

--------------------------------------------------------------------------

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}


private void button1_Click(object sender, EventArgs e)

{

Program.CallForm1DoSomething();

}

}

---------------------------------------------------------------------------

// In the Program class add this method:

public static void CallForm1DoSomething()

{

//If Form1 is already open

foreach (Form form in Application.OpenForms)

{

if (form is Form1)

{

((Form1)form).DoSomething();

}

}


// If Form1 is not already open create an instance and call DoSeomthing() on that
}
-----------------------------------------------------------------------------------------------------------------------------------------



Hope this helps. Let me know if this worked, otherwise we can try other things.

GoodLuck
Sai Gudigundla

SaiPrasad  Monday, March 24, 2008 4:46 PM

Hi algates,

In my point view, the event handler is actually a method, whose first parameter is the control which trigger this event and the second parameter is the event argument. You can call the event handler anywhere if you can get access to this handler. Thus you can add a reference to the form instance and call this event handler. Try something like this:

Code Snippet

public partial class Form2 : Form

{

public Form2( Form1 f)

{

InitializeComponent();

form = f;

}

Form1 form;

private void button2_Click(object sender, EventArgs e)

{

if (form != null)

{

form.button1_Click(null, null);

/* if your button1 in form1 click event hander has something to with button1 and EventArgs,

* you should try something like the following

form.button1_Click(form.button1, new EventArgs());

*/

}

}

}

But the above method doesn’t actually trigger the event; it just invoked the event handler. To trigger the event, you should send some windows message to the button1 in your form1.

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Friday, March 28, 2008 7:39 AM

You can use google to search for other answers

Custom Search

More Threads

• Determining whether a control came from an inherited form
• Recurse Folders and Files
• what verstion to use?
• Multiple HotKeys in the same form ?
• Catch Combination Keys
• How to select all the checkbox from datagrid when user click submit button
• Form.InvokeRequired fails
• Setting ShowInTaskbar is throwing an error.
• DateTime Inc - but how?
• open a web page