C# - refresh textbox while typing value - c#

How can i make auto refresh textbox while typing value like this?
i tried to do the same but it did not work. i always to hit ENTER to refresh or click on up/down arrows to refresh the value
here is the code
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
try
{
double a = double.Parse(s1.Text); //textbox 1
double b = double.Parse(s2.Text); //textbox 2
double s = a * b;
resultSpeed.Text = "" + s; //s is the result
}
catch
{
MessageBox.Show("Please input the number");
}
}

Just use event KeyUp. It will trigger every time you put a symbol.
ValueChanged isn't working because it only triggers when you are done with editing - you press enter or change focus.
So basically change your event from ValueChanged to KeyUp.
I'm not posting any code because the only change will be subcribing to other event. Your function is fine, however you should change its name :)

Put your code into textbox's TextChanged Event.
Like this
private void textBox1_TextChanged(object sender, EventArgs e)
{
calculate();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
calculate();
}
private void calculate()
{
double a = 0, b = 0, demo;
if (double.TryParse(textBox1.Text, out demo))
a = double.Parse(textBox1.Text); //textbox 1
if (double.TryParse(textBox2.Text, out demo))
b = double.Parse(textBox2.Text); //textbox 2
double s = a * b;
textBox3.Text = s.ToString(); //s is the result
}

Related

A way to add int value to textbox with a button click and then subtract from the int value with another button

I'm trying to have a button click event add an int value to a textbox and then have another button click event subtract from the same textbox that I just added the int value to. The problem I'm having is that the subtract button enters a negative(-1) value to the textbox and if I click on the add button again it goes right back to the int value I had before clicking the subtract button. I just want the add button to add and the subtract button to subtract the in value. I'm very new to c# and I've tried a everything I've found online and I'm losing faith.
I've tried if statements, and some other methods I found online and nothing works.
This is the code I have now. The add button works fine, but the subtract button doesn't do what I want.
private int a = 0;
private void btnAdd_Click(object sender, EventArgs e)
{
a++;
txtSummary.Text = a.ToString();
}
private void comboBoxValues_SelectedIndexChanged(object sender, EventArgs e)
{
}
private int b = -0;
private void btnSubtract_Click(object sender, EventArgs e)
{
b--;
txtSummary.Text = b.ToString();
}
This will convert whatever is in your TextBox to an Integer, then add or subtract 1 to that value:
private void Form1_Load(object sender, EventArgs e)
{
txtSummary.Text = "0";
}
private void btnAdd_Click(object sender, EventArgs e)
{
AddToTextBox(1);
}
private void btnSubtract_Click(object sender, EventArgs e)
{
AddToTextBox(-1);
}
private void AddToTextBox(int changeBy)
{
int value;
if(int.TryParse(txtSummary.Text, out value))
{
value = value + changeBy;
txtSummary.Text = value.ToString();
}
else
{
MessageBox.Show("Invalid Integer in TextBox!");
}
}

How to make buttons to enter specific values?

public partial class Form1 : Form
{
public static string a = "a"; public static string b = "b"; public static string c = "c";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = a;
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = b;
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = c;
}
private void button4_Click(object sender, EventArgs e)
{
a = null;
b = null;
c = null;
}
}
I want to make a simple keyboard for a chat.
I started it with a small sample program in which there are only 3 buttons; Button a, Button b, Button c for a,b,c, respectively.
When I run the Program,I press Button a for a & then Button b for b (Now I want the Output in the form ab) but it first shows a and then on pressing button b it erases a and shows b.
I want to make more buttons like these to make a keyboard.
Basically,I want to print the letters stored in the buttons sequentially,but it erases the first one and then prints the next one..
The easiest way to create an on-screen keyboard is to use the buttons texts, except in special keys like backspace, enter, clear etc`.
That way all your text buttons click events can be handled with a single method:
private void KeyButton_Click(object sender, EventArgs e)
{
textBox1.Text += ((Button)sender).Text;
}
private void ClearButton_Click(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
}
private void BackspaceButton_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.SubString(0, textBox1.Text.Length-1);
}
It erases the value because you are using = operator. Try to use +=
textBox1.Text += c;
textBox1.Text = textBox1.Text + c;
Also you can get a text value from the Button's Text property.
And have only one Button.Click event handler, for each button.
private void button_Click(object sender, EventArgs e)
{
var button = sender as Button;
textBox1.Text = textBox1 + button.Text;
}
As I told you in the comments before you posted the code you need to concatenate (a.k.a append) the character to the text box.
If you have a text box named textBox1 doing this:
textBox1.Text = 'a'
will replace any text already written in the text box with the character 'a'
What you need to do is use the += operator:
textBox1.Text += a;
textBox1.Text = b;
textBox1.Text = c;

How to design a keypad in C#?

I am new to C# and I am planing to design my own keypad but I don't know how/where to start. as shown in photo, I have 4 textBoxes the keypad buttons.
The first problem came into my mind was: how can I detect the cursor location (which textBox is the cursor in?).
So for example if I had only one textbox then it is easy I could write inside button1 : textBox1.text = "1" and inside button2 : textBox1.text = "2" and inside button_A : textBox1.text = "A".... and so on but I have 4 textBoxes and it is confusing.
Can you please provide me with an idea or what to write inside each button to print its value in the textbox which the cursor is in.
Thank you professionals.
Firstly, have a textbox that represents the one that is selected (outside of subroutines but inside the class):
TextBox SelectedTextBox = null;
And then make the "Click" event of each TextBox look like this:
private void textBoxNUM_Click(object sender, EventArgs e)
{
SelectedTextBox = sender as TextBox;
}
And then make the "Click" event of each Button look like this:
private void buttonNUM_Click(object sender, EventArgs e)
{
if (SelectedTextBox != null)
{
SelectedTextBox.Text = buttonNUM.Text;//Or set it to the actual value, whatever.
}
}
Or if that one doesn't work, this should.
private void buttonNUM_Click(object sender, EventArgs e)
{
if (SelectedTextBox != null)
{
(SelectedTextBox as TextBox).Text = buttonNUM.Text;//Or set it to the actual value, whatever.
}
}
To check if a textbox is focused you can do
if(textbox1.Focused)
{
//Print the value of the button to textbox 1
}
else if (textbox2.Focused)
{
//Print the value to textbox 2
}
UPDATE:
Since the textbox will lose focus when you click the button, you should have a temporary textbox (ie lastTextboxThatWasFocused) which is saved to everytime a textbox gains focus. Write an OnFocused Method and do something like
public void Textbox1OnFocused(/*Sender Event Args*/)
{
lastTextboxThatWasFocused=textbox1;
}
Then on button click you can do
if(lastTextboxThatWasFocused.Equals(textbox1))
{
//ETC.
}
You can give something along these lines a try. Create a generic click handler for the buttons and then assign the value to a textbox the text from the button, which happens to be the value. You can check which box was the last one focused in the TextBoxes' Click event. Create a global variable to store which one and use it in the below method.
private TextBox SelectedTextBox { get; set; }
private void NumericButton_Click(object sender, EventArgs e)
{
var clickedBox = sender as Button;
if (clickedBox != null)
{
this.SelectedTextBox.Text += clickedBox.Text;
}
}
private void TextBox_Click(object sender, EventArgs e)
{
var thisBox = sender as TextBox;
if (thisBox == null)
{
return;
}
this.SelectedTextBox = thisBox;
}
Try this code:
TextBox LastTxtBox;
private void textBox_Enter(object sender, EventArgs e)
{
LastTxtBox = sender as TextBox;
}
private void button_Click(object sender, EventArgs e)
{
LastTxtBox.Text = this.ActiveControl.Text;
}
Add textBox_Enter function to all textboxes enter event.
Add button_Click to all buttons click event.
Button Enter Event
Control _activeControl;
private void NumberPadButton_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (_activeControl is TextBox || _activeControl is RichTextBox)
{
_activeControl.Text += btn.Text;
if (!_activeControl.Focused) _activeControl.Focus();
}
}
TextBox or RihTextBox Enter Event
private void TextBoxEnter_Click(object sender, EventArgs e)
{
_activeControl = (Control)sender;
}

C# Calculator typing by pressing buttons

I am studying C# now, so i am making some kind of exercises to get used to C# syntax and learn better. I decided to make a calculator looking alike the normal windows calculator.
I created only one button "1" and one textbox.
I want to make this button write 1 in textbox when I press it and also make an int variable equal to the number in the textbook, to make the calculation later. So I can't either change "int a"'s value or change text in the textbox, it always shows 01, because a always equals to 0.
How can I make the program, both show the correct numbers and change the value of a correctly?
For example how can i make the program show eleven in the textbox when i press button twice and also change "int a"'s value to 11?
public partial class Form1 : Form
{
int a;
string Sa;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Sa = a.ToString() + "1";
textBox1.Text = Sa;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
a = Int32.Parse(textBox1.Text);
}
just it.. Change text on textBox every button click, and change variable a every textBox changed.
The value could then be set using
a = int.Parse(Sa);
textBox1.Text = Sa.TrimStart('0');
Although if you'd like to be more efficient about it,
a = a * 10 + 1;
not have Sa at all,
textBox1.Text = a.ToString();
If you run into integer overflows, you should use BigInteger.
You have several options:
Make int a nullable int. That way you can check if int is already set
int? a;
if ( a.HasValue )
{
}
else
{
}
Check if the Text property of textBox1 is empty (which means you don't have to append a to it)
if ( textBox1.Text == string.Empty)
{
}
else
{
}
public void btnOne_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnOne.Text;
}
private void btnTwo_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
}
// etc
for any button you want to append text to the text box set the click property to btn_Click then pt this code inside the method
private void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
// This will assign btn with the properties of the button clicked
txt_display.Text = txt_display.Text + btn.Text;
// this will append to the textbox with whatever text value the button holds
}

Event Handlers in My Converter App Are Not Working

This is the code I have so far:
public partial class Form2 : Form
{
public Double X;
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
if(textBox1.Text != "")
X = Convert.ToDouble(textBox1.Text);
X *= 0.001;
label3.Text = "metros";
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
X = Convert.ToDouble(textBox1.Text);
X *= 0.62;
label3.Text = "milhas";
}
private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
X = Convert.ToDouble(textBox1.Text);
label3.Text = "quilómetros";
}
private void button5_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
X = Convert.ToDouble(textBox1.Text);
X *= 3280,84;
label3.Text = "pés";
}
private void button6_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
X = Convert.ToDouble(textBox1.Text);
X *= 0.17998560115190784737;
label3.Text = "léguas";
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = Convert.ToString(X);
}
This is how the window looks like:
What these do is, when you insert a value on textBox1 (the red one on the middle left of the window), you then select the measurement from the buttons on the right, this will convert the introduced value to kilometres and store it in the variable X and write the chosen measurment on a label to the right of the textBox1.
When you press the "Converter" button, (for now) I wanted the textBox2 to show X, however, this only works when I press "metros" or "pés", if I choose one of the other buttons for the conversion it will simply do nothing...
Does someone have any idea of what's wrong?
And also, side question, how do you select items from the comboBox?
Firstly, if statements only execute the very next statement if their condition is met:
if(textBox1.Text != "")
X = Convert.ToDouble(textBox1.Text); // only run if 'if' is true
X *= 0.001; // always run
label3.Text = "metros"; // always run
The if is associated with the next line. If you want all of the following code to be associated with the if, then you need to open a block:
if(textBox1.Text != "")
{
X = Convert.ToDouble(textBox1.Text);
X *= 0.001;
label3.Text = "metros";
}
To help guard against this, I would advise adopting a consistent style for single-line if statements:
if (something) SomeStatement(); // same line
if (something)
SomeStatement(); // indented
if (something)
{
SomeStatement(); // single statement block
}
It's possible some of your buttons are not working because the link between the event handler methods and the events has been broken. You should open the designer and ensure that each of the buttons has a Click handler assigned.
With respect to the combo-box part of your question: ComboBox.SelectedItem allows you to get or set the selected item. Alternatively you can use ComboBox.SelectedIndex to get or set the index of the item that is selected.

Categories