-2
I have an application of calculations, and I have the following situations: In a textbox I have a preset value in the codes to perform a calculation with another form that I type the value, for example: the textbox 1 sums with the textbox2. And I’m having trouble writing the code for when I want to change the value in texbox1, because it only reads the value that was inserted in the code and not the value that I enter when running the program.
Follow the code below:
OBS.: The code is only with the preset value. I couldn’t make the logic of how to edit the textbox when running the program.
private void txtValor1_TextChanged(object sender, EventArgs e)
{
valor1 = double.Parse((txtValor1.Text = ("100")).ToString());
}
private void txtValor2_TextChanged(object sender, EventArgs e)
{
valor2 = double.Parse((txtValor2.Text).ToString());
}
private void txtResult_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode ==Keys.Enter)
{
resultado = valor1 + valor2;
txtResult.Text = resultado.ToString();
}
}
The picture below shows that when I start the program, the Valor 1
in the txtValor1
already appears and I just need to type the Valor 2
in the txtValor2
, Valor 3
is the field of the calculation result.
explain better? if you want to do what?
– novic
@novic I need txtValor1 to be editable when running the program so I can change the value. Because the value "100" is fixed, but also if I need to be able.
– Cristina
But you type and it goes like this?
– novic
in the txb1 event you are setting the value to 100... then it will always be 100... if you want to set only at the time the program starts, you have to put in the form load event or after initializing the components in the form constructor.
– Rovann Linhalis
take advantage and search for tryparse, because if the user type a non-numeric character will give conversion error in Parse and crash the application
– Rovann Linhalis
@novic I can’t type, it doesn’t change the value.
– Cristina
@Rovannlinhalis Oh then I define it only in the form load, but even if I define it in the form load it calculates normally with the value of 100? I put Try catch and put exceptions to just type numeric characters.
– Cristina
@Rovannlinhalis Inseri no event Load do form. And I have to insert in txt1:
valor1 = double.Parse((txtValor1.Text).ToString());
Because just by inserting in Load, it does not let me change txt1.– Cristina
yes, take the assignment
txtValor1.Text = ("100")
what you did at the event... | about Try.... I referred to tryparse: https://docs.microsoft.com/pt-br/dotnet/api/system.double.tryparse?view=netframework-4.5 | you can also do the calculation all in one event... without pressing a key in the txb of the result– Rovann Linhalis
@Rovannlinhalis I put the code in the Load form, but when I want to change the value it does not delete the value entered in Load. Why is this happening? Any hints? Yes, I understand about Tryparse and I’ve changed to calculate everything together.
– Cristina
in the textbox event, you are assigning the value in the field.......
valor1 = double.Parse((txtValor1.Text = ("100")).ToString());
....(txtValor1.Text = ("100")
is an assignment– Rovann Linhalis
@Rovannlinhalis It worked! Thank you so much for your help!!
– Cristina