How to activate event procedure
-
-
Hi, I am looking for a way to activate an event procedure, such as combox_clicked, without really clicking on the control . I remember in VB we can do thing like that (combox_clicked = true). Just wonder can we do this in C#? I appreciate your inputs.
Hi You just need to call the event handler :) Like: First derive from Button, then add the following: public void CallButtonClick() { base.OnClick(this, EventArgs.Empty); } Now , call the function from your code :) Hope this helps, there seems to be many ways to do this though. Give them a chance! Do it for the kittens, dear God, the kittens!
-
Hi You just need to call the event handler :) Like: First derive from Button, then add the following: public void CallButtonClick() { base.OnClick(this, EventArgs.Empty); } Now , call the function from your code :) Hope this helps, there seems to be many ways to do this though. Give them a chance! Do it for the kittens, dear God, the kittens!
Hi Leppie, I am not sure if I understand what you mean by :confused: leppie wrote: First derive from Button, then add the following: public void CallButtonclick() { base.onclick(this, EventArgs.Empty); } Can you please give more detailed explaination? I really appreciate that!!;)
-
Hi Leppie, I am not sure if I understand what you mean by :confused: leppie wrote: First derive from Button, then add the following: public void CallButtonclick() { base.onclick(this, EventArgs.Empty); } Can you please give more detailed explaination? I really appreciate that!!;)
D Shen wrote: I am not sure if I understand what you mean by It's the beauty of inheritance :) OK. Lets see how/why I say that :) 1. What we want is a button that we can programatically fire events. It makes sense to add this functionality to the Button class. So we make one:
public class ButtonEx():System.Windows.Forms.Button
{
public void CallButtonclick()
{
base.OnClick(EventArgs.Empty); // sorry mistake in first post :)
}
}2. We replace our existing Button in the in the form with our new ButtonEx.
private System.Windows.Forms.Button button1; //now becomes
private ButtonEx button1;And in the windows forms designer region, change :
button1 = new System.Windows.Forms.Button(); //now changes to
button1 = new ButtonEx();Be sure to save before viewing in the designer(in fact close it beforehand, it does more harm than good :laugh:). 3. Finally we just call our new ButtonEx's CallButtonClick methods as follows:
button1.CallButtonClick();
NOTE: If you'll be calling this method from thread u will need to invoke it. I'm not 100% sure, but it would look something like this:
button1.Invoke( new MethodInvoker(button1.CallButtonClick));
Hope this adds some insight :) Give them a chance! Do it for the kittens, dear God, the kittens!
-
Hi Leppie, I am not sure if I understand what you mean by :confused: leppie wrote: First derive from Button, then add the following: public void CallButtonclick() { base.onclick(this, EventArgs.Empty); } Can you please give more detailed explaination? I really appreciate that!!;)
-
D Shen wrote: I am not sure if I understand what you mean by It's the beauty of inheritance :) OK. Lets see how/why I say that :) 1. What we want is a button that we can programatically fire events. It makes sense to add this functionality to the Button class. So we make one:
public class ButtonEx():System.Windows.Forms.Button
{
public void CallButtonclick()
{
base.OnClick(EventArgs.Empty); // sorry mistake in first post :)
}
}2. We replace our existing Button in the in the form with our new ButtonEx.
private System.Windows.Forms.Button button1; //now becomes
private ButtonEx button1;And in the windows forms designer region, change :
button1 = new System.Windows.Forms.Button(); //now changes to
button1 = new ButtonEx();Be sure to save before viewing in the designer(in fact close it beforehand, it does more harm than good :laugh:). 3. Finally we just call our new ButtonEx's CallButtonClick methods as follows:
button1.CallButtonClick();
NOTE: If you'll be calling this method from thread u will need to invoke it. I'm not 100% sure, but it would look something like this:
button1.Invoke( new MethodInvoker(button1.CallButtonClick));
Hope this adds some insight :) Give them a chance! Do it for the kittens, dear God, the kittens!
-
D Shen wrote: I am not sure if I understand what you mean by It's the beauty of inheritance :) OK. Lets see how/why I say that :) 1. What we want is a button that we can programatically fire events. It makes sense to add this functionality to the Button class. So we make one:
public class ButtonEx():System.Windows.Forms.Button
{
public void CallButtonclick()
{
base.OnClick(EventArgs.Empty); // sorry mistake in first post :)
}
}2. We replace our existing Button in the in the form with our new ButtonEx.
private System.Windows.Forms.Button button1; //now becomes
private ButtonEx button1;And in the windows forms designer region, change :
button1 = new System.Windows.Forms.Button(); //now changes to
button1 = new ButtonEx();Be sure to save before viewing in the designer(in fact close it beforehand, it does more harm than good :laugh:). 3. Finally we just call our new ButtonEx's CallButtonClick methods as follows:
button1.CallButtonClick();
NOTE: If you'll be calling this method from thread u will need to invoke it. I'm not 100% sure, but it would look something like this:
button1.Invoke( new MethodInvoker(button1.CallButtonClick));
Hope this adds some insight :) Give them a chance! Do it for the kittens, dear God, the kittens!
-
leppie, Here comes a minor problem :-O When I compile the code, I got "...does not contain a definition for..." this button1. I followed the order you post here, but....:~ Please help!!