-1

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

Fullsailer
  • 13
  • 3
  • 3
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Jul 14 '22 at 16:18
  • 2
    A lot of times, it's just that you haven't connected event code with the desired event in winforms, have checked that? For example, click on your button_1, select events, and check if you matched event code with click event – TDiblik Jul 14 '22 at 16:18

2 Answers2

0

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);
Petr Voborník
  • 1,249
  • 1
  • 14
  • 11
  • Petr, I checked it and there was already an event linked method in my Designer.cs, it's also not displaying my empty button click. I have set break point to debug it but the code doesn't hit my if statement. It will run and select a item from my list box and display it in my label but it will not display a message box??? – Fullsailer Jul 14 '22 at 18:04
  • This could also be because there is a bug in the code somewhere. When it finds it when you run the program, it offers to run the last working version of the application, not actual. If you click to yes, it then runs it until the bug is fixed. Isn't there something in the bug list? – Petr Voborník Jul 14 '22 at 18:22
0

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;
    }
}
IVSoftware
  • 5,732
  • 2
  • 12
  • 23