简体   繁体   中英

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. 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. 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. It may be less error prone to subscribe to this event as shown above in the OnLoad method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM