Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have textbox and I wont to multiply the value in textbox and then use this textbox to show result in textbox_textChange()
the wiring Appears in x variable
here my code..

What I have tried:

private void textBox1_TextChanged(object sender, EventArgs e)
      {

          int x = int.Parse(textBox1.Text) * 350;

          textBox1.Text = x ;

      }
Posted
Updated 3-Feb-22 0:22am

textBox1.Text = x.ToString() ;

use Int32.ToString Method [^] or Convert.ToString Method [^]

Note: use Int32.TryParse Method [^] to avoid invalid format exception
 
Share this answer
 
v2
You should actually use two textboxes: the first reserved to user input and the second to computation output.
 
Share this answer
 
Comments
NebroProg 31-Jul-17 6:56am    
i tried it but not work
This can't be simply done using the TextChanged event because changing the content would raise the event again.

It is common to use another text box to show the result or provide a button to perform the calculation.

You should also use the Int32.TryParse Method (String, Int32) (System)[^] to handle invalid input.

If you still want to use the same box, you have to use a guarding variable:
bool selfUpdate = false;

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (!selfUpdate)
    {
        int x;
        if (Int32.TryParse(textBox1.Text, out x))
        {
            x *= 350;
            selfUpdate = true;
            textBox1.Text = x.ToString();
            selfUpdate = false;
        }
    }
}

[EDIT]
But note that when using the above each change will update so that only single digits can be entered or text must be pasted.
[/EDIT]
 
Share this answer
 
v3
Comments
NebroProg 31-Jul-17 6:55am    
error here if (int.TryParse(textBox1.Text, x))
Jochen Arndt 31-Jul-17 7:02am    
See my updated answer.

The code was just to show how it can be done. It is an uncommon and not recommended solution.
NebroProg 31-Jul-17 7:25am    
I tried to type any digit but the result is 0
Jochen Arndt 31-Jul-17 7:36am    
I don't know what is happening because I can not run your code.

But you can use the debugger to set a break point on your function and inspect the variables. Or use the WriteLine() methods from the Debug or Trace classes to print them to the debug output window.
Richard MacCutchan 31-Jul-17 7:03am    
The use of selfUpdate here is very clever; never occurred to me. :thumbsup:
Convert int to string before assign it to text-box Text property.
textBox1.Text=x.ToString();


refer this to learn
Convert.ToString Method.
 
Share this answer
 
Comments
NebroProg 31-Jul-17 6:55am    
this gives error
SurangiTi 31-Jul-17 7:31am    
what's the error?
SurangiTi 31-Jul-17 7:48am    
I think you are not clear of what you want to achieve.

I assume you want to multiply the integer written to the Textbox1. There are many ways to achieve this result based on the requirement.
Examples
--------

1)use a text box and a label. [single digits can be entered]
2)use a text box and a button. [this is the ideal solution]
3)there are many other ways...

Either way you need to validate user inputs, otherwise conversion errors will throw and it is not avoidable.
Thank's alot my friends ^^
I try and try until I found the solution
I change the event of textbox from textBox1_textboxChange() totextBox1_KeyUp
and change code to

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
int x = int.Parse(textBox1.Text) * 350;
textBox1.Text = Convert.ToString(x);
}
 
Share this answer
 
hi try this
double result=convert.double(textbox.text)*350;
textbox.text=result.tostring();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900