PC Review


Reply
Thread Tools Rate Thread

copying the listbox behaviour in "Add/Remove Programs"

 
 
Dan Bass
Guest
Posts: n/a
 
      10th Feb 2005
[Problem]
I'm looking to develop a listbox with in-place editing where as each item is
selected, it grows to fit in all the text boxes. When the item is
deselected, it shrinks back to its original size. The editing bit is not a
problem, but I can't get the selected item to resize, as the listbox does
for the "Add/Remove programs".

[Approach]
The initial thought I had was to have a handler for the MeasureItem event,
then have the condition of whether the current index was selected or not,
basing the height on this.
The SelectionChange event would then call a refresh on the list box, which
in turn would cause the MeasureItem event to fire, finally leaving the
DrawItem event handler to draw the items as required.
Is this thinking logical and workable?

[Code]
protected void _listBox_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
if ( e.Index < 0 )
{
selectionPanel.Hide();
return;
}

if ( (e.State & DrawItemState.Selected) ==
DrawItemState.Selected )
{
// The e.Bounds rect appears to remain unchange from the
initial
// MeasureItem event handler settings, and any changes made
// after this seems to be ignored when getting to this
method.

// put the editing panel in the selection spot
selectionPanel.Size = new Size ( e.Bounds.Width,
e.Bounds.Height );
selectionPanel.Location = new Point ( e.Bounds.Location.X+1,
e.Bounds.Location.Y+1 );
selectionPanel.BackColor = SelectedBackgroundColor;
selectionPanel.Show();

}
else
{
// draw unselected items
DrawInterfaceItem(
((InboundInterface)(_itemArray[e.Index])).name,
((InboundInterface)(_itemArray[e.Index])).type,
((InboundInterface)(_itemArray[e.Index])).messageType,
e );
}
}

private void _listBox_MeasureItem(object sender,
System.Windows.Forms.MeasureItemEventArgs e)
{
// this does indeed fire on each selection change and
// can trace the e.ItemHeight being set for selected
// items as expected.
if ( _listBox.SelectedIndex == e.Index )
{
// set the item height to that of the selection panel
e.ItemHeight = selectionPanel.Height;
}
else
{
// items not selected use a default item height
e.ItemHeight = _defaultItemHeight;
}
}

private void _listBox_SelectedIndexChanged(object sender,
System.EventArgs e)
{
// cause a redraw, and MeasureItem to fire
_listBox.Refresh();
}

Thanks for your time.
Dan.


 
Reply With Quote
 
 
 
 
Dan Bass
Guest
Posts: n/a
 
      10th Feb 2005
I managed to get this to work! Think I feel a article coming on...

It's done in short, on the following principle:
- When you reinsert an item, it fires the MeasureItem event for that item.
- You need to reinsert the newly selected index, as well as the last
selectedIndex...

Hope this helps someone else.

[Code]

bool _updatingSelectedIndex = false;
bool _processingSelectionChange = false;
int _lastSelectedIndex = -1;

private void _listBox_MeasureItem(object sender,
System.Windows.Forms.MeasureItemEventArgs e)
{
if ( _updatingSelectedIndex )
{
e.ItemHeight = selectionPanel.Height;
}
else
{
e.ItemHeight = _defaultItemHeight;
}
}

private void _listBox_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if ( _listBox.Items.Count <= 0 )
{
return;
}

if ( !_processingSelectionChange )
{
_processingSelectionChange = true;

// re-evaulate the last selected item height
if ( _lastSelectedIndex >= 0 )
{
int position = _lastSelectedIndex;
Object item = _listBox.Items[position];
_listBox.Items.RemoveAt(position);
_listBox.Items.Insert(position, item);
}

_lastSelectedIndex = _listBox.SelectedIndex;

if ( !_updatingSelectedIndex && _listBox.SelectedIndex >=
)
{
_updatingSelectedIndex = true;

// re-evaulate the selected item height
int position = _listBox.SelectedIndex;
Object item = _listBox.Items[position];
_listBox.Items.RemoveAt(position);
_listBox.Items.Insert(position, item);
_listBox.SelectedIndex = position;

_updatingSelectedIndex = false;

}

_processingSelectionChange = false;

}

}



"Dan Bass" <Not Listed> wrote in message
news:OUGEQ%(E-Mail Removed)...
> [Problem]
> I'm looking to develop a listbox with in-place editing where as each item
> is selected, it grows to fit in all the text boxes. When the item is
> deselected, it shrinks back to its original size. The editing bit is not a
> problem, but I can't get the selected item to resize, as the listbox does
> for the "Add/Remove programs".
>
> [Approach]
> The initial thought I had was to have a handler for the MeasureItem event,
> then have the condition of whether the current index was selected or not,
> basing the height on this.
> The SelectionChange event would then call a refresh on the list box, which
> in turn would cause the MeasureItem event to fire, finally leaving the
> DrawItem event handler to draw the items as required.
> Is this thinking logical and workable?
>
> [Code]
> protected void _listBox_DrawItem(object sender,
> System.Windows.Forms.DrawItemEventArgs e)
> {
> if ( e.Index < 0 )
> {
> selectionPanel.Hide();
> return;
> }
>
> if ( (e.State & DrawItemState.Selected) ==
> DrawItemState.Selected )
> {
> // The e.Bounds rect appears to remain unchange from the
> initial
> // MeasureItem event handler settings, and any changes made
> // after this seems to be ignored when getting to this
> method.
>
> // put the editing panel in the selection spot
> selectionPanel.Size = new Size ( e.Bounds.Width,
> e.Bounds.Height );
> selectionPanel.Location = new Point (
> e.Bounds.Location.X+1, e.Bounds.Location.Y+1 );
> selectionPanel.BackColor = SelectedBackgroundColor;
> selectionPanel.Show();
>
> }
> else
> {
> // draw unselected items
> DrawInterfaceItem(
> ((InboundInterface)(_itemArray[e.Index])).name,
> ((InboundInterface)(_itemArray[e.Index])).type,
> ((InboundInterface)(_itemArray[e.Index])).messageType,
> e );
> }
> }
>
> private void _listBox_MeasureItem(object sender,
> System.Windows.Forms.MeasureItemEventArgs e)
> {
> // this does indeed fire on each selection change and
> // can trace the e.ItemHeight being set for selected
> // items as expected.
> if ( _listBox.SelectedIndex == e.Index )
> {
> // set the item height to that of the selection panel
> e.ItemHeight = selectionPanel.Height;
> }
> else
> {
> // items not selected use a default item height
> e.ItemHeight = _defaultItemHeight;
> }
> }
>
> private void _listBox_SelectedIndexChanged(object sender,
> System.EventArgs e)
> {
> // cause a redraw, and MeasureItem to fire
> _listBox.Refresh();
> }
>
> Thanks for your time.
> Dan.
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Remove" Option Missing in "Add/Remove Programs" =?Utf-8?B?UEs=?= Windows XP General 5 12th Feb 2006 08:22 AM
"Add or Remove Programs" and "Disk Defragmenter" problems =?Utf-8?B?U3R1YXJ0?= Windows XP Help 0 16th Oct 2005 03:59 PM
Unable to access "my computer" and "add or remove programs". peristoteles Windows XP 2 22nd Jun 2005 06:17 PM
When I try to open Control Panel\"Add/remove programs", I get the message shown in the subject line of this message posting. When I then get in "Add/remove programs", I cannot uncheck box next to "Message Queuing" in Con =?Utf-8?B?cGJhZXJ3YWxk?= Windows XP Help 2 14th Jan 2004 03:11 PM
BUG: Incorrect / inconsistent program behaviour when the "Remove Integer Overflow Checks" option is enabled Ralph Purtcher-Wydenbruck Microsoft VB .NET 3 19th Nov 2003 01:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:31 AM.