动态文本框类型有问题吗? [英] Dynamic textbox type problem?
问题描述
我创建了动态文本框控件
如果要输入字符串,我想做的是将其转换为整数值,但是它给了我从"DateTime"到"Int32"的无效转换".
我在textboox中输入了234,但文本框看到了datetime.为什么?
我认为在创建动态文本框控件时,应指定文本框的控件类型.
谢谢您的时间!
代码:
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:
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:
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;
}
推荐答案
int deg = Convert.ToInt32(txtSayi);
请输入txtSayi.Text
即
Please put txtSayi.Text
i.e.
Convert.ToInt32(txtSayi.Text);
我想这将解决问题
I suppose this will solve the problem
int deg = Convert.ToInt32(txtSayi);gives me an error here
应该是
should be
int deg = Convert.ToInt32(txtSayi.Text);
实际上,您应该始终使用 TryParse()
[
In reality you should always use TryParse()
[^] method(s) which let you check for invalid characters.
这篇关于动态文本框类型有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!