Answer to Question #19531 in C# for Tom

Question #19531
Operation
• The user enters a string that consists of a last name, an account code, and a password in this format: name, account code, password. (Each value separated by a comma.)
• When the Parse button is pressed the application parses the string into name, account code, and password and displays the result in the controls shown.
Specifications
• The application should check that a value is entered into the text box (i.e. it is not left blank.)
Enhancement
• The application should check that the value that is entered includes two commas.
• The application should provide for the user entering one or more spaces after each comma and at the beginning and end of the string.
1
Expert's answer
2012-11-27T11:02:49-0500
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Parser
{
class Program
{
& static Form mainForm;
& static TextBox input;
& static Button parse;

& static Label lName;
& static Label lPassword;
& static Label lAccountCode;

& static void ParseClick(Object sender, EventArgs args)
& {
Parse(input.Text);
& }

& static void CreateMainForm()
& {
mainForm = new Form();
input = new TextBox();
parse = new Button();
lName = new Label();
lPassword = new Label();
lAccountCode = new Label();

mainForm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
mainForm.Size = new Size(303, 130);

input.Location = new Point(8, 10);
input.Width = 200;

parse.Location = new Point(216, 8);
parse.Text = "Parse";
parse.Click += new EventHandler(ParseClick);

lName.Left = lPassword.Left = lAccountCode.Left = 8;
lName.Width = lPassword.Width = lAccountCode.Width = 180;

lName.Top = 16 + input.Height;
lPassword.Top = lName.Top + lName.Height;
lAccountCode.Top = lPassword.Top + lPassword.Height;

mainForm.SuspendLayout();
mainForm.Controls.Add(input);
mainForm.Controls.Add(parse);
mainForm.Controls.Add(lName);
mainForm.Controls.Add(lPassword);
mainForm.Controls.Add(lAccountCode);
mainForm.ResumeLayout();
& }

& static void Parse(string str)
& {
str = str.Trim();
if (str == "")
{
lName.Text = "The input field is blank.";
return;
}

string[] parts = str.Split(',');
if (parts.Length != 3)
{
lName.Text = "The entered value doesn't contain two commas.";
return;
}

lName.Text = "Last name: " + parts[0].Trim();
lAccountCode.Text = "Account code: " + parts[1].Trim();
lPassword.Text = "Password: " + parts[2].Trim();
& }

& static void Main()
& {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
CreateMainForm();
Application.Run(mainForm);
& }
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS