Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello can any one help.

I have CheckBox and TextBox in Windows Forms, I would like to have if i make (CheckBox.checked==false) and TextBox.Text is not null, then i would like to make TextBox.clear() and next step I would like TextBox Key.Enter eventargs, then TextBox.Enable==false.

Here is the code
C#
private void checkbox_CheckedChanged(object sender, EventArgs e)
          {
              if (checkbox.Checked == true)
              {
                  this.textBox1.Enabled = true;
              }

               if (checkbox.Checked == false)
              {
                  textBox1.Clear();

              // here i want textBox1.KeyEnterEventArgs "Enter" key pressed ..........

                  this.textBox1.Enabled = false;
              }

          }

Is it possible to do this please, thanks in advance
Posted
Updated 29-Nov-12 22:50pm
v2

1 solution

Don't simulate pressing of a key. If it will do anything, you have an event handler attached anyway. Call that method directly instead of making the event call it.

[Edit]
There are two independent errors in your comment:

1. e.KeyCode = Keys.Enter;
is not possible since e.KeyCode[^] is readonly. What do you want to achieve by doing this?

2. textBox_KeyDown(sender,e);
doesn't work because here your e is of type EventArgs[^], but the method you're calling expects a KeyEventArgs[^] as second parameter.

This is how you could get that to work:
C#
private void MethodNameDescribingTheActionToTake()
{
    // Do whatever needs to be done.
}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    MethodNameDescribingTheActionToTake();
}

private void checkbox_CheckedChanged(object sender, EventArgs e)
{
    if (checkbox.Checked == true)
    {
        this.textBox1.Enabled = true;
    }

    if (checkbox.Checked == false)
    {
        textBox1.Clear();

        MethodNameDescribingTheActionToTake();

        this.textBox1.Enabled = false;
    }
}
[/Edit]
 
Share this answer
 
v2
Comments
getanswer 30-Nov-12 5:11am    
is it possible private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
e.KeyCode = Keys.Enter;

}

how to call in

private void checkbox_CheckedChanged(object sender, EventArgs e)
{
if (checkbox.Checked == true)
{
this.textBox1.Enabled = true;
}

if (checkbox.Checked == false)
{
textBox1.Clear();

textBox_KeyDown(sender,e);

this.textBox1.Enabled = false;
}

}

this is get error.........
lukeer 30-Nov-12 9:00am    
I improved my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900