If you haven’t looked at Part 1, here is the link!
This tutorial will go in a bit more details about Windows Forms, more precisely, we will look at how to retrieve and store user data from a Form. To do so, we will be using the following controls:
- TextBox
- RichTextBox
- ComboBox
- Button
As you start using more and more controls, you will begin to see similarities between them. Just drag and drop the control you wish to add from the Toolbox onto the form. It should look like the form below.
Modify the controls as follow:
1. Rename all you controls with relevant names, like btnAddUser for the button.
2. Change the DropDownStyle property of the ComboBox control to DropDownList.
3. Add male and female in the gender ComboBox Item property. This property works like a list, it means that each line represent one item.
What we will want to do next is, when the Button is pressed, grab all the data from the form and put it in memory. For that, we can create an object corresponding to the content of the form.
In other words, the object will contain the form fields. Here is how I implemented the Client object.
Visual Basic
Public Class Client Public Property FullName As String Public Property Gender As String Public Property Address As String Public Property Details As String Public Sub New(FullName As String, Gender As String, Address As String, Details As String) Me.FullName = FullName Me.Gender = Gender Me.Address = Address Me.Details = Details End Sub End Class
C#
namespace FormsControls { class Client { public string FullName { get; set; } public string Gender { get; set; } public string Address { get; set; } public string Details { get; set; } public Client(string FullName, string Gender, string Address, string Details) { this.FullName = FullName; this.Gender = Gender; this.Address = Address; this.Details = Details; } } }
Here is what my form looks like, feel free to recreate it to your taste!
Don’t forget that you should rename at least the controls that you plan on using in your code. This should simplify your coding task a lot.
An example for a TextBox name could be: tbFirstName, tbLastName, and so on.
You can modify some other properties on the form like the maximum text length of the Text Boxes, but I am done with the interface and will go straight in the code. The first thing we will want to do is to validate our user input. Always remember that our user is “kind” of our enemy here because we don’t know what he/she has in mind (remember that this goes both ways) and we don’t want wrong values entered.
There are multiple techniques and variation to validation, but I will only validate one thing for now: the fields must all be filled. Double click on the button to create a new (default) click event.
Visual Basic
Public Class frmClients Private Sub btnAddUser_Click(sender As Object, e As EventArgs) Handles btnAddUser.Click If txtFullName.Text = String.Empty Or txtAddress.Text = String.Empty Or cbGender.Text = String.Empty Or rtbDetails.Text = String.Empty Then MessageBox.Show("") Return End If 'Standard process goes here End Sub End Class
C#
private void btnAddUser_Click(object sender, EventArgs e) { if (txtFullName.Text == String.Empty || txtAddress.Text == String.Empty || cbGender.Text == String.Empty || rtbDetails.Text == String.Empty) { MessageBox.Show("All fields must be filled."); return; } //Standard process goes here }
Note: remember that if you copy and paste that code, you might have to enter the event name manually in the Events section of the property window.
I am simply validating that if any of the fields Text value is equal to an empty string, the user will be warned with a message box and the method will stop to give a chance to our user correct the data entered.
In the same method, we will write the standard method of creating a new client, fill the object with the user data and display the created client to our user.
Visual Basic
Public Class frmClients Private Sub btnAddUser_Click(sender As Object, e As EventArgs) Handles btnAddUser.Click If txtFullName.Text = String.Empty Or txtAddress.Text = String.Empty Or cbGender.Text = String.Empty Or rtbDetails.Text = String.Empty Then MessageBox.Show("") Return End If Dim FullName As String Dim Address As String Dim Gender As String Dim Details As String FullName = txtFullName.Text Address = txtAddress.Text Gender = cbGender.Text Details = rtbDetails.Text Dim NewClient As New Client(FullName, Gender, Address, Details) End Sub End Class
C#
public partial class frmUsers : Form { public frmUsers() { InitializeComponent(); } private void btnAddUser_Click(object sender, EventArgs e) { if (txtFullName.Text == String.Empty || txtAddress.Text == String.Empty || cbGender.Text == String.Empty || rtbDetails.Text == String.Empty) { MessageBox.Show("All fields must be filled."); return; } //Standard process goes here string FullName = txtFullName.Text; string Address = txtAddress.Text; string Gender = cbGender.Text; string Details = rtbDetails.Text; Client NewClient = new Client(FullName, Address, Gender, Details); MessageBox.Show(NewClient.ToString()); } }
I just want to point out that I am using variables to store the content of the TextBoxes before assigning them to the new object, this is my working standard. In other words you could have input the fields Text property directly into the object constructor. What I mean is something like:
' Visual Basic
Dim NewClient As Client = New Client(txtFirstName.Text, txtLastName.Text, cbGender.Text, rtbDetails.Text)
// C#
Client NewClient = new Client(txtFirstName.Text, txtLastName.Text, cbGender.Text, rtbDetails.Text);
On the final line I am using the ToString() method to display the object in the MessageBox but this displays only the namespace because it is the default ToString() method that comes from the parent. That’s why we have to override this method in the class to make sure it displays relevant information.
Visual Basic
Public Class Client Public Property FullName As String Public Property Gender As String Public Property Address As String Public Property Details As String Public Sub New(FullName As String, Gender As String, Address As String, Details As String) Me.FullName = FullName Me.Gender = Gender Me.Address = Address Me.Details = Details End Sub Public Overrides Function ToString() As String Dim OutputString As String OutputString = String.Format("Client added \nFull name: {0} \nGender: {1} \nAddress: {2} \nDetails: {3}", FullName, Gender, Address, Details) Return OutputString End Function End Class
C#
namespace FormsControls { class Client { public string FullName { get; set; } public string Gender { get; set; } public string Address { get; set; } public string Details { get; set; } public Client(string FullName, string Gender, string Address, string Details) { this.FullName = FullName; this.Gender = Gender; this.Address = Address; this.Details = Details; } public override string ToString() { string OutputString = String.Empty; OutputString = String.Format("Client added \nFull name: {0} \nGender: {1} \nAddress: {2} \nDetails: {3}", FullName, Gender, Address, Details); return OutputString; } } }
Pay close attention to how the ToString method is created (using override). That directive specifies that it overrides an already existing method from its parent (object). It might be useful to specify that the ‘\n’ is what will return to a new line in the MessageBox we will display.
You can now give this program a test and try to enter no value in one of the fields to see what happens. Again, breakpoints can be used to really see what the computer is doing at every steps.
YouTube
Visual Basic
C#
No Comments Yet!
You can be first to comment this post!