This is the companion webpage to the WinForms Name Lookup (http://youtu.be/xSNa3MPOtns) video on
the SkyCoder Channel (www.youtube.com/c/SkycoderChannel).
The first thing I like to do when writing a WinForms app is
to lay out the form the way I want it to look and make sure the eye candy is
all good. In this application, I set the Anchor property of the Get Names
button to hug the bottom left of the form, and the DataGridView to size with
the form by setting Top, Left, Bottom and Right.
Next, since this is a WinForms app, my protocol is to use
'Swedish notation,' which is where you prefix the controls with a tag
indication the type of control. So for example I used frmMain for the form,
btnGetNames for the button and so on.
Just to make things look a little nicer, I added an icon for
both the form and project. This is done by setting the Icon property in the form and the Icon and manifest under Project Properties.
private void
btnGetNames_Click(object sender, EventArgs e)
{
var
connectionString =
ConfigurationManager.ConnectionStrings["Personel"].ToString();
var dataAccessClass = new
Repository(connectionString);
grdResults.DataSource = dataAccessClass.GetNames
(
string.IsNullOrEmpty(txtLastName.Text)
? null : txtLastName.Text,
string.IsNullOrEmpty(txtFirstName.Text)
? null : txtFirstName.Text,
string.IsNullOrEmpty(txtMiddleInitial.Text)
? null :
txtMiddleInitial.Text,
GetSSN(txtSSN),
dteDOB.Checked ? dteDOB.Value : (DateTime?)null
);
grdResults.Columns[0].Visible = false;
grdResults.Columns[1].HeaderText = "First
Name";
grdResults.Columns[2].HeaderText = "Middle
Initial";
grdResults.Columns[3].HeaderText = "Last
Name";
}
private string
GetSSN(MaskedTextBox txtSSN)
{
var textMaskFormat = txtSSN.TextMaskFormat;
txtSSN.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
var gotText = !string.IsNullOrEmpty(txtSSN.Text.Trim());
txtSSN.TextMaskFormat = textMaskFormat;
return gotText ? txtSSN.Text : null;
}
A DateTimePicker control was used for Date of Birth. I set
the ShowCheckBox property to true so that a checkbox will be used to enable or
disable the control. If this property is checked, I send the Date of Birth
entered by the user to the method, otherwise null.
For the Social Security Number, I used a Masked Edit Control
and selected the preset for Social Security Number. When this control is used,
the default will be to return the entire string along with the formatting
characters - which in this case are dashes. Setting the TextMaskFormat property
to ExcludePromptAndLiterals will override this behavior. The GetSSN function
saves the current setting of TextMaskFormat, sets it to
ExcludePromptAndLiterals and then checks to see if anything has been entered
into the MakedEdit control. The function will return the string, including the
format characters if something has been entered by the user, otherwise it will
return null.
Now all that's needed is to add a reference to the Data
Access class, which was already developed in a previous different video - https://www.youtube.com/watch?v=zelFDKfJA18.
(The download link above has all of the code needed including the database).
The Data Access class has a method called GetNames, that will return a IEnumerable
list of names from the database based on the search criteria - First Name, Last
Name, Middle Initial, Date of Birth or Social Security Number. All we need to
is to set the DataSource property of the DataGridView to the results of the GetNames method and the grid will be
populated.
The DataGridView control will be populated with all of the
fields from the GetNames result and the column headings will default to the
field names. So the last thing is to hide the Id column and change the
FirstName, MiddleInitial and LastName columns to 'First Name,' 'Middle Name,'
and 'Last Name.'