I have created the following GUI:
I enter my text in a TextBox (#1 in the picture). I have a Button (#2 in the picture) which should change the input in the TextBox to uppercase so I wrote the following code:
private void tuUpperCase(object sender, RoutedEventArgs e)
{
if (crossBox.IsChecked == true)
{
machineName.Text.ToUpper();
}
}
string.ToUpper() does not modify the string, just returns the modified string. So you need to set new modified value to the machineName.Text.
private void tuUpperCase(object sender, RoutedEventArgs e)
{
if (crossBox.IsChecked == true)
{
machineName.Text = machineName.Text.ToUpper();
}
}
The ToUpper method returns a new string, it does not change the machineName.Text property.
Returns a copy of this string converted to uppercase.
If you assign its return value (the new uppercase string) to machineName.Text it will work.
machineName.Text = machineName.Text.ToUpper();
One issue with this is that the caret position will be reset, when the new text is set. You could store the CaretIndex, modify the text and then reassign it to preserve the position.
private void tuUpperCase(object sender, TextChangedEventArgs e)
{
if (crossBox.IsChecked == true)
{
var caretIndex = machineName.CaretIndex;
machineName.Text = machineName.Text.ToUpper();
machineName.CaretIndex = caretIndex;
}
}
From your comment I assume that you use a CheckBox event like Checked or Unchecked. However, this will only convert the text to uppercase the moment you check the CheckBox, everthing typed afterwards is still lowercase. A solution to this would be to use the TextChanged event of the TextBox, which would convert the text as you type.
Related
I have been trying to figure out on disabling the text box when text is entered in the text box. I am able to do this but I have also got another problem which is, lets say you have a text box with some word i.e "Welcome". If I edit that and add more letter on to that i.e "WelcomeSSS" adding SSS then text is enabled. But when I delete "SSS" from that text box, button is still enabled and not DISABLED as the text is the same as it was before editing.
How do I make sure that the text is disabled in this situation?
And also I want to add dialog box when a user click on different button to go to different page without saving the edited content. How do i do this?
Here is my code so far:
private void textbox1_IsChanged(object sender, KeyEventArgs e)
{
//SaveButton.IsEnabled = !string.IsNullOrEmpty(TextBox1.Text);
if (TextBox1.Text.Trim().Length > 0)
{
SaveButton.IsEnabled = true;
}
if (WpfHelpers.Confirmation(resources.QuitWithoutSaving, resources.Changes))
{
}
}
This is using KeyUp event handler in wpf.
If I understood your question correctly...
private void textbox1_IsChanged(object sender, KeyEventArgs e)
{
if (textbox1.Text == "Welcome"){
SaveButton.IsEnabled = false;
}
else{
SaveButton.IsEnabled = true;
}
}
You need a data structure for storing the saved values. E.g. a List of strings. In the following snippet, these values are stored in the SavedTextBoxTexts list.
At first, the SaveButton gets disabled (you can do this in the XAML as well). When SaveButton has been clicked, the textBox1.text value will be stored in the list and the button gets disabled.
When textBox1.text is edited and SaveButton exists (already), the different conditions get checked.
If textBox1.text is already stored in SavedTextBoxTexts or textBox1.text is empty or contains only whitespace characters, SaveButton gets disabled. Otherwise the SaveButton will be enabled.
public partial class MainWindow : Window
{
private List<string> SavedTextBoxTexts = new List<string>();
public MainWindow()
{
InitializeComponent();
// Disable button right from the beginning.
SaveButton.IsEnabled = false;
}
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
// The function may be called, while the window has not been created completely.
// So we have to check, if the button can already be referenced.
if (SaveButton != null)
{
// Check if textBox1 is empty or
// textBox1.text is already in the list of saved strings.
if (String.IsNullOrEmpty(textBox1.Text) ||
textBox1.Text.Trim().Length == 0 ||
SavedTextBoxTexts.IndexOf(textBox1.Text.Trim()) >= 0)
{
// Disable Button
SaveButton.IsEnabled = false;
}
else
{
// If textBox1.text has not been saved already
// or is an empty string or a string of whitespaces,
// enable the SaveButton (again).
SaveButton.IsEnabled = true;
}
}
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
// Store the text in textBox1 into the SavedTextBoxTexts list.
SavedTextBoxTexts.Add(textBox1.Text.Trim());
// Disable the SaveButton.
SaveButton.IsEnabled = false;
}
// This is executed, when the other button has been clicked.
// The text in textBox1 will not be saved.
private void AnotherButton_Click(object sender, RoutedEventArgs e)
{
if (WpfHelpers.Confirmation(resources.QuitWithoutSaving, resources.Changes))
{
// Move to other page ...
}
}
}
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.
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.
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;
}
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.
}
}