问题
I am creating my first C# project, but I can't find a way to display an integer variable in a TextBox or Label. I am using visual C# and Visual Studio 2013.
I have been using C++ for quite a while and I would intuitively use 'cout', but I have no idea where to put fe: textBox4.text = cal
(where cal is variable).
回答1:
but I have no idea where to put fe: textBox4.text = cal (where cal is variable).
You should put this code inside of a method.It can be Form_Load event, or a Button
click, for example add a button to your form with designer (drag and drop from toolbox),double click your button and put this code inside of your button click event:
textBox4.Text = cal.ToString();
回答2:
Use one of these forms:
textBox4.Text = cal.ToString();
textBox4.Text = "" + cal;
textBox4.Text = string.Format("{0}", cal);
The last is the best for flexibilty, as shown here with some decorations:
textBox4.Text = string.Format("[CELL: {0}]", cal);
If there aren't any decorations the string.Format()
will have an unnecessary overhead that can be avoided by the first form.
For readability string.Format()
should be prefered over concatenating strings (second form). What you can't do with concatenating but is possible with string.Format()
is making the format string a variant (taken from configuration for I18N
for example).
Pay attention to the capital T
in the Text
property.
回答3:
Problem : you can only Assign String
to the TextBox Text
property.
Solution : convert your cal
variable into String using ToString()
function and then assign it to the TextBox Text
property.
Replace This:
textBox4.text = cal;
With This:
textBox4.Text = cal.ToString();
回答4:
Just Use like this
textBox4.Text = cal.ToString();
ToString()
textBox4.Text=Convert.ToString(cal);
textBox4.Text = new StringBuilder().Append(cal).ToString();
textBox4.Text=Convert.string.Empty + Cal;
来源:https://stackoverflow.com/questions/21486151/displaying-an-int-variable-in-textbox-label-in-visual-c-sharp