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.
thanks guys...

Recommended Answers

All 8 Replies

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()
    {
        OnClick();
    }
}

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

You don't need to simulate a key press. You could just set maskedTextBox.SelectionStart = 0; and maskedTextBox.SelectionLength = 0; .

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.
thanks again

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:

private void maskedTextBox2_GotFocus(object sender, EventArgs args)
{
    BeginInvoke((MethodInvoker)delegate()
    {
        maskedTextBox2.SelectionStart = 0;
        maskedTextBox2.SelectionLength = 0;
    }
}

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:

public Form1()
        {
            InitializeComponent();
            maskedTextBox2.GotFocus +=new EventHandler(maskedTextBox2_GotFocus);
        }

void  maskedTextBox2_GotFocus(object sender, EventArgs e)
{
     BeginInvoke((MethodInvoker)delegate()
    {
        maskedTextBox2.SelectionStart = 0;
        maskedTextBox2.SelectionLength = 0;
    }
}

No I did not...this should work:

private void maskedTextBox2_GotFocus(object sender, EventArgs args)
{
    BeginInvoke((MethodInvoker)delegate()
    {
        maskedTextBox2.SelectionStart = 0;
        maskedTextBox2.SelectionLength = 0;
    });
}

yes it works, and thank you, i made it a little better by selecting all the text in it istead of moving the pointer at the beginning of text:

private delegate void SetMaskedTextBoxSelectAllDelegate(MaskedTextBox mask);

        

        private void mtx_Enter(object sender, EventArgs e)
        {

            this.BeginInvoke(new SetMaskedTextBoxSelectAllDelegate(SetMaskedTextBoxSelectAll), new object[] { (MaskedTextBox)sender });

        }

        private void SetMaskedTextBoxSelectAll(MaskedTextBox mask)
        {

            mask.SelectAll();

        }

i also found another way of doing it, not entirely different tho:

private delegate void SetMaskedTextBoxSelectAllDelegate(MaskedTextBox mask);

        

        private void mtx_Enter(object sender, EventArgs e)
        {

            this.BeginInvoke(new SetMaskedTextBoxSelectAllDelegate(SetMaskedTextBoxSelectAll), new object[] { (MaskedTextBox)sender });

        }

        private void SetMaskedTextBoxSelectAll(MaskedTextBox mask)
        {

            mask.SelectAll();

        }

any ways thank you very much you helped a lot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.