How to remove waiting cursor from textBox? - c#

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?

Related

Cannot set focus on Text Box

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;

Any click after clicking my richTextBox changing the size back to normal

When I click in my richTextBox, I run this code to make my textbox bigger for the user:
this.richTextBox1.Size = new System.Drawing.Size(500, 100);
Now I want any click after clicking my richTextBox to change the size back to the original:
this.richTextBox1.Size = new System.Drawing.Size(477, 26);
I can use the code above for each element mouse click event, but I am wondering if there is an easier way.
Just write the first code in richtextbox.enter event. And the second in richtextbox.leave event. Otherwise MouseClick is not a bad idea

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.

Form control selection

How to change order of control selection in C# Window form. I wanted to change where text box appears in the form, but now when I use Tab key it skips that text box an later goes to it. I've worked with WPF and I would just change control position in XAML, but I can't do it in Win form.
Here is a simple example that adds a button and sets its TabIndex property
// Create a button and add it to the form.
Button button1 = new Button();
// Anchor the button to the bottom right corner of the form
button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
// Assign a background image.
button1.BackgroundImage = imageList1.Images[0];
// Specify the layout style of the background image. Tile is the default.
button1.BackgroundImageLayout = ImageLayout.Center;
// Make the button the same size as the image.
button1.Size = button1.BackgroundImage.Size;
// Set the button's TabIndex and TabStop properties.
button1.TabIndex = 1;
button1.TabStop = true;
// Add a delegate to handle the Click event.
button1.Click += new System.EventHandler(this.button1_Click);
// Add the button to the form.
this.Controls.Add(button1);
Alternatively you can also take a look at this (To set from Design)
Hope it helps

Categories

Resources