Newbie question about textboxes

G

George

Hi all,

I am just starting out in C3, so please forgive me for asking this question:

How can i put the contents of an integer in a textbox? I have this:
int a;
a = 10;
textBox1.Text = a;

I get a "cannot convert type 'int' to 'string' compile error.

Thank you in advance.

George
 
B

Bob Powell [MVP]

To do it yourself you can use the ToString method of the integer to get the
text representing the string.

To convert it back you can ise the int.Parse method to get the integer from
the string.

The correct way to do this however is to use databinding to bind the integer
to the textbox. The databinding system uses the TypeConverter for the data
type to do the conversion to and from the string representation.

public partial class Form1 : Form

{

int n;

public int N

{

get { return n; }

set { n = value; }

}

public Form1()

{

InitializeComponent();

this.textBox1.DataBindings.Add("Text", this, "N");

}

}


--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


You need to convert "a" to the correct type of the Text property, in this
case is string so you have to do
txtBox1.Text = a.ToString();

To do the opposite you have to do something similar:

a = Convert.ToInt32( txtBox1.Text);


You should get a book of C# though.
 
G

George

Wow...thanks guys....I guess those answers would be considered "the long and
the short of it"!

Thanks again.

George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top