Click here to Skip to main content
12,079,011 members (47,227 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# WebForms
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
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 29-Nov-12 22:57pm
Edited 29-Nov-12 23:50pm
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:
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]
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160212.1 | Last Updated 30 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100