Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PRODUCT CLASS
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ProductWithListSorting
- {
- class Product:IComparable<Product>
- {
- public int prod_id { set; get; }
- public string prod_name{ set; get; }
- public int CompareTo(Product ob1) //OVERRIDE OF IComparable<Product>
- {
- if (prod_id > ob1.prod_id)
- return 1;
- if (prod_id < ob1.prod_id)
- return -1;
- else
- return 0;
- }
- }
- }
- // FORM1.CS
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace ProductWithListSorting
- {
- public partial class Form1 : Form
- {
- List<Product> plist= new List<Product>();
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void btnAdd_Click(object sender, EventArgs e)
- {
- Product p = new Product();
- p.prod_id = Convert.ToInt32(txtPid.Text);
- p.prod_name = textBox2.Text;
- plist.Add(p);
- listBox1.Items.Add(p.prod_id + " " + p.prod_name);
- }
- private void btnSort_Click(object sender, EventArgs e)
- {
- plist.Sort(); //CALLS CompareTo FUCNTION OF PRODUCT CLASS
- listBox1.Items.Clear();
- foreach (Product x in plist)
- {
- listBox1.Items.Add(x.prod_id + " " + x.prod_name);
- }
- }
- }
- }
- //DESIGNER CLASS
- namespace ProductWithListSorting
- {
- 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.btnAdd = new System.Windows.Forms.Button();
- this.btnSort = new System.Windows.Forms.Button();
- this.txtPid = new System.Windows.Forms.TextBox();
- this.textBox2 = new System.Windows.Forms.TextBox();
- this.listBox1 = new System.Windows.Forms.ListBox();
- this.label1 = new System.Windows.Forms.Label();
- this.txtPname = new System.Windows.Forms.Label();
- this.SuspendLayout();
- //
- // btnAdd
- //
- this.btnAdd.Location = new System.Drawing.Point(91, 224);
- this.btnAdd.Name = "btnAdd";
- this.btnAdd.Size = new System.Drawing.Size(75, 23);
- this.btnAdd.TabIndex = 0;
- this.btnAdd.Text = "Add";
- this.btnAdd.UseVisualStyleBackColor = true;
- this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
- //
- // btnSort
- //
- this.btnSort.Location = new System.Drawing.Point(190, 224);
- this.btnSort.Name = "btnSort";
- this.btnSort.Size = new System.Drawing.Size(75, 23);
- this.btnSort.TabIndex = 1;
- this.btnSort.Text = "Sort";
- this.btnSort.UseVisualStyleBackColor = true;
- this.btnSort.Click += new System.EventHandler(this.btnSort_Click);
- //
- // txtPid
- //
- this.txtPid.Location = new System.Drawing.Point(102, 27);
- this.txtPid.Name = "txtPid";
- this.txtPid.Size = new System.Drawing.Size(186, 20);
- this.txtPid.TabIndex = 3;
- //
- // textBox2
- //
- this.textBox2.Location = new System.Drawing.Point(102, 101);
- this.textBox2.Name = "textBox2";
- this.textBox2.Size = new System.Drawing.Size(186, 20);
- this.textBox2.TabIndex = 4;
- //
- // listBox1
- //
- this.listBox1.FormattingEnabled = true;
- this.listBox1.Location = new System.Drawing.Point(385, 12);
- this.listBox1.Name = "listBox1";
- this.listBox1.Size = new System.Drawing.Size(229, 186);
- this.listBox1.TabIndex = 5;
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(29, 33);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(58, 13);
- this.label1.TabIndex = 6;
- this.label1.Text = "Product ID";
- //
- // txtPname
- //
- this.txtPname.AutoSize = true;
- this.txtPname.Location = new System.Drawing.Point(12, 104);
- this.txtPname.Name = "txtPname";
- this.txtPname.Size = new System.Drawing.Size(75, 13);
- this.txtPname.TabIndex = 7;
- this.txtPname.Text = "Product Name";
- //
- // Form1
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(626, 313);
- this.Controls.Add(this.txtPname);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.listBox1);
- this.Controls.Add(this.textBox2);
- this.Controls.Add(this.txtPid);
- this.Controls.Add(this.btnSort);
- this.Controls.Add(this.btnAdd);
- this.Name = "Form1";
- this.Text = "Form1";
- this.Load += new System.EventHandler(this.Form1_Load);
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.Button btnAdd;
- private System.Windows.Forms.Button btnSort;
- private System.Windows.Forms.TextBox txtPid;
- private System.Windows.Forms.TextBox textBox2;
- private System.Windows.Forms.ListBox listBox1;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Label txtPname;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement