배경 : Sorted 속성에서만 다른 두 개의 콤보 상자가 있습니다. comboBox1의 Sorted 속성이 으로 설정되어 있고 comboBox2의 Sorted 속성이 false으로 설정되어 있습니다. 이 두 콤보 상자의 데이터 소스 속성을 다시 할당하거나 재설정하려고하면 comboBox1은 데이터를 표시하지 않고 comboBox2는 데이터를 표시하지 않습니다. Sorted 속성이 comboBox1이 '데이터를 올바르게 표시하지 못하게하는 이유는 무엇입니까?정렬 된 콤보 상자 및 데이터 소스 재 할당
모든 코드는 다음과 같다 :
public partial class Form1 : Form
{
private string[] a8BitGames = { "Metroid", "Zelda", "Phantasy Star", "SB:S&SEP" };
private string[] a16BitGames = { "StarFox", "Link", "Final Fantasy", "Altered Beast" };
private List<string> lSomeList = null;
private List<string> lSomeOtherList = null;
public Form1()
{
InitializeComponent();
this.lSomeList = new List<string>(a8BitGames);
this.lSomeOtherList = new List<string>(a16BitGames);
this.comboBox1.DataSource = lSomeList;
this.comboBox2.DataSource = lSomeOtherList;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.IndexChanged(1);
}
private void IndexChanged(int comboBox)
{
this.comboBox1.DataSource = null;
this.comboBox1.DataSource = a16BitGames;
this.comboBox2.DataSource = null;
this.comboBox2.DataSource = a8BitGames;
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
this.IndexChanged(2);
}
}
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.comboBox2 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(13, 13);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.Sorted = true;
this.comboBox1.TabIndex = 0;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// comboBox2
//
this.comboBox2.FormattingEnabled = true;
this.comboBox2.Location = new System.Drawing.Point(13, 41);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(121, 21);
this.comboBox2.TabIndex = 1;
this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.comboBox2);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.ComboBox comboBox2;
}
색인에서 데이터 소스를 왜 다시 변경 하시겠습니까? –
@Sriram, 이것은 예제 코드입니다. 실제 프로그램에서는 여러 콤보 상자에 공통된 값 집합을 관리하고 두 콤보 상자에서 동일한 값을 선택할 수 없습니다. 색인이 변경 될 때마다 목록에서 새 색인 항목을 제거하고 이전 색인 항목을 추가합니다. 이렇게하면 새 인덱싱 된 항목이 다른 콤보 상자에서 선택 항목으로 제거되고 이전 인덱싱 된 항목이 선택 항목으로 추가됩니다. 그래도 질문과 관련이 있는지 확신하지 못했습니다. – HighExodus