public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string a = textBox1.Text;
int result;
bool bl = int.TryParse(textBox2.Text, out result);
if (!bl)
{
throw new YC();
}
else
{
int b = int.Parse(textBox2.Text);
bool bl1 = ceshi(a, b);
if (bl1)
{
// MessageBox.Show("CHENGONG");
this.Close();
}
else
{
MessageBox.Show("SH");
}
}
}
catch (YC yc)
{
MessageBox.Show(yc.ToString());
}
//string a = textBox1.Text;
//int b= int.Parse(textBox2.Text);
//bool bl = ceshi(a, b);
//if (bl)
//{
// MessageBox.Show("CHENGGONG");
//}
//else
//{
// MessageBox.Show("SHIBAI");
//}
finally
{
}
}
public bool ceshi(string a, int b)
{
string connectionstring = "server=localhost;database=ceshi;integrated security=SSPI";
SqlConnection con = new SqlConnection(connectionstring);
con.Open();
//string commandtext = "select * from Table_1 where name='" + a + "'and passward=" + b;
//string @name;
//string @passward;
string commandtext = "select * from Table_1 where name=@name and passward = @passward";
SqlCommand cmd = new SqlCommand(commandtext, con);
//cmd.Parameters.Add("@name", SqlDbType.VarChar);
//cmd.Parameters["@name"].Value = "a";
cmd.Parameters.AddWithValue("@name", a);
cmd.Parameters.Add("@passward", SqlDbType.Int);
cmd.Parameters["@passward"].Value = b;
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
return true;
}
else
{
return false;
}
con.Close();
}
private void Form2_Load(object sender, EventArgs e)
{
if (textBox1.CanFocus)
{
textBox1.Focus();
}
}
}
class YC : ApplicationException
{
private string Message;
public YC()
{
this.Message = "输入有误,请重新输入";
}
public override string ToString()
{
return Message;
}
}
这段代码运行起来之后,光标并没有在textbox1中
把tabindex 改成0试下
支持楼上的做法
if (textBox1.CanFocus)
{
textBox1.Select(); //加这句试试,有时候.Focus()会莫名其妙般无作用。
textBox1.Focus();
}