【问题标题】:Showing message box in form2 when i press the cancel button当我按下取消按钮时,在 form2 中显示消息框
【发布时间】:2021-04-10 13:02:01
【问题描述】:

我有三个表单,一个 winform 应用程序。其中form3有两个按钮和一些文本框。我想知道为什么当我按下表单的取消按钮时,我的表单会显示一个名为“重复”的消息。

我实际上是告诉form3取消并返回form1。它在做这项工作。但在进入 form1 之前,它向我显示了一个我不想看到的消息框“重复”。

我怎样才能避免这个弹出窗口?

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

【问题讨论】:

  • 上面的代码对我来说看起来不错,checkexistornot() 方法中只有一个问题是cmd.ExecuteNonQuery() 但是,它与您的问题无关。我需要知道您在表单中的哪些地方调用checkexistornot() 方法。然后再添加cell_check() 代码。
  • @Shell stackoverflow.com/questions/25468109/…。您已经为这项权利提供了解决方案。现在我想知道.. 你能帮我吗.. 就像我创建一个新文件名.. 假设旧文件名叫做sample,我已经按照你说的附加并移动了。下次如果用户想要更新到 sample-rev-01 而不是 sample 我想将旧附件从 column2 移动到 `c:\elec\old` 中的不同文件夹并将新附件从 column1 更新到 column2 .你能帮帮我吗??????

标签: c# datagridview


【解决方案1】:

如果您希望您的代码在单击 Form3 中的取消按钮后不执行代码,则一种选择是利用 DialogResult 属性。

取消按钮点击处理程序:

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

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

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-06
    • 2021-05-07
    • 2020-06-02
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多