Cannot set focus on Text Box - c#

I have a form with 4 textboxes. This form is viewed inside a split container panel.
I can enter the values inside the text box but after the value, I click on the text I entered to modify a value but it is not letting me click, there is no cursor coming on the text box, the only thing I can do is backspace or select all and delete.
I cant click anywhere on the middle of the text entered.
If I check the focus I see it is false.
Can someone tell me what could be the problem and how to set the focus to the text box?
To show the form on the panel this is what I am doing
splitContainerControl1.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Both;
splitContainerControl1.Panel2.Controls.Clear();
myform.TopLevel = false;
myform.FormBorderStyle = FormBorderStyle.SizableToolWindow;
splitContainerControl1.Panel2.Controls.Add(myform);
myform.Show();
I have tried the below, but not working, I see focus still false.
myform.Focus();
myform.textbox_latitude.Select();
Thanks,

Try using this.ActiveControl:
this.ActiveControl = textbox_latitude;
Or if you want to focus it on the child form:
myform.ActiveControl = textbox_latitude;

Related

Set TextBox enabled to input without Keyboard.Focus() method

I have a small window in WPF app, it's contain TextBox and Button. When user click another button I need to make TextBox enabled to input text, and besides that I need to set KeyBoard.Focus() to the Button. Something like that:
private void Show()
{
textBox.Focus();
KeyBoard.Focus(button);
}
However, its didn't work. Can you help me?
You can't have more than one item have keyboard focus. If you want the user to be able to press Enter after entering text, set IsDefault to true on the second button.

Allow button click even if validation on another control fails

I have an interesting problem that I don't know how to solve. I wrote a form which does a password change. The form displays the current password too in a read-only TextBox (not for validation purposes; this isn't important in this case.) Each password TextBox has a button in it that when clicked, masks or unmasks the password (replacing the password characters with bullets and vice versa.) Here's an image of the whole thing:
Notice how the mask/unmask buttons are inside the text boxes, not outside of them. The buttons have been placed inside the text boxes with:
var button = new Button();
button.Width = 20;
button.Cursor = Cursors.Default;
button.FlatStyle = FlatStyle.Flat;
button.Image = SystemIcons.Shield.ToBitmap();
button.Dock = DockStyle.Right;
button.CausesValidation = false;
textBox.Controls.Add(button);
The last TextBox has validation enabled. Now the problem is, that the user is unable to click the passwork unmask button on the other text box, because the validation event fails. Thus, the user is unable to see the current password without entering a new one.
I need a way to have the password mask/unmask button be clickable even if validation is failing in the text box. I can't think of anything. Moving those buttons outside the text boxes is not an option.
The unmask buttons themselves, as well as the parents of the text boxes, all have CausesValidation set to false. Only the text boxes themselves have it set to true.
This is a .NET 2.0 C# project in Visual Studio 2010.
It's not easy to do it with the Validating event of the text box. The click event will not even reach the button when the text box loses focus. I'm thinking that you could either create your own TextBox (by extending TextBox or TextBoxBase) and hack the validation behavior there or override the form's DefWndProc and catch the mouse events + associated info (coordinates) there and still dispatch them (could get ugly) when validation fails.
One easy way out is to not rely on the Validating event anymore. Instead do the validation in the Leave event of the text box and if it fails just mark the text box as such. The user will still see there is a problem.
One more thing you need to take care of is the OK button of the dialog. You need to make sure the user won't be able to close the dialog if there are unvalidated controls on the form. Since you don't have validation support anymore, maybe you can use the Tag property to store a False (for example) when the data is not valid. On OK just iterate over all textboxes and check their tags.
The behavior for the end user will ultimately be the same, you just have to write a bit more code.
Well, I found a way where I can keep using the Validating event and still be able to click the buttons in the other text boxes. It turns out that the Button.MouseUp event is sent even if a button doesn't have focus.
So the solution is to handle both the MouseUp as well as the Click event and perform the password masking/unmasking depending on whether the text box that contains the button currently has keyboard focus or not:
button.MouseUp += (sender, e) =>
{
if (button.Parent.ContainsFocus || e.Button != MouseButtons.Left
|| !button.ClientRectangle.Contains(e.Location))
{
return;
}
textBox.UseSystemPasswordChar = !textBox.UseSystemPasswordChar;
};
button.Click += delegate
{
if (!button.Parent.ContainsFocus) {
return;
}
textBox.UseSystemPasswordChar = !textBox.UseSystemPasswordChar;
};
We do want to handle both events, because if we only handle MouseUp, then clicking the buttons with the keyboard (Tab to switch to the button, and Enter or Space to click it) would not work anymore.

How to set cursor position in textBox on button click event using c#?

I have two text boxes and a button in windows form app using c#.
when i enter text in txtbox1 and send that text to txtbox2 on button click event, the cursor also goes to txtbox2.
My requirement is when i send data from textBox1 to textBox2 on buton click event the cursor should remain in textBox1.
In the buttonClick event just add at the end:
textBox1.Focus();
How to: Set Focus in a TextBox Control
Any reasons why dont set cursor position manually after the click event?
Cursor.Position = new Point(MyFirstTextbox.Location.X -5, MyFirstTextbox.Location.Y -5);
Also Control.Focus() helps you.
On button click write at last:
textbox1.focus()
focus is the method you need to use.

How to Use The Form Opened When Using ShowDialog()?

Currently I am working with the ShowDialog() method and trying to figure out how it is supposed to work. I have a form, testDialog, that has a text box that will take an input string. I followed the code on the MSDN page as follows:
string Range;
testDialog specRange = new testDialog();
if (specRange.ShowDialog(this) == DialogResult.OK)
{
Range = specRange.txtPageRange.Text;
}
else
{
Range = "";
}
specRange.Dispose();
The thing I can't find any information on and that I can't figure out is, how do I enter the text and get it to submit? I put buttons on the form but they didn't show up when I ran the program. I enter the text into the text box but I can't hit enter or anything, my only option is to close the form.
Is there something I'm missing that I need to add so that I can hit enter or click an Okay button after entering the text?
From msdn :
The dialog box can be assigned one of the values of the DialogResult enumeration by assigning it to the DialogResult property of a Button on the form or by setting the DialogResult property of the form in code. This value is then returned by this method. You can use this return value to determine how to process the actions that occurred in the dialog box. For example, if the dialog box was closed and returned the DialogResult.Cancel value through this method, you could prevent code following the call to ShowDialog from executing.
The simplest method to do that is to add a Button "Ok" to your testDialog and change its property DialogResult to Ok. So when you click on it, it will return DialogResult.ok and you will enter your if.

How to remove waiting cursor from textBox?

Whenever I load windows form with text box, I'm get waiting cursor blinking in first textbox, but I waiting to blink when I click on that textbox.
This is how textbox code is looking like:
this.textBox1.Location = new System.Drawing.Point(166, 51);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(122, 20);
this.textBox1.TabIndex = 3;
Could anyone please tell me how I make it possible?
You can set the focus to your first input field(textbox) by using TextBox1.Focus();
If you don't want any other textbox to be focused, you need to add an element that has Focus method implemented and use its Focus like a picture box. The easiest would be to add an invisible textbox and set the focus to it.
This is probably because the textbox is the first control to get focus.
Do you wish to have the focus on a different control, or not have the text box focussed?

Categories

Resources