MaskedTextBox.SelectAll on GotFocus doesn't work with mouse - c#

I want to select all the contents of a MaskedTextBox when the clicks (or tabs onto) the control, so they can easily replace the old content. I tried calling SelectAll() in the Enter event, but that didn't work at all.
I switched to using the GotFocus event, which works great when tabbing through controls, but doesn't work when I click on it with the mouse. I would only want to select all the contents when first entering/focusing on the control (subsequent clicks might be used to position the cursor to edit the existing text).
I added a button and tried calling SelectAll() in the button click event, but that didn't do anything either. What's going on? Is this a bug?
How can I get around this?
Steps to reproduce
Create a new Windows Form Application in .NET 4.0 in Visual
Studio 2010.
Add a TextBox, MaskedTextBox, and Button to the default form
Change the Mask property on the MaskedTextBox to "_____".
Add some event handlers:
private void maskedTextBox1_GotFocus(object sender, EventArgs e)
{
Debug.WriteLine("GotFocus");
maskedTextBox1.SelectAll();
}
private void button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Click");
maskedTextBox1.SelectAll();
}
Run the program, entered some data into the MaskedTextBox, tab through controls back to it. It selects the contents of the MaskedTextBox.
Select the other TextBox. Try clicking on MaskedTextBox. Output shows that GotFocus event was called, but text doesn't get selected.
Try clicking on button in form. Text doesn't get selected.
Tested in Visual Studio 2010 with .NET 4.0 in a Windows Forms Application project
Why this isn't a duplicate of TextBox.SelectAll() does not work with TAB
If you notice, the title says "SelectAll doesn't work with TAB". In my case, it does work with Tab, it doesn't work with the mouse - completely opposite scenario. The answer for that question is to use the GotFocus event. I'm already using the GotFocus event, but it doesn't work. That answer does not answer this question. It is clearly not a duplicate. If I'm wrong, please explain in the comments.

Your SelectAll() is being overwritten by the default functionality of the masked textbox select. I would use the Enter event, it allows for tabbed entry or mouse click entry to the masked text box. You will most likely need to use the BeginInvoke method. Try the code below. It worked for me when I tried...
private void maskedTextBox1_Enter(object sender, EventArgs e)
{
BeginInvoke((Action) delegate { SetMaskedTextBoxSelectAll((MaskedTextBox) sender); });
}
private void SetMaskedTextBoxSelectAll(MaskedTextBox txtbox)
{
txtbox.SelectAll();
}

Executing Focus before Select All worked for me:
private void Masked_Enter(object sender, EventArgs e) {
((MaskedTextBox)sender).Focus();
((MaskedTextBox)sender).SelectAll();
}

Related

Is there a way to have a textbox value show up in another textbox when a button is clicked?

I have implemented a trackbar that adjusts a textbox value accordingly with the slider as shown with the code below.
private void trackBar_Temp_Scroll(object sender, EventArgs e)
{
Oven_Temp.Text = trackBar_Temp.Value.ToString();
I am trying to have the value displayed in the oven_temp textbox appear within another textbox called feedback, when a button is clicked using this code.
private void Oven_select_Click(object sender, EventArgs e)
{
Oven_Temp.Text = Feeback.Text;
No errors seem to be appearing however no output appears within the feedback textbox.
Anyone able to help a student out for a coursework who has done a limited amount of coding before? :)
You set Oven_Temp.Text to the value of Feeback.Text. According to your text, you however wanted to set Feeback.Text to the value of Oven_Temp.Text. So you got the Assignment flipped.
Try Feeback.Text = Oven_Temp.Text;
The other common mistake is that the Event is not actually registered with the Button. You can put a simple Message Box in to test if the event even fires. That is usually one of the first things I check with Events.

C# how to make it so that a combobox doesn't highlight the text when selecting an item

I'm using a ComboBox with DropDownList property enabled in Microsoft Visual Studio. The drop down box works just fine, however when I open the form it has the text highlighted as though you do a Ctrl A on it. I would prefer it such that it didn't appear highlighted.
EDIT:
It would appear however that the cause of the problem is that my co-worker didn't instantiate the comboBox as a Drop down box as I thought he had. So in able to do that I changed the code to
stateComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
This has the desired effect. Thanks to all who had suggestions.
How about clearing the selection when the form is activated:
private void Form1_Activated(object sender, EventArgs e)
{
comboBox1.Select(0, 0);
}
Of course it's likely only because this is the default form input that it gets focused and highlighted to begin with. (TabIndex is set to 0 on this control)

C# mouse click event after focus in windows 2012

I find a strange bug in windows 2012. I have a simple window (WinForm) with a text box and a button (textBox1 and button1).And I try to focus on textbox1, after form appear.
private void Find_Paint(object sender, PaintEventArgs e)
{
textBox1.Focus();
}
And if I set it Click and MouseClick events stop working. So I can't click on button.
In windows 2008 it's work. If comment focus line - works too.
Who can suggest a solution or perhaps an alternative? Need to get the cursor in the textbox after the form has appeared
You should use the Shown event instead:
private void Find_Shown(object sender, EventArgs e){
textBox1.Focus();
}
Note: you used Paint event which will be very nasty, everytime your form is repainted, your textBox1 will be focused, the Paint event is fired every time your form resizes, state changes, ... we can't determine exactly the time it fires but it fires fairly frequently when your form is running. That is the reason why you can't click on and select anything on your form. That's because clicking or selecting controls fires the Paint event and makes your textBox1 focused then.

how to auto focus an .Net winform application running in background

I have a weird problem, my .net win form application triggers a third party CRM application. When the customer is working with CRM application and clicks some button in my application, on first click the button does not trigger the event, only on the second click it responds.
The reason I suspect is that my application is not in focus.
I tried with the following code
private void XXXXX_MouseHover(object sender, EventArgs e)
{
this.BringToFront();
this.Focus();
}
But then its not working. I am new to .Net can any one point me how to fix this issue?
Any help is highly appreciated.
update: my toolstrip button.
btnbutton.Click += delegate(object sender, EventArgs e)
{
//some code to execute on button click.
};
Thanks
The .NET tool strip ignores clicks when the application doesn't have focus. This is just like how Words work. The idea here is that the user can click "anywhere" in the window without having to worry that he actually performs an action. Only when the window has focus will the click "count".
The post at http://blogs.msdn.com/b/rickbrew/archive/2006/01/09/511003.aspx describes how you can work around this. Basically, you inherit from ToolStrip and override the WndProc, and change the WM_MOUSEACTIVATE with the MA_ACTIVATEANDEAT result to the MA_ACTIVATE result.

How to stop textbox leave event firing on form close

using c# winforms vs2008
I've got a textbox on a form with a method being called from the textBox1_Leave event. The method takes the contents of the textbox in question and populates other textboxes based on the contents.
My problem is that is the user has focus on the text box then clicks the button to close the form (calling this.close) then the form does not close because the textbox leave event gets fired.
If the textbox does not have focus on form close then the form closes fine.
If however a user closes the form by clicking the little X close icon in the top corner the it closes fine all the time with out the textbox leave event being fired.
How can I duplicate the X close functionality so that I can always close the form without the textbox leave event being fired?
The simplest solution is going to be to check which control is actually focused before doing your post-processing - but you can't do it in the Leave handler, because the focus will still be on the text box at that point.
Instead, you need to move your logic to the LostFocus event, which is not in the designer. You'll have to wire it up at runtime:
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
}
private void textBox1_LostFocus(object sender, EventArgs e)
{
if (closeButton.Focused)
return;
// Update the other text boxes here
}
}
The LostFocus event happens to fire after the new control receives focus.
Clarification - you might find that it works by putting this logic in the Leave event - if the focus is changed by the mouse. If the keyboard is used instead, you'll get the wrong behaviour. LostFocus is reliable in both cases - the focused control will always be the "new" control. This is documented on MSDN: Order of Events in Windows Forms.
Incidentally, the reason why you're not having this problem with the "red X" is that the X is not actually a control that can receive focus, it's part of the window. When the user clicks that, it's not causing the text box to lose focus, and therefore isn't causing the Leave event to fire.
Another approach:
Use the textbox's validating event instead of it's leave event, then change the button's CausesValidation property to false. You will also have to set the textbox to not cause validation in the button's click event so the validating event will not fire when the form is closing (thanks to #Powerlord for pointing this out).
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.CausesValidation = false;
this.Close();
}
You could also handle the FormClosing event and make sure the e.Cancel argument does not get set to true by the validating events on the other controls on the form. I think they will be fired off before the FormClosing event.
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = false;
return;
}
}
you can check to see which control has just got focus.
private void textBox1_Leave(object sender, EventArgs e)
{
if (btnClose.Focused)
return;
// go from here
}
Just check if the form owning the textbox is disposing? If it's getting closed, it's disposing. If it's disposing you could simply end the pesky 'leave' event without doing anything. I didn't check it and forgive me, I'm choked on a project of my own so and I was searching myself, so I don't think I'll have time for that.
private void GuiltyTextBox_Leave(object sender, EventArgs e) {
Form formOwningTheTextBox=(Form)((Control)sender).TopLevelControl;
if (formOwningTheTextBox.Disposing || formOwningTheTextBox.IsDisposed) return;
.......
}
I just believe this is going to work with minimum effort and wanted to send a quick answer before I resume searching my own answer.
Write Following line of code in text box leave event on top
if me.closing then
return
end if

Categories