Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling event from within another event

Status
Not open for further replies.

raydona

Programmer
May 12, 2005
27
GB
Sorry, I am using C++ syntax, but it should be the same for C#. How can I call one event from within another? For example, say I have the two events, one for a button and one for a textbox.
private: System::Void Button_Click(System::Object^ sender, System::EventArgs^ e)
private: System::Void TextBox_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)

In the button’s click event after processing something I wish to focus on text box and validate user input in textbox. I have tried the following:

private: System::Void Button_Click(System::Object^ sender, System::EventArgs^ e)
{ // Do something
if(TextBox->CanFocus == true)
{ TextBox->Focus();
}
TextBox_KeyPress();
}
It doesn’t work because I do not know what arguments to pass. Also after validating user input in KeyPress event I wish to return to Button_Click event. Can I do that by simply saying ‘return’ in KeyPress event? Thank you for all help.
 
If you want to "call" that event then you can pass NULL values
 
Why don't you use the CausesValidationChanged event? This event is called whenever the control needs to be validated. That will seperate the validation action from the button action and provide a more encapsulated solution.

im in ur stakz, overflowin ur heapz!
 
If you want to "call" that event then you can pass NULL values

This will only work if you don't use either of the parameters passed into the method.

private void button_Click(object sender, EventArgs e)
{
Button btn = (Button)sender; //Null reference exception!
Console.WriteLine(btn.Text);
}

 
Really not sure why you would want to do this, but here's an example of how. In this example, clicking on the button will show the message box.
Code:
private void button1_Click(object sender, EventArgs e)
{
    textBox1_TextChanged(sender, e);
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    MessageBox.Show("hi");
}

There's a thin line between genius, and insanity!
 
After reading your post again I think something like this would be better for you:
Code:
private void button1_Click(object sender, EventArgs e)
{
    // do something here.

    ValidateText();

    // continue here.
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    ValidateText();
}

private bool ValidateText()
{
    try
    {
        // validation code goes here.
        // For example:
        if (textBox1.Text.Trim().Length == 0)
        {
            return false;
        }

        return true;
    }
    catch
    {
        return false;
    }
}

There's a thin line between genius, and insanity!
 
if you really have to call one event from another, you could overload the method so that you can provide default parameters (OTTOMH):

Code:
private void textBox1_KeyPress()
{
    textBox1_KeyPress(this, new KeyPressEventArgs());
}



mr s. <;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top