2. Message Boxes
• A message box is a special dialog box used to display a piece of information to user.
– MessageBox class
– EX:
MessageBox.Show("Hello, how are you?","My SMS");
MessageBox.Show("Welcome to the Wonderful World of Visual C#","Visual C#
Tutorials", MessageBoxButtons.OKCancel);
MessageBox.Show("Your order appears to be correct" +
"nAre you ready to provide your credit card information?",
"Customer Order Processing",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information);
3. EX:
MessageBox.Show("Your order appears to be correct" +
"nAre you ready to provide your credit card information?",
"Customer Order Processing",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
NOTE:Button1 , Button2 , Button3
12. 1. Adding Menu Items
2. Creating Checked Menu Items
3. Assigning Shortcut Keys
4. Using the Toolbar Control
13. Creating MDI Forms
All the projects you've created so far have been single-document interface (SDI) projects. In SDI
programs, every form in the application is a peer of all other forms; no intrinsic hierarchy exists
between forms. C# also lets you create multiple-document interface (MDI) programs. A MDI
program contains one parent window (also called a container) and one or more child windows.
A classic example of a MDI program is Microsoft Word 95 (200 behaves slightly different,
depending on how it's set up).
if (MessageBox.Show("Would you like to do X?", "MessageBoxsample", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.Close();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = DateTime.Now.ToString();
this.rSum.Checked = true;
}
private void rSum_CheckedChanged(object sender, EventArgs e)
{
this.lbOp.Text = "+";
this.btOP.Text = "+";
}
private void rMinus_CheckedChanged(object sender, EventArgs e)
{
this.lbOp.Text = "-";
this.btOP.Text = "-";
}
private void rMult_CheckedChanged(object sender, EventArgs e)
{
this.lbOp.Text = "*";
this.btOP.Text = "*";
}
private void rDiv_CheckedChanged(object sender, EventArgs e)
{
this.lbOp.Text = "/";
this.btOP.Text = "/";
}
private void btOP_Click(object sender, EventArgs e)
{
Random rnd = new Random();
this.lbV1.Text = (rnd.Next(100)).ToString();
this.lbV2.Text = (rnd.Next(100)).ToString();
switch (this.lbOp.Text)
{
case "+":
lbResult.Text = (float.Parse(lbV1.Text) + float.Parse(lbV2.Text)).ToString();
break;
case "-":
lbResult.Text = (float.Parse(lbV1.Text) - float.Parse(lbV2.Text)).ToString();
break;
case "*":
lbResult.Text = (float.Parse(lbV1.Text) * float.Parse(lbV2.Text)).ToString();
break;
case "/":
lbResult.Text = (float.Parse(lbV1.Text) / float.Parse(lbV2.Text)).ToString();
break;
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void cmdOK_Click(object sender, EventArgs e)
{
string output;
output="ID: " + txtID.Text + "\r\n";
output+= "Name: " + txtName.Text + "\r\n";
output+="Gender: " + txtGender.Text + "\r\n";
output+="DOB:" + txtDOB.Text + "\r\n";
output += "Address:" + txtGender.Text + "\r\n____________________________________\r\n";
txtOUTPUT.Text =txtOUTPUT.Text + output;
}
private void cmdHELP_Click(object sender, EventArgs e)
{
string output;
output = "ID= Your ID\r\n";
output +="Name = Your name\r\n";
output +="Sex = Your sex\r\n";
output += "Address = Your address\r\n";
output += "DOB = Your date of birth 'dd/mm/yyyy'\r\n";
// Insert the new text.
this.txtOUTPUT.Text = output;
}
}
}
//_____________________________________
private Boolean isDate(String st)
{
Boolean b = true;
try
{
DateTime dt = DateTime.Parse(st);
}
catch
{
return false;
}
return true;
}
private void textBox1_Leave(object sender, EventArgs e)
{
if (isDate(this.textBox1.Text)==false)
{
MessageBox.Show("Please, enter only date!");
this.textBox1.Focus();
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.oFD.Filter = "Windows Bitmaps|*.BMP|JPEG Files|*.JPG";
}
private void btSelect_Click(object sender, EventArgs e)
{
oFD.InitialDirectory = @"C:\";
oFD.Title = "Select a File";
this.oFD.FileName = "";
if (this.oFD.ShowDialog() == DialogResult.OK)
{
this.pBox.Image = Image.FromFile(oFD.FileName);
this.Text = String.Concat("Picture Viewer (" + oFD.FileName + ")");
this.cboPath.Items.Add(oFD.FileName);
}
}
private void btQuit_Click(object sender, EventArgs e)
{
this.Close();
}
private void cboPath_SelectedIndexChanged(object sender, EventArgs e)
{
this.pBox.Image = Image.FromFile(cboPath.Text);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btEnlarge_Click(object sender, EventArgs e)
{
this.Width = this.Width + 20;
this.Height = this.Height + 20;
}
private void btShrink_Click(object sender, EventArgs e)
{
this.Width = this.Width - 20;
this.Height = this.Height - 20;
}
private void btObject_Click(object sender, EventArgs e)
{
for (int intIndex = 0; intIndex < this.Controls.Count;intIndex++)
{
MessageBox.Show("Control # " + intIndex.ToString() +" has the name " + this.Controls[intIndex].Name);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _5
{
public partial class Form1 : Form
{
public static string opt;
public static string u1;
public static string u2;
public Form1()
{
InitializeComponent();
}
private static double operate(string v1, string v2, string opt)
{
double result=0;
switch (opt)
{
case "+":
result = double.Parse(v1) + double.Parse(v2);
break;
case "-":
result = double.Parse(v1) - double.Parse(v2);
break;
case "*":
result = double.Parse(v1) * double.Parse(v2);
break;
case "/":
result = double.Parse(v1) - double.Parse(v2);
break;
case "Min":
result = (double.Parse(v1)> double.Parse(v2))?double.Parse(v2):double.Parse(v1);
break;
case "Max":
result = (double.Parse(v1)> double.Parse(v2))?double.Parse(v1):double.Parse(v2);
break;
}
return result;
}
private void rBSum_CheckedChanged(object sender, EventArgs e)
{
if (this.rBSum.Checked)
{
if (this.txtVal1.Text == "")
{
MessageBox.Show("សូមវាតតំលៃទី១");
this.txtVal1.Focus();
}
else if (this.txtVal2.Text == "")
{
MessageBox.Show("សូមវាតតំលៃទី២");
this.txtVal2.Focus();
}
else
{
opt = "+";
this.txtResult.Text = (operate(this.txtVal1.Text, this.txtVal2.Text, opt)).ToString();
}
}
}
private void rBMinus_CheckedChanged(object sender, EventArgs e)
{
if (this.rBMinus.Checked)
{
if (this.txtVal1.Text == "")
{
MessageBox.Show("សូមវាតតំលៃទី១");
this.txtVal1.Focus();
}
else if (this.txtVal2.Text == "")
{
MessageBox.Show("សូមវាតតំលៃទី២");
this.txtVal2.Focus();
}
else
{
opt = "-";
this.txtResult.Text = (operate(this.txtVal1.Text, this.txtVal2.Text, opt)).ToString();
}
}
}
private void rbMult_CheckedChanged(object sender, EventArgs e)
{
if (this.rbMult.Checked)
{
if (this.txtVal1.Text == "")
{
MessageBox.Show("សូមវាតតំលៃទី១");
this.txtVal1.Focus();
}
else if (this.txtVal2.Text == "")
{
MessageBox.Show("សូមវាតតំលៃទី២");
this.txtVal2.Focus();
}
else
{
opt = "*";
this.txtResult.Text = (operate(this.txtVal1.Text, this.txtVal2.Text, opt)).ToString();
}
}
}
private void rBDiv_CheckedChanged(object sender, EventArgs e)
{
if (this.rBDiv.Checked)
{
if (this.txtVal1.Text == "")
{
MessageBox.Show("សូមវាតតំលៃទី១");
this.txtVal1.Focus();
}
else if (this.txtVal2.Text == "")
{
MessageBox.Show("សូមវាតតំលៃទី២");
this.txtVal2.Focus();
}
else
{
opt = "/";
this.txtResult.Text = (operate(this.txtVal1.Text, this.txtVal2.Text, opt)).ToString();
}
}
}
private void rbMin_CheckedChanged(object sender, EventArgs e)
{
if (this.rbMin.Checked)
{
if (this.txtVal1.Text == "")
{
MessageBox.Show("សូមវាតតំលៃទី១");
this.txtVal1.Focus();
}
else if (this.txtVal2.Text == "")
{
MessageBox.Show("សូមវាតតំលៃទី២");
this.txtVal2.Focus();
}
else
{
opt = "Min";
this.txtResult.Text = (operate(this.txtVal1.Text, this.txtVal2.Text, opt)).ToString();
}
}
}
private void rBMax_CheckedChanged(object sender, EventArgs e)
{
if (this.rBMax.Checked)
{
if (this.txtVal1.Text == "")
{
MessageBox.Show("សូមវាតតំលៃទី១");
this.txtVal1.Focus();
}
else if (this.txtVal2.Text == "")
{
MessageBox.Show("សូមវាតតំលៃទី២");
this.txtVal2.Focus();
}
else
{
opt = "Max";
this.txtResult.Text = (operate(this.txtVal1.Text, this.txtVal2.Text, opt)).ToString();
}
}
}
private void chClear_CheckedChanged(object sender, EventArgs e)
{
if (this.chClear.Checked)
{
u1 = this.txtVal1.Text;
u2 = this.txtVal2.Text;
this.txtVal1.Text = "";
this.txtVal2.Text = "";
this.txtResult.Text = "";
}
}
private void chUndo_CheckedChanged(object sender, EventArgs e)
{
if (chUndo.Checked)
{
this.txtVal1.Text = u1;
this.txtVal2.Text = u2;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btSearch_Click(object sender, EventArgs e)
{
Boolean b=false;
for (int i = 0; i < lstProduct.Items.Count; i++)
{
if (lstProduct.Items[i].ToString() == (txtProID.Text + "\t" + txtProName.Text + "\t" + txtQty.Text + "\t" + txtUnitPrice.Text + "\t" + double.Parse(txtQty.Text) * double.Parse(txtUnitPrice.Text)))
{
lstProduct.Focus();
lstProduct.SelectedIndex=i ;
b = true;
return;
}
}
if (b == false)
{
MessageBox.Show("រកមិនឃើញទេ!");
}
}
private void btInsert_Click(object sender, EventArgs e)
{
this.lstProduct.Items.Add(txtProID.Text + "\t" + txtProName.Text + "\t" + txtQty.Text + "\t" + txtUnitPrice.Text + "\t" + double.Parse(txtQty.Text)*double.Parse(txtUnitPrice.Text));
}
private void btDelete_Click(object sender, EventArgs e)
{
if (this.lstProduct.SelectedIndex!=-1)
{
//this.lstProduct.Items.Remove(this.lstProduct.Text);
this.lstProduct.Items.RemoveAt(this.lstProduct.SelectedIndex);
}
}
private static string IdProduct()
{
return "";
// for(int i=0;i<
}
private void btNew_Click(object sender, EventArgs e)
{
txtProID.Text = "";
txtProName.Text = "";
txtQty.Text = "";
txtUnitPrice.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
this.lstProduct.Sorted = true;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void TM_Tick(object sender, EventArgs e)
{
this.lbDay.BackColor = Color.AliceBlue;
DateTime objToday = DateTime.Now;
lbDay.Text = (objToday.DayOfWeek).ToString();
lbTime.Text = "Time: " + objToday.ToLongTimeString();
lbMonth.Text ="Month: " + (objToday.Month).ToString();
lbD.Text = "Day: " + (objToday.Day).ToString();
lbYear.Text = (objToday.Year).ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btResult1_Click(object sender, EventArgs e)
{
int i=0;
int s=0;
while(i < 10)
{
i = i + 1;
s = s + i;
}
this.txtResult1.Text = s.ToString();
}
private void btResult2_Click(object sender, EventArgs e)
{
int i = 1;
int s = 0;
while (i < 10)
{
s = s + i;
i = i + 2;
}
this.txtResult2.Text = s.ToString();
}
private void btResult3_Click(object sender, EventArgs e)
{
int i = 2;
int s = 0;
while (i <= 10)
{
s = s + i;
i = i + 2;
}
this.txtResult3.Text = s.ToString();
}
private void tabControl1_MouseClick(object sender, MouseEventArgs e)
{
int i = 0;
double rndNum = 0;
Random rnd = new Random();
do
{
rndNum = rnd.Next(10) + 1;
cboValue.Items.Add(Math.Round(rndNum));
i = i + 1;
} while (i < 10);
}
private void btMin_Click(object sender, EventArgs e)
{
int i=0;
int min=int.Parse(cboValue.Items[0].ToString());
do
{
if(min>int.Parse(cboValue.Items[i].ToString()))
{
min=int.Parse(cboValue.Items[i].ToString());
}
i=i+1;
}while(i<cboValue.Items.Count);
txtMin.Text=min.ToString();
}
private void btMax_Click(object sender, EventArgs e)
{
int i = 0;
int max = int.Parse(cboValue.Items[0].ToString());
do
{
if (max < int.Parse(cboValue.Items[i].ToString()))
{
max = int.Parse(cboValue.Items[i].ToString());
}
i = i + 1;
} while (i < cboValue.Items.Count);
txtMax.Text = max.ToString();
}
private void btRnd_Click(object sender, EventArgs e)
{
cboValue.Items.Clear();
int i = 0;
double rndNum = 0;
Random rnd = new Random();
do
{
rndNum = rnd.Next(10) + 1;
cboValue.Items.Add(Math.Round(rndNum));
i = i + 1;
} while (i < 10);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.cboGender.Items.Add("Male");
this.cboGender.Items.Add("Female");
this.cboGender.SelectedIndex = 0;
lvwCustomer.View = View.Details;
lvwCustomer.Columns.Add("CustID", 120, HorizontalAlignment.Left);
ColumnHeader colCustName = new ColumnHeader();
colCustName.Text = "CustName";
colCustName.Width = 120;
colCustName.TextAlign = HorizontalAlignment.Right;
ColumnHeader colGender = new ColumnHeader();
colGender.Text = "Gender";
colGender.Width = 80;
colGender.TextAlign = HorizontalAlignment.Right;
ColumnHeader colDOB = new ColumnHeader();
colDOB.Text = "Date of birth";
colDOB.Width = 120;
colDOB.TextAlign = HorizontalAlignment.Right;
ColumnHeader colAddress = new ColumnHeader();
colAddress.Text = "Address";
colAddress.Width = 200;
colAddress.TextAlign = HorizontalAlignment.Right;
ColumnHeader colPhoto = new ColumnHeader();
colPhoto.Text = "Path";
colPhoto.Width = 200;
colPhoto.TextAlign = HorizontalAlignment.Left;
ColumnHeader[] cols = {colCustName , colGender,colDOB,colAddress,colPhoto};
lvwCustomer.Columns.AddRange(cols);
}
private void btClear_Click(object sender, EventArgs e)
{
this.txtCustID.Text = "";
this.txtCustName.Text = "";
this.txtAddress.Text = "";
this.txtDOB.Text = "";
}
private static string path;
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
oFD.InitialDirectory = @"C:\";
oFD.Title = "Select a File";
this.oFD.FileName = "";
if (this.oFD.ShowDialog() == DialogResult.OK)
{
this.pB.Image = Image.FromFile(oFD.FileName);
path = oFD.FileName;
}
}
private void btInsert_Click(object sender, EventArgs e)
{
ListViewItem lvicust = lvwCustomer.Items.Add(this.txtCustID.Text);
lvicust.SubItems.Add(this.txtCustName.Text);
lvicust.SubItems.Add(this.cboGender.Text);
lvicust.SubItems.Add(this.txtDOB.Text);
lvicust.SubItems.Add(this.txtAddress.Text);
lvicust.SubItems.Add(path);
}
private void btDelete_Click(object sender, EventArgs e)
{
int i;
Boolean b=false;
for (i = lvwCustomer.Items.Count-1; i >= 0; i--)
{
if (lvwCustomer.Items[i].Selected == true)
{
lvwCustomer.Items.RemoveAt(i);
b = true;
}
}
if (b == false)
{
MessageBox.Show("Cannot delete, no items te");
}
}
private void lvwCustomer_Click(object sender, EventArgs e)
{
int i;
for (i = lvwCustomer.Items.Count - 1; i >= 0; i--)
{
if (lvwCustomer.Items[i].Selected == true)
{
txtCustID.Text = lvwCustomer.Items[i].Text;
txtCustName.Text = lvwCustomer.Items[i].SubItems[1].Text;
cboGender.Text = lvwCustomer.Items[i].SubItems[2].Text;
txtDOB.Text = lvwCustomer.Items[i].SubItems[3].Text;
txtAddress.Text = lvwCustomer.Items[i].SubItems[4].Text;
this.pB.Image = Image.FromFile(lvwCustomer.Items[i].SubItems[5].Text);
break;
}
}
}
private void lvwCustomer_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void pB_Click(object sender, EventArgs e)
{
}
private void btUpdate_Click(object sender, EventArgs e)
{
int i;
Boolean b = false;
for (i = lvwCustomer.Items.Count - 1; i >= 0; i--)
{
if (lvwCustomer.Items[i].Selected == true)
{
lvwCustomer.Items[i].SubItems[1].Text = this.txtCustName.Text;
lvwCustomer.Items[i].SubItems[2].Text = this.cboGender.Text;
lvwCustomer.Items[i].SubItems[3].Text = this.txtDOB.Text;
lvwCustomer.Items[i].SubItems[4].Text = this.txtAddress.Text;
lvwCustomer.Items[i].SubItems[5].Text = path;
b = true;
}
}
if (b == false)
{
MessageBox.Show("Cannot Update te");
}
}
private void btSearch_Click(object sender, EventArgs e)
{
int i;
Boolean b = false;
for (i = lvwCustomer.Items.Count - 1; i >= 0; i--)
{
if (lvwCustomer.Items[i].Text == this.txtCustID.Text)
{
this.txtCustName.Text = lvwCustomer.Items[i].SubItems[1].Text;
this.cboGender.Text = lvwCustomer.Items[i].SubItems[2].Text;
this.txtDOB.Text=lvwCustomer.Items[i].SubItems[3].Text;
this.txtAddress.Text=lvwCustomer.Items[i].SubItems[4].Text;
this.pB.Image =Image.FromFile(lvwCustomer.Items[i].SubItems[5].Text);
b = true;
}
}
if (b == false)
{
MessageBox.Show("Cannot Update te");
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!closeToolStripMenuItem.Checked)
{
if (MessageBox.Show("Do you really wish to exit?","QuitVerification",MessageBoxButtons.YesNo) ==DialogResult.No)
return;
}
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
closeToolStripMenuItem.Checked = (!closeToolStripMenuItem.Checked);
}
}
}