hey ya all
my question is simple, how to simulate an event by code, for example consider a simple form which has 2 buttons. button1 changes the forms color, now i want the code that i should put in click event of button2 that when i click it the app thinks that i have clicked button1 and thus changes the form's color as if i have clicked button1.
Events can only be raised from the class that declared them (even derived class' cannot invoke them). Luckily WPF/Forms controls implement protected methods to allow derived classes to do this. Just derive from the Button class, and call OnClick(), for example:
public MyButton : Button
public void SimulateClick()
uh..., i'm a noob in c# so would you mind making it a little bit simpler? actually here is exactly what i'm trying to do:
i have some masked text boxes on a form, if you navigate between them by tab it's fine but if you click in one of them you cant type any thing because the pointer is at the end of where the text would be and you have to either hold the Backspace key or press the Home key, so i want to simulate a Home key keypress so the pointer jumps to the first character on the enter event of the masked text boxes
well it didn't work, i made a simple winapp that i attached for you to see. the 2nd and 3rd text boxes has the code you provided, you see when you enter them by pressing tab there is no problem but if user uses mouse and clicks anywhere other that the most left end of the control he/she can not type the whole amount of desired digits . i think if there is no other way in c#, then the only solution is simulating the home key press or even better then that selecting the whole text in the masked text box upon entering it.
It was not working with tab stops since the SelectionStart/SelectionLength can only be set when the Control has focus. Therefore it should actually be in the GotFocus event. However, the mouse click and caret positioning is done after this. If you use BeginInvoke() method, the message should be processed after the mouse click is handled, for instance:
i'm not sure to even ask this but did you copy and paste this code from C# itsef? it appears to lak a ) after delegate() cuz it dosn't work like this and when i put one it gives me a ton of syntax errors, and before you ask i created a new handler: