Form1.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:6k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. namespace SimpleListView
  8. {
  9. /// <summary>
  10. /// Summary description for Form1.
  11. /// </summary>
  12. public class Form1 : System.Windows.Forms.Form
  13. {
  14. private System.Windows.Forms.ListView lvCountries;
  15.     private System.Windows.Forms.ImageList imgLarge;
  16.     private System.Windows.Forms.ImageList imgSmall;
  17.     private System.Windows.Forms.ComboBox cbView;
  18.     private System.Windows.Forms.Label lblAbbreviation;
  19.     private System.ComponentModel.IContainer components;
  20. public Form1()
  21. {
  22. //
  23. // Required for Windows Form Designer support
  24. //
  25. InitializeComponent();
  26. //
  27. // TODO: Add any constructor code after InitializeComponent call
  28. //
  29. lvCountries.Items.Add(new CountryItem("United States", "US", "Dollar"));
  30.       lvCountries.Items[0].ImageIndex = 0;
  31.       lvCountries.Items.Add(new CountryItem("Great Britain", "GB", "Pound"));
  32.       lvCountries.Items[1].ImageIndex = 1;
  33.       lvCountries.Items.Add(new CountryItem("Canada", "CA", "Dollar"));
  34.       lvCountries.Items[2].ImageIndex = 2;
  35.       lvCountries.Items.Add(new CountryItem("Japan", "JP", "Yen"));
  36.       lvCountries.Items[3].ImageIndex = 3;
  37.       lvCountries.Items.Add(new CountryItem("Germany", "GM", "Deutch Mark"));
  38.       lvCountries.Items[4].ImageIndex = 4;
  39.       cbView.Items.Add(View.LargeIcon);
  40.       cbView.Items.Add(View.SmallIcon);
  41.       cbView.Items.Add(View.List);
  42.       cbView.Items.Add(View.Details);
  43.       cbView.SelectedIndex = 0;
  44.       lvCountries.Columns.Add("Country",100, HorizontalAlignment.Left);
  45.       lvCountries.Columns.Add("Currency",100, HorizontalAlignment.Left);
  46. }
  47. /// <summary>
  48. /// Clean up any resources being used.
  49. /// </summary>
  50. protected override void Dispose( bool disposing )
  51. {
  52. if( disposing )
  53. {
  54. if (components != null) 
  55. {
  56. components.Dispose();
  57. }
  58. }
  59. base.Dispose( disposing );
  60. }
  61. #region Windows Form Designer generated code
  62. /// <summary>
  63. /// Required method for Designer support - do not modify
  64. /// the contents of this method with the code editor.
  65. /// </summary>
  66. private void InitializeComponent()
  67. {
  68.       this.components = new System.ComponentModel.Container();
  69.       System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  70.       this.lvCountries = new System.Windows.Forms.ListView();
  71.       this.imgLarge = new System.Windows.Forms.ImageList(this.components);
  72.       this.imgSmall = new System.Windows.Forms.ImageList(this.components);
  73.       this.cbView = new System.Windows.Forms.ComboBox();
  74.       this.lblAbbreviation = new System.Windows.Forms.Label();
  75.       this.SuspendLayout();
  76.       // 
  77.       // lvCountries
  78.       // 
  79.       this.lvCountries.LargeImageList = this.imgLarge;
  80.       this.lvCountries.Location = new System.Drawing.Point(24, 72);
  81.       this.lvCountries.Name = "lvCountries";
  82.       this.lvCountries.Size = new System.Drawing.Size(256, 248);
  83.       this.lvCountries.SmallImageList = this.imgSmall;
  84.       this.lvCountries.TabIndex = 0;
  85.       this.lvCountries.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lvCountries_MouseUp);
  86.       // 
  87.       // imgLarge
  88.       // 
  89.       this.imgLarge.ImageSize = new System.Drawing.Size(48, 48);
  90.       this.imgLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgLarge.ImageStream")));
  91.       this.imgLarge.TransparentColor = System.Drawing.Color.Transparent;
  92.       // 
  93.       // imgSmall
  94.       // 
  95.       this.imgSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth16Bit;
  96.       this.imgSmall.ImageSize = new System.Drawing.Size(16, 16);
  97.       this.imgSmall.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgSmall.ImageStream")));
  98.       this.imgSmall.TransparentColor = System.Drawing.Color.Transparent;
  99.       // 
  100.       // cbView
  101.       // 
  102.       this.cbView.Location = new System.Drawing.Point(32, 32);
  103.       this.cbView.Name = "cbView";
  104.       this.cbView.Size = new System.Drawing.Size(128, 21);
  105.       this.cbView.TabIndex = 1;
  106.       this.cbView.SelectedIndexChanged += new System.EventHandler(this.cbView_SelectedIndexChanged);
  107.       // 
  108.       // lblAbbreviation
  109.       // 
  110.       this.lblAbbreviation.Location = new System.Drawing.Point(176, 32);
  111.       this.lblAbbreviation.Name = "lblAbbreviation";
  112.       this.lblAbbreviation.Size = new System.Drawing.Size(48, 23);
  113.       this.lblAbbreviation.TabIndex = 2;
  114.       // 
  115.       // Form1
  116.       // 
  117.       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  118.       this.ClientSize = new System.Drawing.Size(312, 339);
  119.       this.Controls.Add(this.lblAbbreviation);
  120.       this.Controls.Add(this.cbView);
  121.       this.Controls.Add(this.lvCountries);
  122.       this.Name = "Form1";
  123.       this.Text = "Form1";
  124.       this.ResumeLayout(false);
  125.     }
  126. #endregion
  127. /// <summary>
  128. /// The main entry point for the application.
  129. /// </summary>
  130. [STAThread]
  131. static void Main() 
  132. {
  133. Application.Run(new Form1());
  134. }
  135.     private void cbView_SelectedIndexChanged(object sender, System.EventArgs e)
  136.     {
  137.       lvCountries.View = (View)cbView.SelectedItem;
  138.     }
  139.     private void lvCountries_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  140.     {
  141.       if(lvCountries.View != View.Details)
  142.         lblAbbreviation.Text = ((CountryItem)lvCountries.GetItemAt(e.X, e.Y)).CountryAbbreviation;
  143.       else
  144.         lblAbbreviation.Text = ((CountryItem)lvCountries.GetItemAt(5, e.Y)).CountryAbbreviation;
  145.     }
  146.     
  147. }
  148. }