Combobox Trong Lập Trình C# Winform | How Kteam
Có thể bạn quan tâm
Dẫn nhập
Sức mạnh của hệ điều hành Window là không thể chối cãi. Và để tạo nên sức mạnh đó không thể thiếu những ứng dụng mạnh mẽ. Vậy để tạo ra những ứng dụng đó, người lập trình viên cần học cái gì?
Cùng nhau tìm hiểu serial Lập trình Winform.
Nội dung
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.Threading.Tasks; using System.Windows.Forms; namespace ComboboxGUI { public partial class Form1 : Form { List<Food> listItem; List<CBClass> ListClass; public Form1() { InitializeComponent(); listItem = new List<Food>() { new Food(){Name = "Mực một nắng nướng sa tế", Price = 200000}, new Food(){Name = "Dú dê nướng sữa", Price = 75000}, new Food(){Name = "Ếch núp lùm", Price = 50000} }; comboBox1.DataSource = listItem; comboBox1.DisplayMember = "Name"; AddBinding(); ListClass = new List<CBClass>(); ListClass.Add(new CBClass() { ClassName = "12CK1", ListStudent = new List<string>() { "K9", "Kosak"} }); ListClass.Add(new CBClass() { ClassName = "12CK5", ListStudent = new List<string>() { "DG" } }); cbBranch.DataSource = ListClass; cbBranch.DisplayMember = "ClassName"; //AddClassBinding(); } void AddClassBinding() { cbClass.DataBindings.Add("DataSource", cbBranch.SelectedItem, "ListStudent", true, DataSourceUpdateMode.OnPropertyChanged); } private void button1_Click(object sender, EventArgs e) { } void AddBinding() { textBox1.DataBindings.Add(new Binding("Text", comboBox1.DataSource, "Price")); } private void comboBox1_SelectedValueChanged(object sender, EventArgs e) { //ComboBox cb = sender as ComboBox; //if (cb.SelectedValue != null) //{ // Food food = cb.SelectedValue as Food; // textBox1.Text = food.Price.ToString(); //} } private void cbBranch_SelectedValueChanged(object sender, EventArgs e) { ComboBox cb = sender as ComboBox; if (cb.SelectedValue != null) { CBClass cl = cb.SelectedValue as CBClass; cbClass.DataSource = cl.ListStudent; } } } public class Food { public string Name { get; set; } public float Price { get; set; } } public class CBClass { public string ClassName { get; set; } public List<string> ListStudent { get; set; } } }Form1.Designer.cs
namespace ComboboxGUI { 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.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.cbBranch = new System.Windows.Forms.ComboBox(); this.cbClass = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // comboBox1 // this.comboBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend; this.comboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems; this.comboBox1.FormattingEnabled = true; this.comboBox1.IntegralHeight = false; this.comboBox1.Location = new System.Drawing.Point(12, 41); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(121, 21); this.comboBox1.TabIndex = 0; this.comboBox1.SelectedValueChanged += new System.EventHandler(this.comboBox1_SelectedValueChanged); // // button1 // this.button1.Location = new System.Drawing.Point(12, 12); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(93, 23); this.button1.TabIndex = 1; this.button1.Text = "load list items"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(152, 42); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 20); this.textBox1.TabIndex = 3; // // cbBranch // this.cbBranch.FormattingEnabled = true; this.cbBranch.Location = new System.Drawing.Point(12, 126); this.cbBranch.Name = "cbBranch"; this.cbBranch.Size = new System.Drawing.Size(121, 21); this.cbBranch.TabIndex = 4; this.cbBranch.SelectedValueChanged += new System.EventHandler(this.cbBranch_SelectedValueChanged); // // cbClass // this.cbClass.FormattingEnabled = true; this.cbClass.Location = new System.Drawing.Point(151, 126); this.cbClass.Name = "cbClass"; this.cbClass.Size = new System.Drawing.Size(121, 21); this.cbClass.TabIndex = 5; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(284, 261); this.Controls.Add(this.cbClass); this.Controls.Add(this.cbBranch); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Controls.Add(this.comboBox1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.ComboBox comboBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.ComboBox cbBranch; private System.Windows.Forms.ComboBox cbClass; } }Download project
Kết luận
Bài sau chúng ta sẽ cùng tìm hiểu về Picturebox trong lập trình C# Winform.
Cảm ơn các bạn đã theo dõi bài viết. Hãy để lại bình luận hoặc góp ý của mình để phát triển bài viết tốt hơn. Đừng quên “Luyện tập – Thử thách – Không ngại khó”.
Tải xuống
Tài liệu
Nhằm phục vụ mục đích học tập Offline của cộng đồng, Kteam hỗ trợ tính năng lưu trữ nội dung bài học Combobox trong lập trình C# Winform dưới dạng file PDF trong link bên dưới.
Ngoài ra, bạn cũng có thể tìm thấy các tài liệu được đóng góp từ cộng đồng ở mục TÀI LIỆU trên thư viện Howkteam.com
Đừng quên like và share để ủng hộ Kteam và tác giả nhé!
Thảo luận
Nếu bạn có bất kỳ khó khăn hay thắc mắc gì về khóa học, đừng ngần ngại đặt câu hỏi trong phần BÌNH LUẬN bên dưới hoặc trong mục HỎI & ĐÁP trên thư viện Howkteam.com để nhận được sự hỗ trợ từ cộng đồng.
CỘNG ĐỒNG HỎI ĐÁP HOWKTEAM.COM
GROUP THẢO LUẬN FACEBOOK Từ khóa » Duyệt Combobox C#
-
Duyệt Qua Tất Cả ComboBox, Và Lấy Hết Các Giá Trị đc Chọn????
-
ComboBox - ListBox Trong Lập Trình C# Winforms - Freetuts
-
Add And Remove Items From A Windows Forms ComboBox, ListBox ...
-
Bind A Windows Forms ComboBox Or ListBox Control To Data
-
Thêm Văn Bản Và Giá Trị Vào Một Mục (không Có Nguồn Ràng Buộc)
-
Nhập Các Mục ComboBox Với Các Thành Viên Từ Cơ Sở Dữ Liệu động ...
-
[PDF] Bài Thực Hành Số 9.2 : Xây Dựng ứng Dụng Duyệt Xem Hệ Thống File
-
Thêm Sửa-xóa-combobox - C# - SlideShare
-
Cần Chỉ Giáo Cách Lấy Value Của Từng Item Trong Combobox
-
Ngôn Ngữ ASP.NET - Điều Khiển Listbox
-
Bài Tập Về Nhà Listbox C#
-
Lập Trình Cơ Sở Dữ Liệu Với C# | Tìm ở đây
-
[Lập Trình C#] – Điều Khiển ListBox | Tài Liệu Kết Nối Trẻ
-
Làm Thế Nào để Load Những Combobox Khác Khi Click 1 Combobox?