简体   繁体   English

当我运行 Windows 窗体应用程序时,为什么我的 if 语句和 MessageBox 没有被点击或显示?

[英]Why aren't my if statements and MessageBox's not getting hit or displayed when I run my Windows form applications?

I've researched several common issue, like the message box displaying behind the form window, trying different wat to call them, however nothing seems to display them.我研究了几个常见问题,例如在表单窗口后面显示的消息框,尝试使用不同的 wat 来调用它们,但似乎没有显示它们。 I'm sure it's something simple I'm missing?我确定这是我想念的简单东西吗?

The application should open a form window with a listbox and some items, if nothing is selected and you click on the button it should display the "Please select and item form the list box" in a message box, but it does not.应用程序应该打开一个带有列表框和一些项目的表单窗口,如果没有选择任何内容并且您单击按钮,它应该在消息框中显示“请从列表框中选择和项目”,但它没有。

Also it should display the "Are you sure you want to close?"它还应该显示“您确定要关闭吗?” message in a box when you x out of the forms window.当您离开表单窗口时,框中的消息。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsDemos
{
    public partial class Dialogs : Form
    {
        public Dialogs()
        {
            InitializeComponent();
        }

        private void Dialogs_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("Oranges");
            listBox1.Items.Add("Grapes");
            listBox1.Items.Add("Bananas");
            listBox1.Items.Add("Peaches");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
            {
                var msg = "Please select an item from the list box";
                MessageBox.Show(msg, this.Text);
                return;
                
            }
            else
            {
                label1.Text = listBox1.Text;
            }
            
        }
        private void Dialogs_FormClosing(object sender, FormClosingEventArgs e)
        {
            var msg = "Are you sure you want to close?";

            if (MessageBox.Show(msg, this.Text, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
    }
}

This should work.这应该有效。 Are you sure you have an event linked to the method?您确定您有与该方法相关联的事件吗?

// in file Dialogs.Designer.cs or in Form Property/Events tool window
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Dialogs_FormClosing);

Instead of having your Dialogs class subscribe to its own events like Load and FormClosing it's cleaner (and may be less error-prone) to simply override the OnLoad and OnFormClosing methods that are causing the events to be fired in the first place.与其让您的Dialogs类订阅其自己的事件(如LoadFormClosing ),不如简单地覆盖导致事件首先被触发的OnLoadOnFormClosing方法,这样更简洁(并且可能更不容易出错)。 I tested this code and it produces the expected outcomes described in your post.我测试了这段代码,它产生了您帖子中描述的预期结果。


public Dialogs()
{
    InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e); // <== The `Load` event you were subscribing to is fired here.
    listBox1.Items.Add("Oranges");
    listBox1.Items.Add("Grapes");
    listBox1.Items.Add("Bananas");
    listBox1.Items.Add("Peaches");

    // Subscribing to the `Click` event here as an
    // alternative to relying on the Designer to do it.
    button1.Click += onButton1Clicked;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
    base.OnFormClosing(e); // <== The `FormClosing` event is fired here.
    var msg = "Are you sure you want to close?";

    if (MessageBox.Show(msg, this.Text, MessageBoxButtons.YesNo) == DialogResult.No)
    {
        e.Cancel = true;
    }
}

I also agree with the other answer posted so far, that the button's Click event doesn't seem to be linked.我也同意到目前为止发布的其他答案,即按钮的Click事件似乎没有链接。 It may be less error prone to subscribe to this event as shown above in the OnLoad method.上面OnLoad方法中所示,订阅此事件可能不太容易出错。

private void onButton1Clicked(object sender, EventArgs e)
{
    if (listBox1.SelectedIndex == -1)
    {
        var msg = "Please select an item from the list box";
        MessageBox.Show(msg, this.Text);
        return;
    }
    else
    {
        label1.Text = listBox1.Text;
    }
}

暂无
暂无

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

相关问题 我在窗体上有一个DataGridView,但在运行窗体时却没有显示。 为什么? - I have a DataGridView on my form that is not being displayed when I run my form. Why? 为什么我的MarshalByRefObject对象没有击中断点? - Why aren't my breakpoints being hit for my MarshalByRefObject object? 为什么我的窗户没有被垃圾收集? - Why aren't my windows getting garbage collected? 为什么我不能在表格中嵌入这些应用程序? - Why can't I embed these Applications in my Form? 为什么我的表单在显示时会调整大小? - Why is my form being resized when it is displayed? 从分支切换到母版时,为什么项目中的文件没有更改? - Why aren't my files in my project changing when I switch from a branch to master? 为什么使用 Selenium C# 清除表单中的字段时,当我点击提交时,我的表单会重新填充数据? - Why when clearing the fields in my form with Selenium C# is my form repopulating with data when I hit submit? 为什么我不能从Windows服务中找到此UNC路径? - Why can't I hit this UNC path from my Windows Service? Visual Studio 2010-我的Windows窗体不会显示,除非我在之前或之后进行MessageBox.Show()调用 - Visual Studio 2010 - my Windows Form doesn't show up unless I make a MessageBox.Show() call before or after 为什么我的RequiredFieldValidators没有停止发布表单? - Why aren't my RequiredFieldValidators stopping the form from posting?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM