Text in textbox is selected - how to change? - c#

I created a simple form in C# with only a textbox and a button.
The form contains a function to set the text in the textbox and another function that closes the form if the button is clicked.
Now I execute the form, set the text and display the form.
Everything is fine, but: The text in the textbox is "selected / marked".
What can I do that the text is not selected and the focus is on the button?
(button.focus is not working)
Thanks for help

You can manually set the selection after changing the content, for example like this:
this.textBox1.SelectionStart = this.textBox1.Text.Length;
Where textBox1 is the textbox you're working with. This clears the previous selection and makes a new one, effectively setting the cursor inside the textbox to the last element of it's content.
Alternatively, select the button instead of focussing it, like this:
this.button1.Select();
Where button1 is the button you want to select after changing the textbox' content.

Solution:
I changed the TabIndex from textbox from 0 to 1 and
have tabindex 0 to the button.
Then text is no longer marked.

Related

How do I stop the text in a Winforms textbox from being automatically selected/highlighted when first displayed?

In the case where I don't want a user to inadvertently delete text displayed in a textbox when the textbox is first loaded and displayed, e.g., by inadvertently fumble-fingering on the keyboard, how do I stop the text in the textbox from being automatically selected when first displayed and before the user has access to it?
Make the tabstop property of textbox false for which you want to disable the highlight do -
textBox1.TabStop = false;
This will stop highlighting the text of your textbox.
//set SelectionStart property to zero
//This clears the selection and sets the cursor to the left of the 1st character in the textbox
textBox1.SelectionStart = 0;
//This clears the selection and sets the cursor to the end of whatever is in the textbox
textBox1.SelectionStart = textBox1.Text.Length;

Focus does not move to next new row in grid view when press tab key

I have designed a wpf page. But I not able to set Proper Tab Navigation on Grid view. Controls (grid view) on the page are not following tab index. Page contain Grid and Save,Cancel button.
There is a gridview. This grid has rows and columns. Each row contains 2 autocompletebox and 6 textboxes. When first i enter the value on the first autocompletebox,then enter tab it move to next box and so on. I enter the value in last text box and press enter button, then a new row will be formed in the grid. Then i press the tab it focus move on the outside button(Save button). I want to move the focus on the next box( first autocomplete box,not on the save button) in the second row in the grid.Pls help...
Have you tried to set/ correct the Control.TabIndex properties of the freshly added item?
See MSDN - Control.TabIndex Property .
Maybe you like to do this using the AddingNewItem event in order to manipulate added items before they are displayed.
See MSDN - DataGrid.AddingNewItem Event.

When auto filling the textbox using javascript mvc still shows required red icon

Hi have two textboxes and a checkbox with an ok button.
When I enter name in first textbox and press ok, it shows second textbox is required.
And when I click the checkbox, it auto fills the same name from first textbox to second.
But it still shows the required red tag on second textbox.
Currently I am using jquery to fill the data into second textbox.
If I fill the second textbox manually, the red required sign goes away.
Any idea?
Thanks
Assuming your first input has an ID of firstInput and your second input an ID of secondInput and your checkbox has an ID of myCheckbox you can use:
$('#myCheckbox').click(function () {
//Toggle the value of the input
$('#secondInput').val( $(this).is(':checked')? $('#firstInput').val(): '');
//Fire validation ONLY on the secondInput field
$('#secondInput').valid();
//OR Fire validation on the entire form
$(this).parents('form').valid();
});
You might be able to do something like this:
$("#myCheckbox").click(function(){
if (this.checked){
$("#input2").val($("#input1").val());
$("#input1").triggerHandler("keyup"));
//OR
$("#input1").triggerHandler("focus"));
}
});

Change label to textbox for edit

Have a look at this example first,
click here. I want you to see only the example of how he changed a label into text box after he hits edit.I want to make edit profile page but I don't want to use bind grid view. Is there any possible ways to retrieve some data from table in database to be shown in labels then when uset hits the edit button, the label will change to textbox?
why do you need to show a label ?
you can show it in a text box and set IsReadOnly or ReadOnly to true, set style to flat and remove border(it'll appear like a label), and then on the Edit Action, you can change this ReadOnly/Editable property.

C# Button Text Condition

Using a WinApp form in c#, and many buttons here...
I want to create a condition, that if a button has text in it, then the background color of that button changes. That sounds easy enough to do. But what I have is a common set of buttons, that have text in them dependant of values in a XML document.
Example: Week 1 - Buttons 1, 3 and 5 have text in them. Week 2 - Buttons 2 and 3 have text.
How can I setup a seperate condition to check if the button has text in it or not, and then change the color if there is a text value in the button.
Thank you.
I would extend button and override the label setter such that it also changes the color when setting the contents of the label to some non-empty value.
Do you want something like this?
foreach (var btn in this.Controls.OfType<Button>()) {
btn.BackColor = (string.IsNullOrEmpty(btn.Text))
? SystemColors.ButtonFace : Color.AliceBlue;
}
I would put it in a method, and call it on form load, or whenever the buttons' texts changes.

Categories