Friday, May 6, 2016

Program 3: Create a windows form to read the details of a student and display the details using labels.

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 StudentDetails and specify the address of your folder.
4. In the main window design the form as shown below.

Consider the attributes like, RollNo, Name, Gender, Branch, Interested in etc.
Use the control Textbox for Rollno, Name. use the control Radiobutton for Gender .One radiobutton for male another for female. these two radiobuttons are kept in a GroupBox.
For Branch, use the control combobox which provides you a drop-down list of values. of course you need to set these values in the properties of combobox. Then use checkbox for the attribute interested in.(sports, cultural activities, NSS etc). These checkboxes are kept in GroupBox. all these above controls can be named accordingly from their corresponding properties window.

Labels are used against each control to increase readability and understand ability. Top right corner, one label is used named as lblDetails to show all these details.
Show and cancel are two buttons where we are going to write some code. 

3.    5. Double click the Show  button and  write the following code
            
            String gens;
            String interest="";
            if (radmale.Checked)
            {
                gens = radmale.Text;

            }
            else
            {
                gens = radfemale.Text;

            }
          
            if (chkBoxSports.Checked)
            {
                interest = chkBoxSports.Text;

            }
            if (chkBoxCA.Checked)
            {
                interest += chkBoxCA.Text;

            }
            if (chkBoxNss.Checked)
            {
                interest += chkBoxCA.Text;

            }
           
            lblDetails.Text = "Your Details are "+"\n"
                +"RollNo: "+ txtrollno.Text+"\n"
                + "Name : " + txtname.Text + "\n"
                + "Gender : " + gens + "\n"
                + "Branch : " + combranch.SelectedItem + "\n"
                +"Interested in : " +interest;

               
      }


Explanation:

Here, radmale & radfemale are the name of radiobuttons used for gender. The string variable gens is used to hold the value of the gender based on the radiobutton which is checked.

similarly, chkBoxSports, chkBoxCA and chkBoxNSS are the names of the options available for the attribute interested in. The variable interest is used to hold the value of the attribute interested in based on the checkboxes selected or checked. Note, you can select multiple checkbox but cannot select multiple radiobuttons of a single Group.

Last six lines are used to display the details of student, in a label lblDetails.

3.   6.   Double click the Cancel  button and  write the following code,

private void btnCancel_Click(object sender, EventArgs e)
        {
           Application.Exit();
          
        }
4.   7.  Run your program  and check the result.




e
E

No comments:

Post a Comment