TextBox.Text without the suffix of the appended part of Autocomplete - c#

Is there a way to get the Text in a TextBox that has an Autocomplete that appends text, but without the part that is appended? I want to use this text in the Textbox_TextChanged-event. This code doesn't work
private void Textbox_TextChanged(object sender, EventArgs e)
{
var t = textbox.Text.Substring(0, textbox.SelectionStart);
}
It seems the TextChanged-event fires when the autocomplete-text is appended, but the selection of the appended text is applied after that.
E.g. if the user types in ho and the autocomplete has an entry house, the textbox contains the text house, but the use is selected and can be overwritten when the user continues typing. I want to get the ho-part, because that is the text the user has typed in, without the use-part, which doesn't come from the user.

Question is some what confusing, I have provide an answer as I understood it.
Text change event fires when you change text in the textbox. You can use suggest instead of appending it
Assume that textbox name is "Textbox1"
private void Textbox1_TextChanged(object sender, EventArgs e)
{
this.Textbox1.AutoCompleteMode =
System.Windows.Forms.AutoCompleteMode.Suggest;
var t = Textbox1.Text;
}

I solved it with a delay, so the textbox has time to apply the selection. This seems to work so far, but could be an issue if the applying of the selection takes longer than the wait-time.
private async void Textbox_TextChanged(object sender, EventArgs e)
{
await Task.Delay(200);
var t = textbox.Text.Substring(0, textbox.SelectionStart);
}

Related

How to identify whether the text changed in textbox either by programmatically or by user

We are using a TextBox to display input received from the I/O system. If the user enters some data on the text box, the value will be written to the IO system.
We are using the OnTextChanged event to write the data entered by the user to the IO system.
The problem is that we get this event when we update the value received from the IO system to the text box (from the code).
Is it possible to know whether the value of the TextBox is changed by the user or by using the code?
You could set a Boolean variable named IsUserInput, after the I/O system sends data to the text box, you need to set IsUserInput to false.
In the KeyDown event of the TextBox, the variable can be set to true. Finally, you could use this variable to determine the text inputter in the textChanged event.
Please refer to the following code.
public Boolean IsUserInput;
private void Button_Click(object sender, RoutedEventArgs e)
{
IsUserInput = false;
myTextBox.Text = "hellohello";
}
private void myTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (IsUserInput)
{
//write data to I/O sytem
}
}
private void myTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
IsUserInput = true;
}

SelectAll not working in Dialog WPF

I'm not sure what's going wrong but I can't select the Text of my TextBox in a dialog.
I added a FirstTimeLoadedHandler to Loaded in my view where I select the text:
public ParticipantView()
{
InitializeComponent();
Loaded += FirstTimeLoadedHandler;
}
private void FirstTimeLoadedHandler(object sender, RoutedEventArgs e)
{
SurnameBox.Focus();
Keyboard.Focus(SurnameBox);
SurnameBox.SelectAll();
}
However my textbox is in Keyboard-Focus, but not selected at all. I'm not sure why it's not working.
I thought it has something to do with my databinding but the data should be received before Loaded fires or am I wrong?
Your code would work fine with two small changes. The first is that obviously, selecting all on a TextBox that has no text in it will have no effect. Secondly, if you focus it after calling SelectAll, you'll have more luck. Try this:
private void FirstTimeLoadedHandler(object sender, RoutedEventArgs e)
{
SurnameBox.Text = "This text is selected";
SurnameBox.SelectAll();
Keyboard.Focus(SurnameBox);
}

Text added instead of being set in .NET TextBox

This is my LostFocus event handler:
private void txtThrow_LostFocus(object sender, System.EventArgs e)
{
TextBox source = (TextBox)sender;
if (source.Text == "")
source.Text = "0";
}
This actually interferes with txtThrow_KeyPress, so that after I do my processing on my TextBox which accepts to only hold one character, I find it having two: mine and this zero you see here!! What I want to do is to keep txtThrow_KeyPress exactly as it is, but whenever the user types nothing, I want to enforce a zero.
What I can understand from here is that txtThrow_LostFocus is triggered before txtThrow_KeyPress is done with its job, since at the time txtThrow_LostFocus is triggered, the text is still empty. How can that be correct?!
I would recommend using the TextChanged event instead of the KeyPress event. I am assuming the text box could be empty already, and the user presses Backspace or something of the nature.
TextChanged event is triggered every time the text is changed within the field.
And the code you have should work perfectly fine when you fill the new event handler method with such. However, you may need to use
private void txtThrow_LostFocus_TextChanged(object sender, System.EventArgs e)
{
TextBox source = (TextBox)sender;
if (source.Text == "" || source.Text == null)
source.Text = "0"
}
Hope this helps!
Try to this code
private void txtThrow_LostFocus_TextChanged(object sender, System.EventArgs e)
{
TextBox source = (TextBox)sender;
if (string.nullorempty(source.text)
source.Text = "0"
}

Passing a textbox to a void

I'm not sure if I'm using the wrong terminology so that may be why my searching has not turned up anything.
I have a bunch of text boxes that I want to validate and check that they don’t contain apostrophes. The code that I have is:
public void apostropheCheck(TextBox fieldName)
{
Match m = Regex.Match(fieldName.Text, #"'");
if (m.Success)
{
validationErrorProvider.SetError(fieldName, "Field can not contain apostrophes");
}
else if (!m.Success)
{
validationErrorProvider.SetError(fieldName, "");
}
}
and the validation on the textbox is:
private void FirstNameTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
//Checks for apostrophes
apostropheCheck(FirstNameTextBox);
}
However when I run this the value that gets passed to the void is the text that is in the text box (e.g ‘John’ or ‘Mary’) I could get this to work just using the code that’s in the void for each validation event but that would be repeating myself a lot. Is there a better way?
You can have one common handler and use that for all of the textboxes' Validating event.
private void CommonTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
//Checks for apostrophes
apostropheCheck((TextBox)sender);
}
The sender object of the event is a TextBox, so you can cast it to a text box and repeat the same event handler for all of the text boxes in your application
private void FirstNameTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
apostropheCheck(((TextBox)sender).Text);
}
Terminology: you are referring to your function apostropheCheck as the void which is its return type the standard way is to use the function name apostropheCheck.
All of you text boxes can be validated using the same function if you replace the name of the text box in the code with sender ex.
private void FirstNameTextBox_Validating(object sender,System.ComponentModel.CancelEventArgs e)
{
//Checks for apostrophes
apostropheCheck((TextBox)sender);
}

Dropped down in combo after focused

I have a combo box. It must display its content, when focused and its value changed as well.
I wrote this code in its Value Change event:
if(combo1.Focused)
combo1.DroppedDown=true;
But it doesn't work!
what's your solution?
What Event handler are you putting that code in? Assuming that you want to show the drop down when the user types in the edit box part of the combo just handle the TextChanged event and put that code inside there and it should work.
If I understand your requirement correctly, when the combobox gets focus you want the drop down list to show. That can be achieved as follows
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.GotFocus += new EventHandler(comboBox1_GotFocus);
}
void comboBox1_GotFocus(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
}

Categories

Resources