简体   繁体   English

当我按下取消按钮时,在 form2 中显示消息框

[英]Showing message box in form2 when i press the cancel button

I have got three forms, of a winform app.我有一个 winform 应用程序的三种形式。 in which form3 there are two butons and some textboxes.其中form3有两个按钮和一些文本框。 I wanted to know why my form is showing a messagepop called "Duplicate" when I press the cancel button of the form.我想知道为什么当我按下表单的取消按钮时,我的表单会显示一个名为“Duplicate”的消息。

I actually told the form3 to cancel and return to the form1.我实际上告诉form3取消并返回到form1。 Its doing the job.它的工作。 but before coming to the form1 it's showing me a messagebox "Duplicate" which I don't want to see.但在进入form1之前,它向我展示了一个我不想看到的消息框“重复”。

How can I avoid this popup?我怎样才能避免这个弹出窗口?

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

Codes in form1表格 1 中的代码

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

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.如果您希望您的代码在单击 Form3 中的取消按钮后不执行代码,一种选择是利用 DialogResult 属性。

Cancel button click handler:取消按钮点击处理程序:

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

Imediately after showing Form3 ("f.ShowDialog();")显示 Form3 后立即 ("f.ShowDialog();")

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

Hope it helps.希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我按下C#中的Form1上的按钮时,如何从Form1内部显示原始Form2 - how would I show the original Form2 from inside Form1 when I press button on Form1 in C# 我如何在Form2上创建查找/搜索按钮以在C#中的Form1上的富文本框中搜索和突出显示文本 - How would I create a find/search button on form2 to search and highlight text in a rich text box on form1 in c# 单击form2按钮时如何停止form1的计时器 - How to stop timer of form1 when form2 button is clicked 我怎么能这样做,当我单击form2上的按钮时,它会影响form1(C#)Winforms上的richtextbox? - How can I make it so that when I click a button on form2, it affects a richtextbox on form1 (C#) Winforms? 当我单击按钮对象时如何显示 form2 的新对象?(C#-Windows 窗体应用程序) - how can when i click the object of button show new object of form2?(C#-Windows form application) 在C#中移动Form2时移动Form1 - Move Form1 when I move Form2 in C# 当我按下关闭按钮时,表单正在关闭而不是执行功能 - Form is closing instead of performing function when i press close button 如果已经加载了form2,则更改了form1中的值,我正在尝试刷新form2中的数据 - I am trying to refresh the data in form2 if the value from form1 is changed when the form2 is already loaded 在Form2上调用函数时,从Form1上的文本框中获取文本 - Get Text from text box on Form1 when calling function on Form2 从form1单击按钮时从form2调用按钮 - Calling a button from form2 when clicking a button from form1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM