当消息框显示味精形式关闭时 [英] when messagebox show msg form is closing

查看:54
本文介绍了当消息框显示味精形式关闭时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
当messagebo框显示msg并且我按ok按钮时,窗体关闭
这是我的C#代码

private void btnOk_Click(object sender, EventArgs e)
       {

           if (txtBarCode.Text == "" || txtProductName.Text == "" || txtPrice.Text == "" || numericUpDown1 == null)
           {

               MessageBox.Show("Please Fill it", "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);

               txtBarCode.Focus();

           }

解决方案

请确保未将您的确定"按钮标记为表单中的"CancelButton"(不是按钮) "的属性.
确保按钮的DialogResult属性未设置为取消". (已获得关注).


您的btnOk点击事件中还有其他内容吗?

这是我的猜测.有.它可能看起来像:

 私有  void  btnOk_Click(对象发​​件人,EventArgs e)
{
    如果(txtBarCode.Text == "  || txtProductName.Text == "  || txtPrice.Text ==  " || numericUpDown1 == " " 警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
           txtBarCode.Focus();
    }

    // 更多代码
     .Close();
} 




您正在检查以确保所有内容均已填写,然后如果尚未发送,则发送一条消息说尚未填写,并告诉他们将其填写.

但是随后,您的btnOk click事件的其余部分将运行,并且其中可能存在一个this.Close()事件或关闭该表单的内容.

调用txtBarCode.Focus()不会停止其余代码的执行.您需要在txtBarCode.Focus();之后添加return;或将其余代码放在else块中.

当遇到这样的问题时,请在正在执行的代码之前插入一个断点并逐步执行代码,以查看是否可以找到关闭原因.


Hello people
When messagebo box shows msg and i press ok button form is closing
thi is my c# code

private void btnOk_Click(object sender, EventArgs e)
       {

           if (txtBarCode.Text == "" || txtProductName.Text == "" || txtPrice.Text == "" || numericUpDown1 == null)
           {

               MessageBox.Show("Please Fill it", "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);

               txtBarCode.Focus();

           }

解决方案

Make sure your OK button isn''t marked as the form''s CancelButton in the form''s (not the button''s) properties.
Make sure your button''s DialogResult property isn''t set to Cancel.


It''s more likely that the form loses active focus and it''s behind some other window now (which has gained focus).


is there anything else in your btnOk click event?

Here''s my guess. There is. It probably looks like:

private void btnOk_Click(object sender, EventArgs e)
{
    if (txtBarCode.Text == "" || txtProductName.Text == "" || txtPrice.Text == "" || numericUpDown1 == null)
    {
           MessageBox.Show("Please Fill it", "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
           txtBarCode.Focus();
    }

    //more code
    this.Close();
}




You are checking to make sure that everything has been filled in, and then if it hasn''t you are sending a message saying that it hasn''t and telling them to fill it in.

But then the rest of your btnOk click event runs and there''s probably a this.Close() event in there or something that closes the form.

Calling txtBarCode.Focus() doesn''t halt execution of the rest of your code. You need to add a return; after txtBarCode.Focus(); or put the rest of your code in an else block.

When you get an issue like that, put in a breakpoint before the code that is executing and step-through your code and see if you can find why it is closing.


这篇关于当消息框显示味精形式关闭时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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