当我按取消按钮时,在form2中显示消息框 [英] Showing message box in form2 when i press the cancel button

查看:56
本文介绍了当我按取消按钮时,在form2中显示消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三种形式的Winform应用程序。在哪个form3中,有两个按钮和一些文本框。我想让
知道为什么当我按下表单的取消按钮时,表单显示一个名为 Duplicate的消息弹出窗口。
我实际上告诉form3取消并返回到form1。它的工作。但是在进入form1
之前,它向我显示了一个我不想看到的消息框 Duplicate。我如何避免此弹出窗口。请指导我
谢谢我提前

I have got three forms, of a winform app. in which form3 there are two butons and some textboxes. I wanted to know why my form is showing a messagepop called "Duplicate" when i press the cancel button of the form. I actually told the form3 to cancel and return to the form1. Its doing the job. but before coming to the form1 its showing me a messagebox "Duplicate" which i dont want to see. How can i avoid this popup. Please guide me Thanks i advance

form3中的代码

   private void submit_Click(object sender, EventArgs e)
    {

        // this button click event handler will raise the 
        // event which can then intercepted by any listeners
    //some codes....
        this.Dispose();
    }

private void cancel_Click(object sender, EventArgs e)
    {
        this.Close();
    }

form1中的代码

private void btn_open_form3_Click(object sender, EventArgs e)
    {
        Form3 f = new Form3();
        string l = "true";
        // Add an event handler to update this form
        // when the address form is updated (when AddressUpdated fires).
        f.AddressUpdated += new Form3.AddressUpdateHandler(AddressForm_ButtonClicked);

        f.ShowDialog();
        if (String.IsNullOrEmpty(file_NameTextBox.Text.ToString()))
        {
            frmShowMessage.Show("Try again!!!", "Missing!!!!", enumMessageIcon.Error, enumMessageButton.OK);
            //cell_check();
        }
        else
        {
            if (checkexistornot())
            {
                if (file_NameTextBox.Text == string.Empty)
                {
                    cell_check();
                }

                else
                {
                    SqlConnection con = new SqlConnection(@"Data Source=DVSQL\SQLEXPRESS;Initial Catalog=CncDB;Persist Security Info=True;User ID=CncDbUser;Password=*****");
                    con.Open();
                    SqlCommand cmd = new SqlCommand(@"INSERT INTO cncinfo (part,drawings,draftpath,comments) VALUES ('" + file_NameTextBox.Text + "','" + drawingsTextBox.Text + "','" + gcodeTextBox.Text + "','" + commentsTextBox.Text + "') ", con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    load_table();
                }
            }
        }
    }

    private void AddressForm_ButtonClicked(object sender, AddressUpdateEventArgs e)
    {
        // update the forms values from the event args
        file_NameTextBox.Text = e.File_Name;
        drawingsTextBox.Text = e.Drawings;
        gcodeTextBox.Text = e.Gcode;
        commentsTextBox.Text = e.Comments;
    }

    public bool checkexistornot()
    {
        SqlConnection con = new SqlConnection(@"Data Source=DVSQL\SQLEXPRESS;Initial Catalog=CncDB;User ID=CncDbUser;password=*****");
        con.Open();
        SqlCommand cmd = new SqlCommand("select part from cncinfo where part='" + this.file_NameTextBox.Text + "'  ;", con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Add("@part", SqlDbType.NVarChar).Value = Convert.ToString(dataGridView1.Rows[0].Cells["Part Number"].Value);
        object obj = cmd.ExecuteScalar();
        cmd.ExecuteNonQuery();
        con.Close();
        if (obj!=null)
        {
            frmShowMessage.Show(""Duplicate!!!!", enumMessageIcon.Warning, enumMessageButton.OK);//- - - ->>showing this line if i press the cancel_Click from FORM3
            cell_check();
            return false;
        }
        else  
            return true;
    }


推荐答案

如果您希望代码在单击在Form3中单击取消按钮,一个选项是利用DialogResult属性。

If you want your code to not execute code after clicking the cancel button in Form3, one option is taking advantage of the DialogResult property.

取消按钮单击处理程序:

private void cancel_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.Cancel;
    this.Close();
}

在显示Form3之后立即显示( f.ShowDialog( );)

if (f.DialogResult == DialogResult.Cancel)
{
    return;
}

希望有帮助。

这篇关于当我按取消按钮时,在form2中显示消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆