首页 新闻 会员 周边

关于Form的show()方法的若干问题

0
[待解决问题]

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();
                       Form3 form3=new Form3();
                       form3.Show();

                    }
                    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();

        }

     
    }

    class YC : ApplicationException
    {
        private string Message;
        public YC()
        {

            this.Message = "输入有误,请重新输入";
        }

        public override string ToString()
        {
            return Message;
        }

    }以上代码是Form2中的代码,在设置断点调试过程中弹出了Form3窗体,继续调试后会运行到Form3的方法dispose(),但是没有设置断点时没有弹出Form3,不知道什么原因, 请高手解答一下

以下是Form3中的dispose()方法:

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
870087629的主页 870087629 | 初学一级 | 园豆:170
提问于:2010-09-26 09:12
< >
分享
所有回答(2)
0

主窗体都关闭了,还能显示.

同一线程

.NET快速开发框架 | 园豆:946 (小虾三级) | 2010-09-26 10:40
0

我猜测应该是这样的,你的form2是主窗体吧。主窗体所在的线程完蛋了,其它子线程就都完蛋了,所以也不会再弹出子窗体,那么至于为什么设置了断点就会弹出。我猜测是设置了断点后导致系统无法马上销毁主线程,主线程被你设置的断点暂停了,而子线程仍在继续,所以就弹出了子窗体(show方法是在子线程中代开窗体的)了。如果你把Show换成ShowDialog的话,设置了断点也不会弹出了。不知道你想实现什么功能。可以先调用Form3的showDialog(),再调用Form2的close();

会长 | 园豆:12459 (专家六级) | 2010-09-26 11:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册