Click here to Skip to main content
15,879,348 members
Home / Discussions / C#
   

C#

 
GeneralRe: Instantiating an array of objects Pin
crushinghellhammer29-Jul-04 9:32
crushinghellhammer29-Jul-04 9:32 
GeneralRe: Instantiating an array of objects Pin
Nick Parker29-Jul-04 10:11
protectorNick Parker29-Jul-04 10:11 
GeneralWM_PRINT for themed controls Pin
Mathew Hall29-Jul-04 6:31
Mathew Hall29-Jul-04 6:31 
GeneralRe: WM_PRINT for themed controls Pin
Heath Stewart29-Jul-04 9:25
protectorHeath Stewart29-Jul-04 9:25 
GeneralCalculating Point locations in relation to Parent client area. Pin
Tristan Rhodes29-Jul-04 6:27
Tristan Rhodes29-Jul-04 6:27 
GeneralRe: Calculating Point locations in relation to Parent client area. Pin
Nick Parker29-Jul-04 8:06
protectorNick Parker29-Jul-04 8:06 
GeneralRe: Calculating Point locations in relation to Parent client area. Pin
Anonymous30-Jul-04 13:42
Anonymous30-Jul-04 13:42 
GeneralCheckedListBox event handling strangeness Pin
James Kolpack29-Jul-04 5:32
James Kolpack29-Jul-04 5:32 
Hi,

I want to update both the contents and the checked state of items in a CheckedListBox on the ItemCheck event. It turns out that when removing an item that occurs before the checked item (the one that is being processed), both this checked item and the one after it will appear to be checked after the event finishes. To illustrate, here is some code that first populates a CheckedListBox with a sequence of integers (named checkedListBox), and then when an item is checked, removes this item, and also checks the previous item if it was even, or the following item if it was odd.

private void checkedListBox_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
{
// get the currently checked index
int checkedItemIndex = (int)checkedListBox.Items[checkedListBox.SelectedIndex];
bool checkedEven = checkedItemIndex % 2 == 0 ? true : false;

// remove the event handler for now - it is triggered by SetItemCheckState
checkedListBox.ItemCheck -= new ItemCheckEventHandler(checkedListBox_ItemCheck);

// if it was an even number, check the previous index
// ie - checking 4 will result in 3 being checked
// else, check the following index
// ie - checking 5 will result in 6 being checked
if (checkedItemIndex > 1 && checkedEven)
checkedListBox.SetItemCheckState(checkedItemIndex - 1, CheckState.Checked);
else if (checkedItemIndex < checkedListBox.Items.Count - 1)
checkedListBox.SetItemCheckState(checkedItemIndex, CheckState.Checked);

checkedListBox.ItemCheck += new ItemCheckEventHandler(checkedListBox_ItemCheck);

// remove the currently clicked index
checkedListBox.Items.RemoveAt(checkedItemIndex);
}


// here is the code to populate the box
private void resetListBoxContents()
{
checkedListBox.Items.Clear();
for (int i=0; i < 20; i++)
checkedListBox.Items.Add(i);
}


As you see, when checking 9, 9 is removed and 10 is checked. But when 4 is checked and removed, 3 becomes checked, as it should be, but so is 5, which should not be.

Any help in this matter would be greatly appreciated. Thanks!
GeneralRe: CheckedListBox event handling strangeness Pin
Nick Parker29-Jul-04 8:01
protectorNick Parker29-Jul-04 8:01 
GeneralRe: CheckedListBox event handling strangeness Pin
James Kolpack29-Jul-04 8:31
James Kolpack29-Jul-04 8:31 
GeneralRe: CheckedListBox event handling strangeness Pin
James Kolpack29-Jul-04 11:12
James Kolpack29-Jul-04 11:12 
GeneralC# DataGrid - how to get the sort arrow in column header Pin
T J Manjaly29-Jul-04 5:03
sussT J Manjaly29-Jul-04 5:03 
GeneralColumn selection control needed Pin
Jan R Hansen29-Jul-04 4:38
Jan R Hansen29-Jul-04 4:38 
GeneralRe: Column selection control needed Pin
billb211229-Jul-04 4:52
billb211229-Jul-04 4:52 
GeneralRe: Column selection control needed Pin
Nick Parker29-Jul-04 5:33
protectorNick Parker29-Jul-04 5:33 
GeneralDns.GetHostByAddress problem Pin
Dave@keyspell29-Jul-04 4:12
sussDave@keyspell29-Jul-04 4:12 
GeneralRe: Dns.GetHostByAddress problem Pin
Heath Stewart29-Jul-04 4:41
protectorHeath Stewart29-Jul-04 4:41 
GeneralRe: Dns.GetHostByAddress problem Pin
Dave@keyspell29-Jul-04 5:31
sussDave@keyspell29-Jul-04 5:31 
QuestionHow to Export to CSV file from DataSet? Pin
HyVong29-Jul-04 3:11
HyVong29-Jul-04 3:11 
AnswerRe: How to Export to CSV file from DataSet? Pin
Not Active29-Jul-04 4:13
mentorNot Active29-Jul-04 4:13 
AnswerRe: How to Export to CSV file from DataSet? Pin
Michael P Butler29-Jul-04 4:35
Michael P Butler29-Jul-04 4:35 
QuestionHow hard will it be to move to C# 2.0 ? Pin
Andres Coder29-Jul-04 3:02
Andres Coder29-Jul-04 3:02 
AnswerRe: How hard will it be to move to C# 2.0 ? Pin
SOCM_FP_CPP29-Jul-04 3:11
SOCM_FP_CPP29-Jul-04 3:11 
GeneralRe: How hard will it be to move to C# 2.0 ? Pin
Nick Parker29-Jul-04 5:07
protectorNick Parker29-Jul-04 5:07 
GeneralRe: How hard will it be to move to C# 2.0 ? Pin
SOCM_FP_CPP29-Jul-04 19:42
SOCM_FP_CPP29-Jul-04 19:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.