Advertisement
varungurnaney

C#: Product With List + Sorting+ Study of IComparable<t>

Jul 12th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.98 KB | None | 0 0
  1. //PRODUCT CLASS
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace ProductWithListSorting
  9.     {
  10.     class Product:IComparable<Product>
  11.         {
  12.         public int prod_id { set; get; }
  13.         public string prod_name{ set; get; }
  14.  
  15.         public int CompareTo(Product ob1) //OVERRIDE OF IComparable<Product>
  16.             {
  17.             if (prod_id > ob1.prod_id)
  18.                 return 1;
  19.             if (prod_id < ob1.prod_id)
  20.                 return -1;
  21.             else
  22.                 return 0;
  23.             }
  24.         }
  25.  
  26.     }
  27.  
  28.  
  29.  
  30.  
  31. // FORM1.CS
  32.  
  33. using System;
  34. using System.Collections.Generic;
  35. using System.ComponentModel;
  36. using System.Data;
  37. using System.Drawing;
  38. using System.Linq;
  39. using System.Text;
  40. using System.Windows.Forms;
  41.  
  42. namespace ProductWithListSorting
  43.     {
  44.     public partial class Form1 : Form
  45.         {
  46.         List<Product> plist= new List<Product>();
  47.        
  48.         public Form1()
  49.             {
  50.             InitializeComponent();
  51.             }
  52.  
  53.         private void Form1_Load(object sender, EventArgs e)
  54.             {
  55.            
  56.  
  57.  
  58.             }
  59.  
  60.         private void btnAdd_Click(object sender, EventArgs e)
  61.             {
  62.             Product p = new Product();
  63.             p.prod_id = Convert.ToInt32(txtPid.Text);
  64.             p.prod_name = textBox2.Text;
  65.             plist.Add(p);
  66.             listBox1.Items.Add(p.prod_id + " " + p.prod_name);
  67.             }
  68.  
  69.         private void btnSort_Click(object sender, EventArgs e)
  70.             {
  71.             plist.Sort(); //CALLS CompareTo FUCNTION OF PRODUCT CLASS
  72.             listBox1.Items.Clear();
  73.             foreach (Product x in plist)
  74.             {
  75.             listBox1.Items.Add(x.prod_id + " " + x.prod_name);
  76.  
  77.                 }
  78.  
  79.             }
  80.         }
  81.     }
  82.  
  83.  
  84.  
  85. //DESIGNER CLASS
  86.  
  87.  
  88. namespace ProductWithListSorting
  89.     {
  90.     partial class Form1
  91.         {
  92.         /// <summary>
  93.         /// Required designer variable.
  94.         /// </summary>
  95.         private System.ComponentModel.IContainer components = null;
  96.  
  97.         /// <summary>
  98.         /// Clean up any resources being used.
  99.         /// </summary>
  100.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  101.         protected override void Dispose(bool disposing)
  102.             {
  103.             if (disposing && (components != null))
  104.                 {
  105.                 components.Dispose();
  106.                 }
  107.             base.Dispose(disposing);
  108.             }
  109.  
  110.         #region Windows Form Designer generated code
  111.  
  112.         /// <summary>
  113.         /// Required method for Designer support - do not modify
  114.         /// the contents of this method with the code editor.
  115.         /// </summary>
  116.         private void InitializeComponent()
  117.             {
  118.             this.btnAdd = new System.Windows.Forms.Button();
  119.             this.btnSort = new System.Windows.Forms.Button();
  120.             this.txtPid = new System.Windows.Forms.TextBox();
  121.             this.textBox2 = new System.Windows.Forms.TextBox();
  122.             this.listBox1 = new System.Windows.Forms.ListBox();
  123.             this.label1 = new System.Windows.Forms.Label();
  124.             this.txtPname = new System.Windows.Forms.Label();
  125.             this.SuspendLayout();
  126.             //
  127.             // btnAdd
  128.             //
  129.             this.btnAdd.Location = new System.Drawing.Point(91, 224);
  130.             this.btnAdd.Name = "btnAdd";
  131.             this.btnAdd.Size = new System.Drawing.Size(75, 23);
  132.             this.btnAdd.TabIndex = 0;
  133.             this.btnAdd.Text = "Add";
  134.             this.btnAdd.UseVisualStyleBackColor = true;
  135.             this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
  136.             //
  137.             // btnSort
  138.             //
  139.             this.btnSort.Location = new System.Drawing.Point(190, 224);
  140.             this.btnSort.Name = "btnSort";
  141.             this.btnSort.Size = new System.Drawing.Size(75, 23);
  142.             this.btnSort.TabIndex = 1;
  143.             this.btnSort.Text = "Sort";
  144.             this.btnSort.UseVisualStyleBackColor = true;
  145.             this.btnSort.Click += new System.EventHandler(this.btnSort_Click);
  146.             //
  147.             // txtPid
  148.             //
  149.             this.txtPid.Location = new System.Drawing.Point(102, 27);
  150.             this.txtPid.Name = "txtPid";
  151.             this.txtPid.Size = new System.Drawing.Size(186, 20);
  152.             this.txtPid.TabIndex = 3;
  153.             //
  154.             // textBox2
  155.             //
  156.             this.textBox2.Location = new System.Drawing.Point(102, 101);
  157.             this.textBox2.Name = "textBox2";
  158.             this.textBox2.Size = new System.Drawing.Size(186, 20);
  159.             this.textBox2.TabIndex = 4;
  160.             //
  161.             // listBox1
  162.             //
  163.             this.listBox1.FormattingEnabled = true;
  164.             this.listBox1.Location = new System.Drawing.Point(385, 12);
  165.             this.listBox1.Name = "listBox1";
  166.             this.listBox1.Size = new System.Drawing.Size(229, 186);
  167.             this.listBox1.TabIndex = 5;
  168.             //
  169.             // label1
  170.             //
  171.             this.label1.AutoSize = true;
  172.             this.label1.Location = new System.Drawing.Point(29, 33);
  173.             this.label1.Name = "label1";
  174.             this.label1.Size = new System.Drawing.Size(58, 13);
  175.             this.label1.TabIndex = 6;
  176.             this.label1.Text = "Product ID";
  177.             //
  178.             // txtPname
  179.             //
  180.             this.txtPname.AutoSize = true;
  181.             this.txtPname.Location = new System.Drawing.Point(12, 104);
  182.             this.txtPname.Name = "txtPname";
  183.             this.txtPname.Size = new System.Drawing.Size(75, 13);
  184.             this.txtPname.TabIndex = 7;
  185.             this.txtPname.Text = "Product Name";
  186.             //
  187.             // Form1
  188.             //
  189.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  190.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  191.             this.ClientSize = new System.Drawing.Size(626, 313);
  192.             this.Controls.Add(this.txtPname);
  193.             this.Controls.Add(this.label1);
  194.             this.Controls.Add(this.listBox1);
  195.             this.Controls.Add(this.textBox2);
  196.             this.Controls.Add(this.txtPid);
  197.             this.Controls.Add(this.btnSort);
  198.             this.Controls.Add(this.btnAdd);
  199.             this.Name = "Form1";
  200.             this.Text = "Form1";
  201.             this.Load += new System.EventHandler(this.Form1_Load);
  202.             this.ResumeLayout(false);
  203.             this.PerformLayout();
  204.  
  205.             }
  206.  
  207.         #endregion
  208.  
  209.         private System.Windows.Forms.Button btnAdd;
  210.         private System.Windows.Forms.Button btnSort;
  211.         private System.Windows.Forms.TextBox txtPid;
  212.         private System.Windows.Forms.TextBox textBox2;
  213.         private System.Windows.Forms.ListBox listBox1;
  214.         private System.Windows.Forms.Label label1;
  215.         private System.Windows.Forms.Label txtPname;
  216.         }
  217.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement