Combo Box or Drop Down List . Adding Objects

In a combo box we can add any object –lets start by creating a class Student as shown in the below
Inside 


 But the default implementation of the toString() method in Student class which is inherited from the object class return the class name when a new object is created and called upon.
We created the Student objects and added to the collection of the combo box as a list of items on form load event as seen in the below code snippet
private void Form1_Load(object sender, EventArgs e)
        {
            comboBox4.Items.Add(new Student(1,"Amit"));
            comboBox4.Items.Add(new Student(2,"Arun"));
            comboBox4.Items.Add(new Student(3,"Arup"));
            comboBox4.Items.Add(new Student(4,"Ajit"));
            comboBox4.Items.Add(new Student(5,"Anup"));
        }
This event is registered as shown below
this.Load += new System.EventHandler(this.Form1_Load);
So we need to overrride the toString() method in the below shown
 Manner
  public override string ToString()
        {
            return Id + "," + Name;
        }


GUI- constructing

amespace PracticeWindowsForm
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;




        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.comboBox3 = new System.Windows.Forms.ComboBox();
            this.comboBox2 = new System.Windows.Forms.ComboBox();
            this.comboBox4 = new System.Windows.Forms.ComboBox();

// comboBox1
            //
            this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Items.AddRange(new object[] {
            "One",
            "Two",
            "Three",
            "Four",
            "Five",
            "Six",
            "Seven"});
            this.comboBox1.Location = new System.Drawing.Point(18, 238);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(121, 150);
            this.comboBox1.TabIndex = 16;
            //
            // comboBox3
            //
            this.comboBox3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox3.FormattingEnabled = true;
            this.comboBox3.Items.AddRange(new object[] {
            "One",
            "Two",
            "Three",
            "Four",
            "Five",
            "Six",
            "Seven"});
            this.comboBox3.Location = new System.Drawing.Point(275, 238);
            this.comboBox3.Name = "comboBox3";
            this.comboBox3.Size = new System.Drawing.Size(121, 21);
            this.comboBox3.TabIndex = 18;
            //
            // comboBox2
            //
            this.comboBox2.FormattingEnabled = true;
            this.comboBox2.Items.AddRange(new object[] {
            "One",
            "Two",
            "Three",
            "Four",
            "Five",
            "Six",
            "Seven"});
            this.comboBox2.Location = new System.Drawing.Point(148, 238);
            this.comboBox2.Name = "comboBox2";
            this.comboBox2.Size = new System.Drawing.Size(121, 21);
            this.comboBox2.TabIndex = 17;
            //
            // comboBox4
            //
            this.comboBox4.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox4.FormattingEnabled = true;
            this.comboBox4.Items.AddRange(new object[] {
            "One",
            "Two",
            "Three",
            "Four",
            "Five",
            "Six",
            "Seven"});
            this.comboBox4.Location = new System.Drawing.Point(18, 403);
            this.comboBox4.Name = "comboBox4";
            this.comboBox4.Size = new System.Drawing.Size(223, 21);
            this.comboBox4.TabIndex = 19;
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(440, 458);
            this.Controls.Add(this.comboBox4);
            this.Controls.Add(this.comboBox3);
            this.Controls.Add(this.comboBox2);
            this.Controls.Add(this.comboBox1);
   this.KeyPreview = true;
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
this.ResumeLayout(false);
            this.PerformLayout();
}
#endregion
private System.Windows.Forms.ComboBox comboBox3;
        private System.Windows.Forms.ComboBox comboBox1;
        private System.Windows.Forms.ComboBox comboBox4;
        private System.Windows.Forms.ComboBox comboBox2;
            }
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PracticeWindowsForm
{


    public partial class Form1 : Form
    {

       
        public Form1()
        {

            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox4.Items.Add(new Student(1,"Amit"));
            comboBox4.Items.Add(new Student(2,"Arun"));
            comboBox4.Items.Add(new Student(3,"Arup"));
            comboBox4.Items.Add(new Student(4,"Ajit"));
            comboBox4.Items.Add(new Student(5,"Anup"));
        }

       


    }

    class Student
    {
        public int Id;
        public string Name;

        public Student(int id, string name)
        {
            Id = id;
            Name = name;
        }

        public override string ToString()
        {
            return Id + "," + Name;
        }
    }
}

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6