| View previous topic :: View next topic |
| Author |
Message |
asianking Expert Cheater
Reputation: 0
Joined: 25 Jun 2008 Posts: 109
|
Posted: Tue May 29, 2012 9:54 pm Post subject: Need a simple help with C# calculation |
|
|
I have created a simple textbox1 and a button.
Can someone help me on how to code it so when button is pressed; textbox1 will divide by 10 then convert the total into hex on the same textbox? This event will only happen when the button is pressed.
I know how to convert it to hex already, but just not the calculation part.
_________________
|
|
| Back to top |
|
 |
DanielG Expert Cheater
Reputation: 1
Joined: 13 May 2009 Posts: 130 Location: The Netherlands
|
Posted: Wed May 30, 2012 6:50 am Post subject: |
|
|
| Code: | private void button1_Click(object sender, EventArgs e)
{
int number = 0; // set up a variable
number = int.Parse(textBox1.Text); // parse the text to an int
number = number / 10; // divide by 10
textBox1.Text = number.ToString(); // assign number to textbox (change to hex first if desired)
} |
Something like this?
|
|
| Back to top |
|
 |
asianking Expert Cheater
Reputation: 0
Joined: 25 Jun 2008 Posts: 109
|
Posted: Wed May 30, 2012 8:53 am Post subject: |
|
|
Thanks Daniel, it worked great.
_________________
Last edited by asianking on Wed May 30, 2012 11:45 am; edited 2 times in total |
|
| Back to top |
|
 |
DanielG Expert Cheater
Reputation: 1
Joined: 13 May 2009 Posts: 130 Location: The Netherlands
|
Posted: Wed May 30, 2012 11:43 am Post subject: |
|
|
Change the last line:
| Code: | | textBox1.Text = "0" + number.ToString(); |
If I were you I'd read some "how to program" tutorials. Appending something to a string is quite basic.
|
|
| Back to top |
|
 |
asianking Expert Cheater
Reputation: 0
Joined: 25 Jun 2008 Posts: 109
|
Posted: Wed May 30, 2012 11:47 am Post subject: |
|
|
I see... I didn't think about + would do the trick. On VB, I would just need the quotation and &. I would think they're samiliar, but I guess I was wrong.
Thanks, very useful!
_________________
|
|
| Back to top |
|
 |
Pingo Grandmaster Cheater
Reputation: 8
Joined: 12 Jul 2007 Posts: 571
|
Posted: Thu May 31, 2012 2:33 am Post subject: |
|
|
Another way and slightly cleaner method.
| Code: | static string Output(string Input)
{
int tmp;
return int.TryParse(Input, out tmp) ? (tmp / 10).ToString("X") : Input;
} |
Usage
| Code: | private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = Output(textBox1.Text);
} |
_________________
|
|
| Back to top |
|
 |
|