Click here to Skip to main content
15,905,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created dynamic textbox control

I want to do if you enter a string I will convert it to an integer value, but it gives me "Invalid cast from 'DateTime' to 'Int32'".
I entered 234 into the textboox, but textbox sees datetime. Why?

I think when I create a dynamic textbox control I should specify the textbox's control type.

Thanks your time!


Code:

C#
TextBox txtSayi = new TextBox();
Button btnHesapla = new Button();
Label lblYazi = new Label();
this.Controls.Add(btnHesapla);
this.Controls.Add(txtSayi);
this.Controls.Add(lblYazi);


txtSayi.Location = new Point(100, 150);

lblYazi.Location = new Point(100, 100);
btnHesapla.Text = "HESAPLA";
btnHesapla.Location = new Point(100, 200);
btnHesapla.Size = new Size(20, 20);
btnHesapla.Width = 150;
btnHesapla.Height = 50;
btnHesapla.FlatStyle = FlatStyle.Flat;

btnHesapla.Click += new EventHandler(btnHesapla_Click);



This is the button click handler:
C#
void btnHesapla_Click(object sender, EventArgs e)
{
    int deg = Convert.ToInt32(txtSayi);gives me an error here
    int yuzler, onlar, birler;
    string yuzz, onn, birr;

    yuzler = (((deg) / 100) * 100);
    yuzz = cevir(yuzler);
    onlar = (((deg) / 10) * 10) - yuzler;
    onn = cevir(onlar);
    birler = ((deg) % 10);
    birr = cevir(birler);
    lblYazi = yuzz + " " + onn + " " + birr;
}
Posted
Updated 3-Jan-12 23:12pm
v3
Comments
thatraja 4-Jan-12 4:57am    
Show the code
[no name] 4-Jan-12 5:03am    
Please share the code

C#
int deg = Convert.ToInt32(txtSayi);


Please put txtSayi.Text

i.e.
C#
Convert.ToInt32(txtSayi.Text);


I suppose this will solve the problem
 
Share this answer
 
v2
Comments
Un_NaMeD 4-Jan-12 5:16am    
That does the trick. My 5!
C#
int deg = Convert.ToInt32(txtSayi);gives me an error here

should be
C#
int deg = Convert.ToInt32(txtSayi.Text);

In reality you should always use TryParse()[^] method(s) which let you check for invalid characters.
 
Share this answer
 
Comments
Un_NaMeD 4-Jan-12 5:16am    
That is true. My 5!

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