1.使用CheckedListBox1.Items.Add迴圈方式
賦值:
foreach (DataRow dr in ds.Tables[0].Rows)
{
checkedListBox1.Items.Add(dr["Name"]);
}
選中索引:
foreach (int indexChecked in checkedListBox1.CheckedIndices)
{
MessageBox.Show("Index#: " + indexChecked.ToString());
}
選中值:
foreach (object itemChecked in chk.CheckedItems)
{
MessageBox.Show("Item with title: " + itemChecked.ToString());
}
2.使用DataSource方式
賦值:
CheckedListBox.DataSource = ds.Tables[0];
CheckedListBox.DisplayMember = "Id";
CheckedListBox.ValueMember = "Name";
選中值:
for (int count = 0; count < CheckedListBox1.CheckedItems.Count; count++)
{
string id = (CheckedListBox1.Items[CheckedListBox1.CheckedIndices[count]] as DataRowView)["Id"].ToString(); //獲取ID
string name = (CheckedListBox1.Items[CheckedListBox1.CheckedIndices[count]] as DataRowView)["Name"].ToString(); //獲取名稱
MessageBox.Show("Id:" + id + " Name:" + name);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace WindowsFormsApplication1
{
public class MatchCalssInitialition
{
public static string originalBinPath = @"C:\Program Files\Kodak\CaptureConsole\Bin\";
public static string excelFilePath = @"D:\DRErrorMessageConfigurationFiles\segscbincidents_error_translations_form_011012_01(old) - Copy.xls";
public static string suffixFileName = "DRErrorMessages.config";
/// <summary>
///
/// </summary>
public enum DRErrorMessage
{
German = 5,
Czech = 6,
Danish = 7,
Greek = 8,
Spanish = 9,
Finnish = 10,
French = 11,
Hungarian = 12,
Italian = 13,
Japanese = 14,
Korean = 15,
Dutch = 16,
Norwegian = 17,
Polish = 18,
Portuguese = 19, //pt-br (Portuguese-LatinAmer : Portuguese Brazil )
PortugueseIberian = 20, //pt-PT (Portuguese)
Romanian = 21,
Russian = 22,
Swedish = 23,
Turkish = 24,
ChineseS = 25, //SimplifiedChinese
ChineseT = 26 //TraditionalChinese
}
/// <summary>
///
/// </summary>
/// <param name="fileType"></param>
/// <returns></returns>
public static Dictionary<int, string> dtGetAllConfigurationFiles()
{
Dictionary<int, string> myDictionaryResult = new Dictionary<int, string>();
#region add key value pair for dictionary.
//myDictionaryResult.Add((int)DRErrorMessage.German, DRErrorMessage.German.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Czech, DRErrorMessage.Czech.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Danish, DRErrorMessage.Danish.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Greek, DRErrorMessage.Greek.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Spanish, DRErrorMessage.Spanish.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Finnish, DRErrorMessage.Finnish.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.French, DRErrorMessage.French.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Hungarian, DRErrorMessage.Hungarian.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Italian, DRErrorMessage.Italian.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Japanese, DRErrorMessage.Japanese.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Korean, DRErrorMessage.Korean.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Dutch, DRErrorMessage.Dutch.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Norwegian, DRErrorMessage.Norwegian.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Polish, DRErrorMessage.Polish.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Portuguese, DRErrorMessage.Portuguese.ToString() + suffixFileName); //pt-br (Portuguese-LatinAmer)
//myDictionaryResult.Add((int)DRErrorMessage.PortugueseIberian, DRErrorMessage.PortugueseIberian.ToString() + suffixFileName); //pt-PT (Portuguese)
//myDictionaryResult.Add((int)DRErrorMessage.Romanian, DRErrorMessage.Romanian.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Russian, DRErrorMessage.Russian.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Swedish, DRErrorMessage.Swedish.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.Turkish, DRErrorMessage.Turkish.ToString() + suffixFileName);
//myDictionaryResult.Add((int)DRErrorMessage.ChineseS, DRErrorMessage.ChineseS.ToString() + suffixFileName); //SimplifiedChinese
//myDictionaryResult.Add((int)DRErrorMessage.ChineseT, DRErrorMessage.ChineseT.ToString() + suffixFileName); //TraditionalChinese
#endregion
foreach (DRErrorMessage enumDRErrorMessage in Enum.GetValues(typeof(DRErrorMessage)))
{
//myDictionaryResult.Add((int)enumDRErrorMessage, enumDRErrorMessage.ToString() + suffixFileName);
addDictionaryKeyValeuPairs( myDictionaryResult, enumDRErrorMessage);
}
return myDictionaryResult;
}
/// <summary>
///
/// </summary>
/// <param name="columnIndex"></param>
/// <returns></returns>
public static string getSingleConfigurationFilePath(int columnIndex)
{
string path = string.Empty;
Dictionary<int, string> myDictionary = dtGetAllConfigurationFiles();
foreach (KeyValuePair<int, string> keyValue in myDictionary)
{
if (keyValue.Key == columnIndex)
{
path = originalBinPath + keyValue.Value;
break;
}
}
return path;
}
/// <summary>
///
/// </summary>
/// <param name="myDictionary"></param>
/// <param name="drErrorMessage"></param>
public static void addDictionaryKeyValeuPairs( Dictionary<int, string> myDictionary, DRErrorMessage drErrorMessage)
{
int index = (int)drErrorMessage;
if (!myDictionary.Keys.Contains(index))
{
myDictionary.Add(index, drErrorMessage.ToString() + suffixFileName);
}
}
}
}
this.checkedListBox1.DataSource = null;
Dictionary<int, string> myDictionaryAll = MatchCalssInitialition.dtGetAllConfigurationFiles();
if (myDictionaryAll.Count == 0) { return; }
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = myDictionaryAll;
this.checkedListBox1.DataSource = bindingSource;
this.checkedListBox1.DisplayMember = "Value";
this.checkedListBox1.ValueMember = "Key";
this.checkedListBox1.SetItemCheckState(2,CheckState.Checked);
//foreach (KeyValuePair<int, string> keyValueAll in myDictionaryAll)
//{
// this.checkedListBox1.Items.Add(keyValueAll.Value,false);
//}
////迴圈遍歷checkedlistbox
//for (int i = 0; i < checkedListBox1.Items.Count; i++)
//{
////如果被選中則執行下列程式碼
// if (this.checkedListBox1.GetItemChecked(i))
// {
// MessageBox.Show(this.checkedListBox1.Items[i].ToString());
// }
//}
Dictionary<int, string> myDictionary = new Dictionary<int, string>();
for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
{
//Dictionary<int, string> singleDictionary = checkedListBox1.Items[checkedListBox1.CheckedIndices[i]] as Dictionary<int, string>;
if (this.checkedListBox1.GetItemChecked(i))
{
}
}
if (true) return;
}