1. 1.
Open Visual studio 2013.
2. On the menu bar, choose File, New, and Project.
Choose Programming language as Visual C#. In the templates list, choose the Windows Forms Application icon.
3. Name the project as StringManip
and specify the address of your folder.
4. In the main window design
the form as shown below and set the properties as required.
![]() |
Windows Form Design for StringManipulation: an Example |
In the btnPrintName_Click method write
the following code.
private void btnPrintName_Click(object sender,
EventArgs e)
{
lblDisplay.Text = txtSurName.Text + " " +
txtName.Text;
}
In the btnLength_Click method write the
following code.
private void btnLength_Click(object sender,
EventArgs e)
{
int
surnameLen = txtName.Text.Length;
int nameLen
= txtSurName.Text.Length;
int
fullNameLen = (txtSurName.Text +"
" +
txtName.Text).Length;
lblDisplay.Text = "Surname Length :" +
surnameLen.ToString() + “\n”;
lblDisplay.Text += "Name Lengh :" +
nameLen.ToString() + "\n";
lblDisplay.Text += "Full Name Length :" +
fullNameLen.ToString();
}
In the btnFindPetName_Click method write
the following code.
private void btnFindPetName_Click(object sender,
EventArgs e)
{
String petName
= txtPetName.Text;
String
fullName = txtSurName.Text + "
" +
txtName.Text;
if
(fullName.Contains(petName))
{
lblDisplay.Text = "Petname exists in your
name";
}
else
{
lblDisplay.Text = "Petname doesn't exist in
your
name";
}
}
In the btnShortName_Click method write
the following code.
private void btnShortName_Click(object sender,
EventArgs e)
{
String name =
txtName.Text;
String surName
= txtSurName.Text;
String initial
=surName.Replace(surName,
surName[0].ToString() + ". ");
String
shortName =initial + name;
lblDisplay.Text = shortName;
}
In the btnSplit_Click method write the
following code.
private void btnSplit_Click(object sender,
EventArgs e)
{
lblDisplay.Text = "";
String name =
txtName.Text;
String[] names
= name.Split(' ');
foreach (String s in names)
lblDisplay.Text += s + "\n";
}
In the btnSplit_Click method write the
following code.
private void btnShowName_Click(object sender,
EventArgs e)
{
String
fullName = null;
String name =
txtName.Text;
String surName
= txtSurName.Text;
String str =
cboPosition.SelectedItem.ToString().ToUpper();
if(str == "FIRST")
fullName =
name.Insert(0,surName);
else if(str == "LAST")
fullName =
name.Insert(name.Length, surName);
lblDisplay.Text = fullName +
System.Environment.NewLine;
lblDisplay.Text
+=fullName.PadRight(30,'#')+"\n";
lblDisplay.Text +=
fullName.PadLeft(30, '*')+"\n";
}
In the btnSplit_Click method write the
following code.
private void btnCancel_Click(object sender,
EventArgs e)
{
Application.Exit();
}
Execute your project...Enjoy Programming..
No comments:
Post a Comment