I used System.Globalization.CultureInfo.InvariantCulture on my Textbox, but whenever I enter the Textbox and leave without altering the value it adds two more zeros, how can I sort this out?
private void textBox1_Leave_1(object sender, EventArgs e) {
double txt = double.Parse(textBox1.Text,
System.Globalization.CultureInfo.InvariantCulture);
textBox1.Text = txt.ToString("N2");
return;
}
Well, in general case, you can keep a collection of Coltrols which were manually edited and on control leaving check if the control is in the collection. Assuming that you work with WinForms:
using System.Globalization;
...
private readonly HashSet<Control> m_EditedControls = new HashSet<Control>();
// On each textBox1 change we should decide if we are going
// to update control's Text on leave or not
private void textBox1_TextChanged(object sender, EventArgs e) {
// If TextBox1 was edited while having keyboard focus,
// i.e. user got focus and edit the textbox manually
// put textbox as edited
if (sender is TextBox box && box.Focused)
m_EditedControls.Add(box);
}
Then on leaving textBox1 you can check if textBox1 has been edited manually:
// On leaving textBox1 we and an additional check
// if textBox1 has been edited manually
private void textBox1_Leave(object sender, EventArgs e) {
if (sender is TextBox box &&
m_EditedControls.Contains(box) &&
double.TryParse(box.Text, CultureInfo.InvariantCulture, out var value)) {
box.Text = value.ToString("N2", CultureInfo.InvariantCulture);
m_EditedControls.Remove(box);
}
}
Related
This question already has answers here:
C# update two text boxes at the same time?
(4 answers)
Closed 3 years ago.
I am making a simple unit converter in winforms, which means I'd be needing two Textboxes, in textbox1 'from' value is entered and in textbox2 the converted value is printed out. I want to make it work the other way around too i.e. to take input from textbox2 and to print the converted value out in textbox1. How am I supposed to do that?
You have to implement TextChanged event handlers for both TextBoxes; the only difficulty is to understand which control (textBox1 or textBox2)
has been changed by user (in order to prevent infinite loop when textBox1s value converts to textBox2s which in turn converts to textBox1s etc.). You can either check Focused:
private void textBox1_TextChanged(object sender, EventArgs e) {
// Do nothing, if user is changing some other control (e.g. textBox2)
if (!(sender as Control).Focused)
return;
// Having textBox1.Text value convert it forward to textBox2.Text
textBox2.Text = Convert(textBox1.Text);
}
private void textBox2_TextChanged(object sender, EventArgs e) {
// Do nothing, if user is changing some other control (e.g. textBox1)
if (!(sender as Control).Focused)
return;
// Having textBox2.Text value convert it backward to textBox1.Text
textBox1.Text = ReverseConvert(textBox2.Text);
}
Or play with event handlers (which is more flexible esp. if you can start conversion, by, say, button click, so neither textBox1 nor textBox2 have focus):
private void textBox1_TextChanged(object sender, EventArgs e) {
textBox2.TextChange -= textBox2_TextChanged;
try {
// Since textBox2.TextChanged switched off,
// changing textBox2.Text will not cause textBox1.Text change
textBox2.Text = Convert(textBox1.Text);
}
finally {
textBox2.TextChange += textBox2_TextChanged;
}
}
private void textBox2_TextChanged(object sender, EventArgs e) {
textBox1.TextChange -= textBox1_TextChanged;
try {
// Since textBox1.TextChanged switched off,
// changing textBox1.Text will not cause textBox2.Text change
textBox1.Text = ReverseConvert(textBox2.Text);
}
finally {
textBox1.TextChange += textBox1_TextChanged;
}
}
You can handle both textbox's Text change event in one Event handler (it will trigger event two times)
private void textBox_TextChanged(object sender, EventArgs e)
{
if (((TextBox)sender).Equals(textBox1))
textBox2.Text = Convert((TextBox)sender).Text);
else
textBox1.Text = ReverseConvert((TextBox)sender).Text);
}
This is probably an easy one for some of you.
I have a TextBox and a ListBox. ListBox provides options for the TextBox and copies selected item's text to TextBox on DoubleClick event. ListBox becomes visible only when TextBox fires Enter event. I do not want to discuss my reasons for selecting this control combination.
I want ListBox to disappear when any other control within the Form gets focus. So I capture Leave event of TextBox and call ListBox.Visible = fale The problem is that TextBox will also loose focus when I click on ListBox to select provided option thus preventing me from selecting that option.
What event combination should I use to preserve ListBox to select option but hide it whenever other controls get focus?
In the Leave method, you can check to see if the ListBox is the focused control or not before changing its Visibility:
private void myTextBox_Leave(object sender, EventArgs e)
{
if (!myListBox.Focused)
{
myListBox.Visible = false;
}
}
This example will provide you with the desired outcome:
public Form1()
{
InitializeComponent();
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
}
void textBox1_GotFocus(object sender, EventArgs e)
{
listBox1.Visible = true;
}
void textBox1_LostFocus(object sender, EventArgs e)
{
if(!listBox1.Focused)
listBox1.Visible = false;
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void Form1_Shown(object sender, EventArgs e)
{
//if your textbox as focus when the form shows
//this is the place to switch focus to another control
listBox1.Visible = false;
}
I got a textBox that load value from my database and a button that update changes based on the value of the textBox. What I need is to enabled the button if the textBox value changed. For example, the value that the textBox loads is 3 if I also input again 3 in the textBox the button will still be disable. The button will only enabled if I changed the value for example to 4 or any number but not 3.
Cache the original value somewhere then compare in the TextChanged Event
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == OriginalValue)
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
}
Alternatively, you could just do this (see CodesInChaos' comment below):
private void textBox1_TextChanged(object sender, EventArgs e)
{
button1.Enabled = textBox1.Text != OriginalValue;
}
I have a series of textboxes with which I want to associate a tooltip with. This tooltip would appear when the user clicks on a black textbox, then disappear when they start typing or when they leave the textbox. The tooltip should be placed directly above the textbox, this is why I'm using the ToolTip.Show method instead of the ToolTip.SetTooltip method (it lets me control the placement).
So far, for each textbox I have 3 methods; Enter, Leave and TextChanged:
tt = new ToolTip();
String message = "some message"; //different for each textbox
private void textbox1_Enter(object sender, EventArgs e)
{
if (textbox1.Text == String.Empty)
{
tt.Show(message, textbox1, new Point(0, -2 * textbox1.Height));
}
}
private void textbox1_Leave(object sender, EventArgs e)
{
tt.Hide(textbox1);
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
tt.Hide(textbox1);
}
Now consider two textboxes. Clicking on textbox1 triggers the tooltip as expected, in the expected location, then exiting textbox1 causes it to disappear. Trying the same thing on textbox2 also works. Now if I click on textbox1 again, the tooltip has the proper message, but the placement is in the same place as if I had clicked on textbox2. Not only that, but the shape of the tooltip is the same as for textbox2, meaning that my message gets truncated. (The message for textbox1 is longer than the one for textbox2). Does anyone know what might be causing this?
This only happens I think when the IsBalloon property is true. Unfortunately, known bug.
Try it like this:
private void textbox1_Enter(object sender, EventArgs e) {
if (textbox1.Text == String.Empty) {
tt.Show(string.Empty, textbox1, 0);
tt.Show(message, textbox1, new Point(0, -2 * textbox1.Height));
}
}
I have a windows form application which consists of a bunch of controls, but more specifically, two textBoxes. One of them is read only. The read only textBox value is supposed to be the same as the textBox that the user can type into.
So if the user types "Hello World" into textBox A, the value in textBox B should be automatically updated to "Hello World".
How do I go about doing this? I know I just need to set the text values, I'm just not sure where I place the code to get it done automatically rather than executed when a button is click or something along those lines.
TextChanged event:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
It sounds like you want something like:
writableTextBox.TextChanged += delegate {
readonlyTextBox.Text = writableTextBox.Text;
};
In other words, whenever the text in one textbox changes, update the other. This uses the Control.TextChanged event.
If you want textBoxB to be updated as soon as the text of textBoxA is changed (i.e immediately after the user press a key in textBoxA) the event is TextChanged:
this.textBoxA.TextChanged += new System.EventHandler(this.textBoxA_TextChanged);
private void textBoxA_TextChanged(object sender, EventArgs e)
{
textBoxB.Text = textBoxA.Text;
}
If you prefer to update the text in textBoxB only after the user has finished to edit textBoxA, you should use the Leave event:
this.textBoxA.Leave += new System.EventHandler(this.textBoxA_Leave);
private void textBoxA_Leave(object sender, EventArgs e)
{
textBoxB.Text = textBoxA.Text;
}
This should do what you need:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
Even shorter (better?) than the event approach is using winform's databinding. Just use this right after the InitializeComponents call:
readonlyTextBox.DataBindings.Add("Text", writableTextBox, "Text");