2016-05-27 3 views
1

CheckedListBoxコントロールの項目の文字列を描画または変更しようとしています。だから私はCheckedListBoxから派生したカスタムコントロールを作成しました。CheckedBoxListコントロールのアイテムに文字列を描画するにはどうすればよいですか?

public class CheckedListBoxAdv : CheckedListBox 
{ 
    public CheckedListBoxAdv() 
     :base() 
    { 
    } 

    protected override void OnDrawItem(DrawItemEventArgs e) 
    { 
     base.OnDrawItem(e); 
     //I want to change the text alone this place. But I cannot access the text part of the item. 

    }   
} 

テキストのみを変更する方法はありますか?

+0

は、あなたがにCheckedListBox上に描画する必要がありますか?単純に項目のテキストを変更しないのはなぜですか? –

+0

私は特定の条件に基づいてテキストを変更する必要があるので。何か方法はありますか? –

+0

図面は必要ありません。単に商品テキストを変更するだけです。 –

答えて

2

あなたが任意の図面を必要としない:checkedListBoxNameはあなたにCheckedListBox

例の名前です。あなたはValueNameプロパティを含むクラスItemを作成し、次にあなたがCheckedListBoxに表示するために必要なものを返すために、クラスのToString()メソッドをオーバーライドすることができます。

public class Item 
{ 
    public int Value { get; set; } 
    public string Name { get; set; } 
    public override string ToString() 
    { 
     return this.Name; 
    } 
} 

あなたがアイテムをCheckedListBoxを埋めることができるこの方法を。それはNameの特性を示していますが、Valueプロパティへのアクセス権を持っている:

private void Form1_Load(object sender, EventArgs e) 
{ 
    this.checkedListBox1.Items.Clear(); 

    this.checkedListBox1.Items.Add(new Item() { Value = 1, Name = "One" }); 
    this.checkedListBox1.Items.Add(new Item() { Value = 2, Name = "two" }); 
    this.checkedListBox1.Items.Add(new Item() { Value = 3, Name = "three" }); 

    //Change the Name of item at index 1 (2,"two") 
    ((Item)this.checkedListBox1.Items[1]).Name = "Some Text"; 

    //But the value is untouched 
    MessageBox.Show(((Item)this.checkedListBox1.Items[1]).Value.ToString()); 

} 
0

this.checkedListBoxName.Itemsを試してみてください。this.checkedListBoxName.Items[0] = "abc";

関連する問題