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