Textbox with preset value, but also editable in c#

Asked

Viewed 80 times

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

inserir a descrição da imagem aqui

  • explain better? if you want to do what?

  • @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.

  • But you type and it goes like this?

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

  • 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

  • @novic I can’t type, it doesn’t change the value.

  • @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.

  • @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.

  • 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

  • @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.

  • 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

  • 1

    @Rovannlinhalis It worked! Thank you so much for your help!!

Show 7 more comments

1 answer

-1


The easiest is perhaps to use a property that always returns the sum value of the two fields. This is my proposal. It can be improved and to be more robust, you should use Tryparse instead of Parse.

public double valorCalculado 
{
    get
    {
        if(txtValor1.Text.Lenght > 0 && txtValor2.Text.Lenght > 0)
            return double.Parse((txtValor1.Text)) + double.Parse((txtValor2.Text));
        else if(txtValor1.Text.Lenght > 0)
            return double.Parse((txtValor1.Text));
        else if(txtValor2.Text.Lenght > 0)
            return double.Parse((txtValor2.Text));
        else
            return 0.0;
    }
}

private void txtValor1_TextChanged(object sender, EventArgs e)
{
    Recalcular();
}

private void txtValor2_TextChanged(object sender, EventArgs e)
{
    Recalcular();
}

private void txtResult_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode ==Keys.Enter)
    {
        Recalcular();
    }
}

private void Recalcular()
{
    txtResult.Text = valorCalculado.ToString();
}   

As for preset values, just put them in the constructor.

  • Thank you so much for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.