How to make textbox change event to hold until length is 10 - c#

What condition is satisfied when the amount of text in the text box is equal to 10? Currently, the event is firing on every keystroke.
private void textBox1_TextChanged(object sender, EventArgs e)
{
if(textbox1.Length == 10)
{
}
}

I tried this and it is not working because this event is triggered each time I press a button on the keyboard.
That doesn't matter. Your code wouldn't even compile as TextBox has no property Length, though it does have a Text property, which is a string, which has a Length property.
Be aware that there are other ways to change the text (i.e., pasting text in) which will fire TextChanged only once. Also be careful as you may not want to trigger that code if the user presses the backspace or delete keys. Performing actions in textboxes like this is often a bit trickier than one would expect.

You're 99% there, you're missing on thing, the Text property.
if(textbox1.Text.Length == 10)
{
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.TextLength==10)
{
//read DB and display stuff
textBox1.Clear();
}
}
And just like other answerers I'm curious since your current code won't even compile. And if you write it in VS 2010/2012, the autocomplete should already offered the TextLength

Related

Repeat same steps on every textChanged event

I have in my project 5 text boxes.
Every TextBox should accept only digits.
For that I created a function which takes not prepared text and returns the proper one.
Now I'm wondering if there is any simpler way to perform this action on every TextBox, on every TextChanged event without repeating almost same code?
private void TextGoldPack_TextChanged(object sender, EventArgs e)
{
(sender as TextBox).Text = Only_digits((sender as TextBox).Text);
}
private void TextGoldTake_TextChanged(object sender, EventArgs e)
{
//repeat here and on every _TextChanged event
}
If I'm understanding you correctly, just because it's named TextGoldTake_TextChanged, doesn't mean that's the only textbox that can use that code. On the events tab, you can set the TextChanged function for all you textboxes to lead to that function. If it helps, rename it something that doesn't sound textbox-specific such as TextChanged.
Change all the TextBoxes to refer this method upon TextChanged.
Use the sender property to get the actual caller TextBox.

How may I automatically change focus from textBox1 to textBox2 after populating textBox1?

I've created a stock locater/mover program. The scenario sequence is as follows:
First, the user will scan a QR code into textBox1 and then scan a location into textBox2.
Next, an update statement will execute resulting in 'moving' the stock's location.
How do I auto move the cursor into textBox2 after textBox1 has been populated with a QR Code?
Please note the QR codes vary in length. This prevents me from using textBox max length. I've currently tried the following:
private void textBox1_TextChanged(object sender, EventArgs e)
{
//part number textbox
var partNumber = textBox1.Text;
partNumber = partNumber.TrimEnd('\r', '\n');
if (textBox1.Text!=null)
{
textBox1.Select();
}
else
{
textBox2.Select();
}
}
Using the aforementioned code, the first character of the QR code is input into textBox1 and the remaining characters are input into textBox2. The desire is to have all the QR code characters in textBox1 and then have the cursor change focus to textBox2.
Solution;
private void textBox1_KeyPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
textBox2.Select();
}
}
then went to the properties of textBox1 and set the 'KeyDown' to textBox1_KeyPress under 'Events' and it works.
A similar issue has been described in another question here on SE.
The following answer, posted by esskar might be of interest to you.
Since the scanner will probably take only an instant to scan the QR code into the TextBox, you might use the technique in that answer to start a timer when TextChanged fires. I'd set the interval of the Timer at around 500ms, that should be enough, and once it fires you can be pretty sure that the QR code is inside the TextBox.
Obviously this isn't a perfect solution, since in some cases the scanner might lag or simply be unable to deliver the QR code in that time-frame for whatever reason.
You will need to implement a check for common QR code length and validate the code before you go on.
To be clear: There's no need to create a new type of TextBox.
Just start a Timer when TextChanged is fired.

When combobox changes do something ( C# )

I've been trying to make a program with 3 comboboxes where depending on what you pick different things happen.
Here is a screenshot of what I'm stuck with.
The only thing missing in the screenshot is the following which is in the private void Form1_Load event
cBxColor1.Items.Add("Black");
cBxColor2.Items.Add("Black");
cBxTest.Items.Add("Something");
In the screenshot above I try two methods to write something in the textbox. One whenever the text changes and then checks for the choosen item. In this case Something, Black and Black. I'm planning on adding more later but so far I'm trying to get this to work with one.
The original plan was to have while(the selected texts in the comboboxes are Something, Black and Black) then add some text to the textbox if that is true.
Screenshot of the error I get when trying the other method, I'm not sure what this means.
I've googled and searched around for a solution but I truly couldn't find anything that would help to solve my problem. I would appreciate if the 1337 hax0rz on here would help me out.
TextChanged is a Event. Use it in a methode like this:
private void ComboBox_TextUpdate(Object sender, EventArgs e)
{
//Your code here
MessageBox.Show("You are in the ComboBox.TextUpdate event.");
}
Add the event with += to your combobox in your initialisation:
ComboBox.TextUpdate += ComboBox_TextUpdate;
So at every TextUpdate your Methode ComboBox_TextUpdate gets called and you can code there.
Instead of using the if condition to see if the text has changed, you should use the ComboBox event SelectedValueChanged.
To create that event right-click on your ComboBox and select properties. Select "Events" and double-click the textbox next to the SelectedValueChanged event.
Then you want to check the values of each ComboBox like you did.
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (cBxColor1.SelectedText.Equals("Black") || cBxColor2.SelectedText.Equals("Black") || cBxTest.SelectedText.Equals("Something"))
{
tbxTest.Text = "TEST";
}
}
Also, that while statement is almost a death threat because once it enters that condition, it will not leave.
You won't be able to change the ComboBox value due to the while being executed.

C# and backspace detection in textbox in order to reload listbox

As part of this assignment for my class, in which I have a listbox which is connected to a database and it displays the first and last name of the students. I have also created a search feature in which the user types in the first and/or last name they are looking for in a textbox and when they press the "search" button it displays the filtered results appear in the listbox.
The last part of the question asks me to detect when the user clears the textbox, to once again display the original data in the listbox. I have the data in a method called databaseload()and so it is really down to how to I get my program to detect that the listbox is once again empty.
I found a couple of things online, and when I tried them, it didn't work.
private void searchTextBox_KeyPress(object sender, KeyPressEventArgs e){
if (e.KeyChar == 8)
{
databaseload();
}
}
and I have also tried KeyDown
I also don't want it to reload when first backspace is detected. I want it to reload the listbox when the searchTextBox has nothing in it.
Your help is greatly appreciated.
You should handle the KeyUp event, which fires after the key has been entered into the text.
There is a TextChanged event which seems more appropriate than manually detecting backspaces. The delete key can remove all the text as well, or someone might just highlight all of the text and cut it to the Clipboard.
Example of using the TextChanged event:
private void textBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(((TextBox)sender).Text))
{
// reload database
}
}

Navigation in Windows Forms using enter key

I have textboxes in my Windows Forms application, and I want that when the user presses the ENTER key then the cursor goes to the next textbox.
How do I do this?
Is this a good habit or shall I avoid it? Actually the users are very much prone and have adapted and have become habitual of pressing ENTER key for navigation between textboxes and buttons. So, for them I need to do this.
Please help me with the complete code using two text-boxes as an example.
I would say the nicest way is to create a user control that inherits from TextBox and then override the OnKeyPress method to capture enter and send a tab. Focus will then be given to the next TabIndex on the form, just as though a tab had actually been entered.
The code below does exactly that:
public partial class CustomTextbox : TextBox
{
public CustomTextbox()
{
InitializeComponent();
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
e.Handled = true;
SendKeys.Send("{TAB}");
}
}
}
You could also put similar code in the KeyPress event handlers for your controls but this saves a lot of duplicate code and unnececessary event handler.
As for whether this is good practice - I would say in general, no, changing the default behaviour of forms is never a good idea, but of course, if this is what your users want and expect, then it is their decision.
This is a bad idea. The standard UI is for TAB to move between input fields. You make your app less useable when you elect not to follow well known standards. These standards are what makes UI intuitive.
Yes, it depends on you
For a sample, you can place 4 textbox on the form and use the following code
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
textBox2.Focus();
}
}
private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
textBox3.Focus();
}
}
private void textBox3_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
textBox4.Focus();
}
}
It might help you.
Happy coding...
For example you could trap OnKeyUp event, check if it is RETURN and process source control to use Focus() to next control...
Yes you can fire the KeyDown or KeyUp event on a TextBox. To check whether it was the enter key, you can do the following:
//e is the KeyEventArgs from the event.
e.KeyCode == Keys.Enter
Then, if he has pressed the enter key, you can do:
System.Windows.Forms.Control.SelectNextControl();
To set the order of your controls, in Visual Studio look for this little icon:
http://i.stack.imgur.com/nZWLO.png
Click it, and you'll go into tab ordering mode, as I like to call it. Just click the controls in the order you wish them to be and after you're done, click the little icon again. Presto!
Now whether that is a good idea of not, completely depends on how used to it your end users are. If they have always used it like this, and you give them something that doesn't fit into their mind model, they are going to say your software is broken.
Always always always try to emulate what processes the user already has in place in their head.
Read this if you have the time, it's a really light and very good read:
http://www.joelonsoftware.com/uibook/fog0000000249.html
I think the best way would be to:
1) assign each textbox's TabIndex attribute incrementally (first is x, next is x+1 etc).
2) capture on the OnKeyUp event on the whole form, check the argument to see if the key was RETURN
3) focus the next textbox using its TabIndex. Or simulate the TAB key.
This code should work:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
GetNextControl((TextBox)sender, true);
}
}
This way you end up writing only one function, and you can have as many textboxes as you want.

Categories