How to get the NEW text in TextChanged? - c#

In a TextBox I'm monitoring the text changes. I need to check the text before doing some stuff. But I can only check the old text in the moment. How can I get the new Text ?
private void textChanged(object sender, EventArgs e)
{
// need to check the new text
}
I know .NET Framework 4.5 has the new TextChangedEventArgs class but I have to use .NET Framework 2.0.

Getting the NEW value
You can just use the Text property of the TextBox. If this event is used for multiple text boxes then you will want to use the sender parameter to get the correct TextBox control, like so...
private void textChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if(textBox != null)
{
string theText = textBox.Text;
}
}
Getting the OLD value
For those looking to get the old value, you will need to keep track of that yourself. I would suggest a simple variable that starts out as empty, and changes at the end of each event:
string oldValue = "";
private void textChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if(textBox != null)
{
string theText = textBox.Text;
// Do something with OLD value here.
// Finally, update the old value ready for next time.
oldValue = theText;
}
}
You could create your own TextBox control that inherits from the built-in one, and adds this additional functionality, if you plan to use this a lot.

Have a look at the textbox events such as KeyUp, KeyPress etc. For example:
private void textbox_KeyUp(object sender, KeyEventArgs e)
{
// Do whatever you need.
}
Maybe these can help you achieve what you're looking for.

Even with the older .net fw 2.0 you should still have the new and old value in the eventArgs if not in the textbox.text property itself since the event is fired after and not during the text changing.
If you want to do stuff while the text is being changed then try the KeyUp event rather then the Changed.

private void stIDTextBox_TextChanged(object sender, EventArgs e)
{
if (stIDTextBox.TextLength == 6)
{
studentId = stIDTextBox.Text; // Here studentId is a variable.
// this process is used to read textbox value automatically.
// In this case I can read textbox until the char or digit equal to 6.
}
}

Related

ComboBox SelectedIndexChanged event: why the SelectedText propery is not changed? [duplicate]

This question already has answers here:
ComboBox.SelectedText doesn't give me the SelectedText
(11 answers)
Closed 6 years ago.
I would like to get the text of the selected item in combobox whenever the selection is changed.
I therefore use the SelectedIndexChanged event, but the combobox text does not changed. it remains empty.
private void myCombobox_SelectedIndexChanged(object sender, EventArgs e)
{
string myTxt = myCombobox.SelectedText; //myTxt is null.
}
Just when I select twice the same item, the text is changed accordingly.
Should I use another event?
Any ideas?
If you are looking for the text that is in the combobox after it is selected then you would want to do something like this:
private void myCombobox_SelectedIndexChanged(object sender, EventArgs e)
{
string myTxt = myCombobox.Text;
}
This will take all the text from the combobox, don't forget to look at your Delegate in the Designer to ensure this actually occurs once the Combobox is changed
If you indeed need the ComboBox.SelectedText (I also suggest you carefully read the description for this property before deciding https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedtext(v=vs.110).aspx):
private void myCombobox_SelectedIndexChanged(object sender, EventArgs e)
{
var originalValue = myCombobox.SelectedText;
var tempCb = sender as ComboBox;
if(tempCB != null)
{
var newValue = tempCb.SelectedText;
}
}
The reason for getting Null value is because you are using the 'SelectedText' property. In order to Get the current value You have to use the Text property
private void myCombobox_SelectedIndexChanged(object sender, EventArgs e)
{
string cmbTextValue = this.myCombobox.text;
}
Hopes this will solve the problem :)
If you want the Text of a selected index you have to use the .Text property, not SelectedText.
For after the value is selected use the SelectionChangeCommited Event.
Try this:
private void myCombobox_SelectionChangeCommited(object sender, EventArgs e)
{
string myTxt = myCombobox.Text;
}
You can also test SelectedItem as well, not sure if that will solve a null value.
string myTxt = myCombobox.SelectedItem.Text.ToString()
But I think the latter would be used more for conversion issues. Try both, let me know how it works out.

TextBox c# refer string

Is it possible to like refer a string as content for a TextBox in c#? I have a listbox with a bunch of objects in it. And each object contain a string. And when I select an object in the listbox I want its string to be the content in the TextBox, so that whatever I write gets saved to the string.
Like for example in Java you could have a PlainDocument in an object, and whenever you select a different object in a JList you could set the document in a JTextField to the objects PlainDocument.
You can either use Data Binding for an automated solution or you can manually listen for SelectedIndexChanged event of the list box and set the Text property in the event handler.
listBox1.SelectedIndexChanged += (o, e) => {
object selectedItem = listBox1.SelectedItem;
textBox1.Text = selectedItem != null ? selectedItem.ToString() : null;
};
The content of the textbox can be accessed using
myTextBox.Text
This property expects string, so your answer is Yes. I think simply assigning this property would do.
UPDATE
I think you need something like this(assuming you are using WinForms):
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listBox1.SelectedItem != null)
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
int index = listBox1.Items.IndexOf(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
listBox1.Items.Insert(index, textBox1.Text);
}
Though there is an action for TextChanged event of textbox in WinForms, but changing listbox from there is a bit tricky (ends up calling each other infinitely) as we are already changing textbox from the change event of listbox.
Adding a button to do this simplifies it a lot.

Using a generic clear textbox method on event

I've got a program with a lot of text boxes that I've got text in that I want to be cleared on _click and then reset to default if nothing is entered and the user clicks away.
The way I was going to do it is clearly inefficient, having to name the text box each time and I'd like to know how I could go about streamlining it.
this is what I've got at the minute, and I'd have to change the txtUserName for the text box field name each time
private void txtUserName_Click(object sender, EventArgs e)
{
txtUserName.Text = ""
txtUserName.ForeColor = Color.Black;
}
is there a way I can do essentially
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
string caller = //Get this textbox name
this.ClearBoxes(caller)
}
void ClearBoxes(string Caller)
{
Caller.txt.Text = "";
//..... and so on
}
Yes, you can try this (though it's not generic but there is no need for generics in this case):
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
if(tb != null) tb.Text = "";
}
And you can attach this method to all your textBoxes Click event.
textBox1.Click += txtAnyTextBox_Click;
textBox2.Click += txtAnyTextBox_Click;
I don't think this is gonna work:
void ClearBoxes(string Caller)
{
Caller.txt.Text = "";
//..... and so on
}
If you want to use ClearBoxes method you should pass it your TextBox element.But there is no need for this,you can directly clear your textBox as shown above code.
Also if you want to clear all TextBoxes in the same time,for example one button click you can use this:
private void button1_Click(object sender, EventArgs e)
{
foreach (var tBox in this.Controls.OfType<TextBox>())
{
tBox.Text = "";
}
}
You can use the sender argument for that.
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
var textbox = sender as TextBox;
this.ClearTextbox(textbox)
}
private void ClearTextbox(TextBox textbox)
{
textbox.Text = "";
//...
}
You can get name of textbox from sender of event:
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
string caller = textBox.Name;
this.ClearBoxes(caller); // call your custom method
}
If you want to simply clear textbox text, then you don't need to get its name - you can use it's Clear() method:
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.Clear();
}
Also you can consider creation of custom textbox, which will have some default value and will resent itself to default when clicked:
public class CustomTextBox : TextBox
{
public string DefaultText { get; set; }
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
Text = DefaultText;
}
}
Use custom textboxes instead of default textboxes, and provide DefaultText value for each custom textbox which should reset itself to something more meaningful than empty string (you can use Properties window for that).
This would be quite nasty - as you'd cause a page reload every time someone clicked in the text box.
A far simpler way would be to do it in javascript.
just add a function to clear the text box, and then maybe use a css selector to enable the function for every text box you want to use it in.
e.g.
<input type="text" class="clearme" />
$(".clearme").click(function() {
$(this).val('');
});
this will do it all client side without causing any post backs.

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"
}

RichTextBox last change check

How can i know the last change that the user did in the text in RichTextBox?
Is there a function?
Or I need to build to it a function?
Add an event to your textbox. Select your textbox, go to properties, in event tab you will see different events. Find TextChanged event. Add that event to your code.
This function will trigger everytime textbox is changed. You can write your logic in that function.
It provides an event called TextChanged. You'll need to define an EventHandler and specify what is to happen, when the text changes.
For example:
private void rtb_TextChanged(object sender, EventArgs e)
{
char c = rtb.Text.ElementAt(rtb.Text.Length);
if(c == mystring.ElementAt(mystring.Length))
//is equal
mystring = rtb.Text; //save string for next comparison
}
and now you need to add the eventhandler to the event like
rtb.TextChanged += rtb_TextChanged;
Look here for documentation.
UpdatedChar would contain the newly added character to the textbox1
OldValue would always contain the Old Value in the textbox1
From your example : if my text is: "Text" and than I click 8 so the Text will be "Text8" so the char is 8.
UpdatedChar=8 and
OldValue = Text
public string OldValue = string.Empty;
public char UpdateChar;
private void textBox1_TextChanged(object sender, EventArgs e)
{
var newText = sender as TextBox;
foreach (var val in newText.Text)
{
if(!OldValue.ToCharArray().Contains(val))
UpdateChar = val;
}
OldValue = newText.Text;
}

Categories

Resources