代码如下:namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
ck.S();
}
} public class ck
{
static public void S()
{
Form1 f1 = new Form1();
int K = Convert.ToInt16(f1.textBox1.Text) + 1;
f1.textBox1.Text = K.ToString(); } }
}
每次窗体初始化后 textbox1.text 都是空值,运算报错。
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
ck.S();
}
} public class ck
{
static public void S()
{
Form1 f1 = new Form1();
int K = Convert.ToInt16(f1.textBox1.Text) + 1;
f1.textBox1.Text = K.ToString(); } }
}
每次窗体初始化后 textbox1.text 都是空值,运算报错。
去掉 S 函数中 Form1 f1 = new Form1();
在 Convert.ToInt16(f1.textBox1.Text) 执行前加一个 判断 if( f1.textBox1.Text !="")
如何?
=〉
int K = 1;
try { K = Convert.ToInt16(f1.textBox1.Text) + 1; } catch { }
{
ck.S(this);
}==================
static public void S(Form f1)
{
try
{
int K = Convert.ToInt16(f1.textBox1.Text) + 1;
f1.textBox1.Text = K.ToString();
}catch{} }