Can I use AcceptButton() to move to the next textbox? - c#

I'm doing a simple login screen and I have already implemented a simple method in the password textbox to simulate the 'OK" button being clicked:
private void textpwd_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = btnLogin;
}
can I use the same method in the username textbox to move to the password textbox?
private void textusername_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = textpassword.Focus();
}

private void textusername_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode==Keys.Enter)
textpassword.Focus(); // or SendKeys.Send("{Tab}");
}

Not to write a specific code for each of your TextBox(es) it's better to simulate the Tab key press behavior.
Edit the Tab order via View -> TabOrder based on your needs.
Set the following method for all of your form controls KeyDown event
private void AllControls_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SendKeys.Send("{TAB}");
e.SuppressKeyPress = true;
}
}

Related

I got a multiple buttons event handler here But I need to target only one button if it was clicked how do I do that?

private void btnCodeAkas_Click(object sender, EventArgs e)
{
tmrTimerAkas.Start();
tmrTimerTwoAkas.Start();
}
But I need to target only one button if it was clicked how do I do that?
well you can cast your sender to Control object so you can get the name of the button then you write your conditional logic.
private void button1_Click(object sender, EventArgs e)
{
var control = sender as Control;
if (control.Name == "button1")
{
Console.WriteLine($"Clicked {control.Name} button");
}
else if (((Control)sender).Name == "button2")
{
Console.WriteLine($"Clicked {control.Name} button");
}
}

1 button, 2 webpages, but one webpage at a time?

Here's what H have so far:
private void button1_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.ai_yori_aoshi_5370;
}
private void button2_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.AiYoriAoshi_feature;
}
private void button3_Click(object sender, EventArgs e)
{
audio.Stop();
if (button1.Enabled == true)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide");
if (button2.Enabled == true)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide");
}
}
}
this is only my test so far but what i want to do is change what button 3 does, i.e. if button 1 is clicked button three will open webpage 1, if button2 is clicked button 3 will open webpage 2, button 3's image will change depending, but what im finding with what i have done so far is that it opens BOTH pages AT THE SAME TIME ... how to i prevent this? i have tried if, else and else if, same result every time.
Both of your buttons are enabled, you are checking to see if the buttons are enabled or disabled (clickable or not), not which one has been clicked.
also:if (button2.Enabled == true)
is nested in the first conditional, I'm not sure if that's what you want.
You can: disable buttons 1 and 2 after their clicked so that, for instance button2.Enabled will now = false; (but then you will not be able to reclick that button)
More sophisticated, but better, is to use a delegate for the button3, and assign them in your button1_Click and button2_Click events. Something like this:
private void button1_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.ai_yori_aoshi_5370;
button3.Click += new EventHandler(this.Button3_Click_First);
}
private void button2_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.AiYoriAoshi_feature;
button3.Click += new EventHandler(this.Button3_Click_Second);
}
void Button3_Click_First(Object sender,
EventArgs e)
{
// When the button is clicked,
// change the button text, and disable it.
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide");
}
void Button3_Click_Second(Object sender,
EventArgs e)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide");
}
You may also have to check and make sure an event handler was not previously assigned, in calse someone clicks button1, then button2, then button1 ect. This is described here: Removing event handlers
You can handle your problem by storing the URL of the webpage in a private field, setting it when buttons 1 or 2 are clicked and reading from it after clicking button 3.
private string _address = null;
private void button1_Click(object sender, EventArgs e)
{
// do other stuff
_address = "http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide";
}
private void button2_Click(object sender, EventArgs e)
{
// do other stuff
_address = "http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide";
}
private void button3_Click(object sender, EventArgs e)
{
if (_address != null)
{
audio.Stop();
if (button1.Enabled || button2.Enabled)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start(_address);
}
}
}
I wasn't sure if all the code in button3_Click is necessary, so I cleared it up a little. I might be a bit off, though.
button.Enabled is always true for all buttons by default unless you set it to false. So you cannot use button1.Enabled property to check which button is pressed. try below approach.
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["Button1Clicked"] = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
ViewState["Button1Clicked"] = false;
}
protected void Button3_Click(object sender, EventArgs e)
{
if ((bool)ViewState["Button1Clicked"])
{
//open webpage2 code comes here
}
else
{
//open webpage2 code comes here
}
}

How to select all text in textbox when it gets focus

In Windows phone, how can I select all text in Textbox when the TextBox has focus?
I try setting the get focus property of Textbox:
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox .SelectAll();
}
What I see is I see all the text is being selected for 1-2 sec and then it goes back to cursor mode (i.e. 1 blink line).
I had this same problem on WPF and managed to solve it. Not sure if you can use what I used but essentially your code would look like:
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox .CaptureMouse()
}
private void TextBox_GotMouseCapture(object sender, RoutedEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.SelectAll();
}
private void TextBox_IsMouseCaptureWithinChanged(object sender, RoutedEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.SelectAll();
}
All events hooked up to the original textbox. If this doesn't work for you, maybe you can replace CaptureMouse with CaptureTouch (and use the appropriate events). Good luck!
You can try this code,
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
String sSelectedText = mytextbox.SelectedText;
}
If user clicks on copy icon that comes after selection it will get copied, if you want to do it programmatically you can try this
DataPackage d = new DataPackage();
d.SetText(selectedText);
Clipboard.SetContent(d);
I would suggest doing the copying in some other event rather than gotfocus, as this will be triggered immediately after user taps on text field so this method will be called when there is no text actually entered.
protected override void OnStartup(StartupEventArgs e)
{
//works for tab into textbox
EventManager.RegisterClassHandler(typeof(TextBox),
TextBox.GotFocusEvent,
new RoutedEventHandler(TextBox_GotFocus));
//works for click textbox
EventManager.RegisterClassHandler(typeof(Window),
Window.GotMouseCaptureEvent,
new RoutedEventHandler(Window_MouseCapture));
base.OnStartup(e);
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
(sender as TextBox).SelectAll();
}
private void Window_MouseCapture(object sender, RoutedEventArgs e)
{
var textBox = e.OriginalSource as TextBox;
if (textBox != null)
textBox.SelectAll();
}

KeyUp in datagridview with EditOnEnter not working

I have DataGridView which EditMode is on EditOnEnter. in that case the KeyUP method doesn't work? if i want to have KeyUp i should set the EditMode in EditOnKeyStroke or EditOnKeyStrokeOrF2.
I have set
this.KeyPreview = true;
on Form Load but it doesn't work.
how can i fire Key up in EditOnEnter?
Use
private void YourDataGridView_KeyUp(object sender, KeyEventArgs e)
{
---- Check Column Name is == Your Column Name
---- set the EditMode in EditOnKeyStroke or EditOnKeyStrokeOrF2
}
I am giving this Example just because You was not Posted your DataGridView Codes.
UPDATE : AS per Your Need those Events can be Use full...
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString ());
}
private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
MessageBox.Show("dataGridView1_KeyUp" + e.KeyData );
}
private void dataGridView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
MessageBox.Show("dataGridView1_PreviewKeyDown" + e.KeyData);
}

Changing the BackColor of a textbox upon entry

I have the following code for my form:
private void txt1_Enter(object sender, EventArgs e)
{
txt1.SelectAll();
txt1.BackColor = Color.LightBlue;
}
private void txt2_Enter(object sender, EventArgs e)
{
txt2.SelectAll();
txt2.BackColor = Color.LightBlue;
}
private void txt1_Leave(object sender, EventArgs e)
{
txtThermalConductivity.BackColor = Color.White;
}
private void txt2_Leave(object sender, EventArgs e)
{
txtThermalConductivity.BackColor = Color.White;
}
There are another 20 textboxes on my form that I would like to do the same for. Is it possible to combine all of the enter events and all of the leave events so I have two events in total rather than 44 individual events?
In your Designer view, select each textbox and set the Enter and Leave events to point to a single implementation of each.
Then you can do this:
private void txt_enter(object sender, EventArgs e) {
((TextBox)sender).BackColor = Color.LightBlue;
}
private void txt_leave(object sender, EventArgs e) {
((TextBox)sender).BackColor = Color.White;
}
Also, SelectAll isn't required because you're setting the entire textbox's background color.. not the SelectionColor of a RichTextBox.
You could either add manually or iterate over all textboxes in form (extension method found here GetChildControls.
foreach (TextBox textBox in this.GetChildControls<TextBox>())
{
textBox.Enter += new EventHandler(TextBox_Enter);
textBox.Leave += new EventHandler(TextBox_Leave);
}
The above can be called from the Form's Load event.
The event listener now can look like the following by casting the sender to TextBox.
private void TextBox_Enter(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
txtBox .SelectAll();
txtBox .BackColor = Color.LightBlue;
}
private void TextBox_Leave(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
txtBox.BackColor = Color.White;
}
It is, just use something like the following:
private void tbLeave(object sender, EventArgs e) {
((TextBox) sender).BackColor = Color.White;
}
The set the controls event declaration to point to this function.
You can also do the same for the Leave() event.
(Just a little note to say, I much prefer to handle this kind of thing client side where possible.)

Categories

Resources