TextBox c# refer string - c#

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.

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.

How get Event "item Selected" with AutoComplete in C#?

I have code using an AutoCompleteStringCollection:
private void txtS_TextChanged(object sender, EventArgs e)
{
TextBox t = sender as TextBox;
string[] arr = this.dbService.GetAll();
if (t != null)
{
if (t.Text.Length >= 3)
{
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
collection.AddRange(arr);
this.txtSerial.AutoCompleteCustomSource = collection;
}
}
}
How can I get the event for "item selected" after user selects an AutoComplete suggestion? And value of field?
There's no such thing as chosen item Event for a textBox, which I believe you're using for the AutoComplete. What you could do is add a key down event to your textBox. There you could verify if the enter key was pressed (clicking on a suggested link is the same as pressing enter). Something like that:
private void textBox1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == Keys.Enter) {
String selItem = this.textBox1.Text;
}
}
Rather than focusing on detecting if an item from the autocomplete list was selected, instead you should check if the current value of the textbox is in the set of autocomplete entries.
if (txtSerial.AutoCompleteCustomSource.Contains(t.Text))
{
// Logic to handle an exact match being selected
...
}
else
{
// Update the autocomplete entries based on what was typed in
}
If the user typed in an exact string which happens to be be within the list of autocomplete values -- OR -- they select that value from the autocomplete list -- should this produce any different behavior? I think that in most cases it should not.
Short answer: make a custom event
Long answer:
You can intercept the KeyDown event of your textbox for numpad Enter or normal Enter and the mouse doubleclick event of the toolbox and compare the content of the toolbox then fire an event if they match that a delegate will pick up.
It depends a bit on the situation and workflow of your program but I have an example where I trigger the check on focuslost of the combobox. And then I check if the selected value is part of the collection:
private void cmbt1Name1_LostFocus(object sender, RoutedEventArgs e)
{
ComboBox cmb = sender as ComboBox;
FillFivePoints(cmb);
}
private void FillFivePoints(ComboBox usedCombobox)
{
if (txtSerial.AutoCompleteCustomSource.Contains(t.Text))
{
...

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

How to get the NEW text in TextChanged?

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

How to convert slider value to textbox

i'm new in C# and I want to convert value of slider to textbox. One option i found is set binding for text box, but I need send value in event.
I tried some solutions, but not worked.
private void sliderName_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
Slider sliderName = sender as Slider;
TextBox textBoxName = new TextBox();
textBoxName.Text = sliderName.Value.ToString();
}
Thanks for helping and be patient with me. :)
You're creating a new TextBox, but not positioning it anywhere.
You should have your TextBox already on your form, and reference it by the name you gave it at design-time in the IDE. For example, if you just drop a TextBox on the form, the IDE will give it a name like textBox1, and you use it by that name:
private void sliderName_ValueChanged(object sender,
RoutedPropertyChangedEventArgs<double> e)
{
// Don't use the same name used on the form if you're
// declaring a variable here. Use a name that's local to
// this event.
Slider slide = sender as Slider;
// Use the IDE-set name here.
difficultyBox.Text = slide.Value.ToString();
}
Updated to reflect name change based on comment below.
OR an even faster way:
private void sliderName_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
textBox1.Text = ((Slider)sender).value.ToString();
}
The only difference between this post, and Ken's Post is that I am casting the sender as a slider while setting the text property of textBox1.
Just showing you different options and a different way of doing the same thing.
Your approach would probably work, except that you are declaring a new TextBox local to this event handler, so you'll never see anything on the screen.
Try setting the .Text property of a TextBox in your form;
private void sliderName_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
Slider sliderName = sender as Slider;
difficultyBox.Text = sliderName.Value.ToString();
}

Categories

Resources