-
September 14th, 2011, 08:13 AM
#1
learning c# need help
Hello, im new here!
anyway..im trying to make an Calculator in window forms. so the problem is, i have 2 text box's
like num1 = textbox
and num2 = textbox
then i have button called "Go"
in the text box's im enter 2 numbers and its not reading it...
i mean, i set 2 variables int num1 = 0;
and int num2 = 0;
now when you press go its must do the math.
but after you pressing Go its show me the number 0, its must be bcuz its not reading from the textbox's the numbers of the user.
anyone can help?
-
September 14th, 2011, 08:18 AM
#2
Re: learning c# need help
show the code and maybe we can help
Always use [code][/code] tags when posting code.
-
September 14th, 2011, 08:24 AM
#3
Re: learning c# need help
i just deleted, but i just have one question, how do i read what the user type in textbox's?
-
September 14th, 2011, 08:24 AM
#4
Re: learning c# need help
and save it to an variable (int).
-
September 14th, 2011, 09:39 AM
#5
Re: learning c# need help
thats what i did:
int num1 = 0;
int num2 = 0;
int result = 0;
result = num1 + num2;
MessageBox.Show("Math: " + result);
lets say i typed in num1 (TextBox) - 2
and in num2 (TextBox) - 2
its must show 4 after i press go, but its show 0, bcuz its not reading from the textboxs, what the user typed..(2)
-
September 14th, 2011, 09:53 AM
#6
Re: learning c# need help
When the user clicks your "Go" button, you will need to use the TextBox.Text to reference what they typed in each textbox...
This would show what the user typed in the textbox called "num1".
Code:
MessageBox.Show(num1.Text);
So, consider the two string I create in this example to be each of the TextBox.Text values, and this should give you the general idea...
Code:
string n1 = "2"; // represents TextBox1.Text
string n2 = "2"; // Represents TextBox2.Text
int num1 = 0;
int num2 = 0;
int result = 0;
if (int.TryParse(n1, out num1))
if (int.TryParse(n2, out num2))
result = num1 + num2;
Console.WriteLine("{0} + {1} = {2}", num1, num2, result);
In your case, you could just as well replace the "n1" and "n2" in the TryParse statements of the example with num1.Text and num2.Text, if those are the names of your textboxes.
Best of luck!
-
September 14th, 2011, 09:59 AM
#7
Re: learning c# need help
Console.WriteLine("{0} + {1} = {2}", num1, num2, result);
its not must be MessageBox.Show?
-
September 14th, 2011, 10:07 AM
#8
Re: learning c# need help
i got it to work, but still, its not reading what im typing in textboxs.
lets say im type 5 + 5 its show as math : 4.
string num1 = "2";
string num2 = "2";
int n1 = 0;
int n2 = 0;
int result = 0;
if (int.TryParse(num1, out n1))
if (int.TryParse(num2, out n2))
result = n1 + n2;
MessageBox.Show("Math: " + result);
-
September 14th, 2011, 10:15 AM
#9
Re: learning c# need help
Ok... I used two strings (num1 and num2) to represent your textbox values... YOU should use your actual textbox values instead of those two strings.
Code:
int num1 = 0;
int num2 = 0;
int result = 0;
if (int.TryParse(num1.Text, out num1))
if (int.TryParse(num2.Text, out num2))
result = num1 + num2;
MessageBox.Show(String.Format("{0} + {1} = {2}", num1, num2, result));
As far as the Console.Writeline(), I used that because I do my little tests in a console application instead of a Windows Form application.
As a side note, when you post code, put it between code tags like...
[code]
// your code goes here
[/code]
...makes it easier for everyone to read...
-
September 14th, 2011, 10:16 AM
#10
Re: learning c# need help
Error 2 'int' does not contain a definition for 'Text'
still got erorrs.
Code:
string num1 = "2";
string num2 = "2";
int n1 = 0;
int n2 = 0;
int result = 0;
if (int.TryParse(num1.Text, out n1))
if (int.TryParse(num2.Text, out n2))
result = n1 + n2;
MessageBox.Show(String.Format("{0} + {1} = {2}", num1, num2, result));
my textbox's called: num1,num2
Last edited by Juicers24; September 14th, 2011 at 10:24 AM.
-
September 14th, 2011, 10:41 AM
#11
Re: learning c# need help
ok fixed! :P
Code:
int n1 = int.Parse(num1.Text);
int n2 = int.Parse(num2.Text);
int result = 0;
result = n1 + n2;
MessageBox.Show(String.Format("{0} + {1} = {2}", num1, num2, result));
-
September 14th, 2011, 10:42 AM
#12
Re: learning c# need help
You don't need the two lines that initiate the strings.. remove those as I showed earlier.
When you set string num1 = "0"; and string num2 = "0"; you are replacing your textboxes with strings, thus "no Text" error.
The code below is all you need in your click event.
Code:
int num1 = 0;
int num2 = 0;
int result = 0;
if (int.TryParse(textBox1.Text, out num1))
if (int.TryParse(textBox2.Text, out num2))
result = num1 + num2;
MessageBox.Show(String.Format("{0} + {1} = {2}", num1, num2, result));
-
September 14th, 2011, 11:39 AM
#13
Re: learning c# need help
thanks for the help, i got it to working.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|