0% found this document useful (0 votes)
16 views

Lab 4

This document summarizes a Windows Forms project that builds an employee information form using advanced controls. It describes the controls that will be used, including masked textboxes, multiline textboxes, combo boxes, list boxes, checkboxes, date/time pickers and numeric up/downs. It provides instructions for adding items to the controls, setting properties like auto-complete and formatting, and handling events for the form's buttons.

Uploaded by

lhtduong2100146
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab 4

This document summarizes a Windows Forms project that builds an employee information form using advanced controls. It describes the controls that will be used, including masked textboxes, multiline textboxes, combo boxes, list boxes, checkboxes, date/time pickers and numeric up/downs. It provides instructions for adding items to the controls, setting properties like auto-complete and formatting, and handling events for the form's buttons.

Uploaded by

lhtduong2100146
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Windows Forms for Beginners

Part 3

Demo Projects:

• Employee Information Form (Using advance controls)

What will you learn?

• Using following controls:


o Masked Textbox
o Multiline Textbox
o ComboBox, ListBox
ListBox, CheckBoxList
o DateTimePicker
o NumericUpDown
o RadioButton
• Adding Items to ComboBox
ComboBox, ListBox and CheckBoxList Statically and
Dynamically
• Enabling Autocomplete feature in ComboBox
• Getting Selected items form ComboBox, ListBox and CheckBoxList
• Clearing or resetting all these controls to default values.

Bhushan Mulmule
[email protected]
www.dotnetvideotutorial.com

www.dotnetvideotutorial.com
Project 1: Employee Information Form
Using Advance Controls TextBox:
Step 1: Design UI: Refer instructions below for details Name: txtDetails
MultiLine: True
MaskTextBox:
Name: mskTxtEmpNo
Mask: 0000

TextBox:
Name: txtName

TextBox:
Name: txtAddress
MultiLine: True

ComboBox:
Name: cmbCity
Items: Nagpur, Pune,
Mumbai, Banglore, Nanded
,Nainital.
AutoCompliteSource:
ListItems
AutoCompliteMode:
Suggest

ListBox:
Name: lstState

DateTimePicker:
Name: dtpDOB
RadioButtons:
Format: Custom
Name: radMale, radFemale
CustomFormat: dd/MM/yyyy

NumbericUpDown: CheckBoxList: 3 Buttons:


Name: nudAge Name: chkLstHobbies Name: btnReset, btnOK,
Minimum: 18 Items: Singing, Dancing, btnExit.
Maximum: 60 Panting, Shopping, Text: Reset, OK, Exit
Gardening, Swimming,
Sleeping
MultiColumn: True
CheckOnClick: True
www.dotnetvideotutorial.com
Instructions:

• MaskedTextBox allows defining format for input using Mask property.


o Mask = 0000: To make it compulsory to user to enter 4 digit employee
number.
• Multiline property of textbox need to be set to true in order to enter multiple
lines in textbox
• ComboBox allows user to select item from predefined list
o Items property holds multiple items. When you will click on Items
property Collection Editor Window will open where you can add multiple
items (one item per line)
o AutoCompleteSource and AutoCompleteMode enables AutoComplete
feature of combobox. So that user can type in ComboBox and can select
item from matching suggestion list.
• ListBox is same as ComboBox except it displays multiple items as list.
o Items property can be used to populate control same as ComboBox.
o In this example we are populating it dynamically by writing function
PopulateStates and calling it from Form_Load Event
• DateTimePicker enables user to select date visually.
o Format property provides various predefined formats for date
o CustomFormat property allows to define custom format.
o To use CustomFormat; Format property need to set to CustomFormat
• NumericUpdown enables user to select numeric value from predefined range
o Minimum and Maximum properties need to be set
• Radio buttons for gender has to be in group box to specify that they are in same
group and only one should get selected out of them.
• CheckBoxList allows to select multiple items easily from predefined list
o We can populate it statically using item property or dynamically using
code as for ListBox.
o We will populate it using items property
o We will also
• Details textbox is nothing but textbox with multiline property set to true.
o To show all the details on this textbox first we will collect all the
information in string and then will assign it to details textbox.

www.dotnetvideotutorial.com
o Note: Strings are immutable and creates new object with every
concatenation. So this can be done better using StringBuilder Class.
(When you need to concatenate string number of times you should use
StringBuilder Class) Read about it and try to implement same thing using
StringBuilder class after finishing this walkthrough.

Step 3: Add event handlers for buttons

1. Double Click on Form to go to code window. Create function PopulateStates


outside form_load event

private void PopulateStates()


{
lstStates.Items.Add("Maharashtra");
lstStates.Items.Add("MP");
lstStates.Items.Add("Punjab");
lstStates.Items.Add("Karnataka");
lstStates.Items.Add("Goa");
}

2. Call PopulateSates from Form_Load Event

private void frmEmployeeDetails_Load(object sender, EventArgs e)


{
PopulateStates();
}

3. Double click on OK button:

private void btnOK_Click(object sender, EventArgs e)


{
string details;
details = "EmpNo: " + mskTxtEmpNo.Text;
details += "\r\nName: " + txtName.Text;
details += "\r\nAddress: " + txtAddress.Text;
details += "\r\nCity: " + cmbCity.SelectedItem;
details += "\r\nState: " + lstState.SelectedItem;
details += "\r\nDOB: " + dtpDOB.Value.ToShortDateString();
details += "\r\nAge: " + numAge.Value.ToString();

string gender = radMale.Checked ? "Male" : "Female";


details += "\r\nGender: " + gender;

www.dotnetvideotutorial.com
string hobbies="";
foreach (string h in chkLstHobbies.CheckedItems)
hobbies += h + "\r\n\t";
details += "\r\nHobbies: " + hobbies;

txtDetails.Text = details;
}

4. Reset button code:

private void btnReset_Click(object sender, EventArgs e)


{
mskTxtEmpNo.Text = "";
txtName.Text = "";
txtAddress.Text = "";
cmbCity.SelectedIndex = -1;
lstState.SelectedIndex = -1;
dtpDOB.Value = DateTime.Now;
numAge.Value = numAge.Minimum;
radMale.Checked = false;
radFemale.Checked = false;
//unchek all items
for (int i = 0; i < chkLstHobbies.Items.Count; i++)
chkLstHobbies.SetItemChecked(i, false);
//removes blue selection
chkLstHobbies.ClearSelected();
txtDetails.Text = "";
mskTxtEmpNo.Focus();
}

www.dotnetvideotutorial.com

You might also like