Drawing to a ListBox

O

Oddball

Ok - I have a ListBox control and I'm ready to write my own DrawItem event handler.

What I want to draw as the item is another control. I have created a user control that I
would like to list in this listbox but I can't for the life of me figure out how to draw the
control inside ListBox...

I get as far as:

private void lbImageList_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
// Set the DrawMode property to draw fixed sized items.
lbImageList.DrawMode = DrawMode.OwnerDrawFixed;
// Draw the background
e.DrawBackground();

// Now draw my control in there... umm....
e.

}

Help! :)


------------------------------------

Another unchecked rambeling brought to you by:

Oddball
joshua@bf#N0SP4M#wd.co.uk
 
G

Guest

If you already have a user control, there is no need to ‘draw’ it within the
listbox, instead, simply reposition the user control to the desired location
on top of the list box.

This same method is often used if someone wants to add their own buttons to
a status bar, they create a new class that inherits from the regular
StatusBar class, and also contain the desired buttons, whenever a given panel
is painted, the button’s size and location is adjusted to the corresponding
region.
 
O

Oddball

Great! I've done that and the controls are now displaying properly.

As a related query I now want the user to be able to click anywhere within my control
and fire the same event. Seeing as how the control is being written on top of the
ListBox I can't fire the usual ListBox events (click, drag, etc.) but my control is made up
of a number of other controls which all have their own events.

How do I get back the standard ListBox controls?! :)

I'm just never happy, am I :)

"=?Utf-8?B?QnJlbmRhbiBHcmFudA==?="
If you already have a user control, there is no need to ‘draw’ it within the
listbox, instead, simply reposition the user control to the desired location
on top of the list box.

This same method is often used if someone wants to add their own buttons to
a status bar, they create a new class that inherits from the regular
StatusBar class, and also contain the desired buttons, whenever a given panel
is painted, the button’s size and location is adjusted to the corresponding
region.


------------------------------------

Another unchecked rambeling brought to you by:

Oddball
joshua@bf#N0SP4M#wd.co.uk
 
G

Guest

I would suggest using a usercontrol that contains all of these sub controls
you want, all of which pipe the events you care about back to the user
control.

As an example, both your custom control and listbox both respond to the
Click event, so have both events trigger the click event of the parent user
control.
 
O

Oddball

I've got a ListBox with a number of user controls which are floating on top of the
control.

The problem I have is the with this method of insterting my user controls into the listbox
I have lost the ability to use the ListBox's inbuilt events (doubleclick, drag, etc).

This wouldn't be a problem if I could fire an event if a user clicks, drags... whatever...
ANYWHERE on my control but as it is I have to add handlers for every control
contained within my user control.

Is it possible to either move the Listbox's behavior to the fore or to add a kind of
invisible panel over my control so that you get the same event fired no matter where
you click?


------------------------------------

Another unchecked rambeling brought to you by:

Oddball
joshua@bf#N0SP4M#wd.co.uk
 
O

Oddball

Could you give me an example of how I would do this?


"=?Utf-8?B?QnJlbmRhbiBHcmFudA==?="
I would suggest using a usercontrol that contains all of these sub controls
you want, all of which pipe the events you care about back to the user
control.

As an example, both your custom control and listbox both respond to the
Click event, so have both events trigger the click event of the parent user
control.


------------------------------------

Another unchecked rambeling brought to you by:

Oddball
joshua@bf#N0SP4M#wd.co.uk
 
G

Guest

For my example, I am using the MouseDown event from 3 buttons, the listbox
and the user control. Within the InitializeComponent() function I have the
following to setup my listening to the specific events within the user
control.

this.listBox1.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMouseDownHandler);
this.button1.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMouseDownHandler);
this.button2.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMouseDownHandler);
this.button3.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMouseDownHandler);
this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMouseDownHandler);

You notice that all of them call the same function, which is written as:

private void MyMouseDownHandler(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.OnMouseDown(sender, e);
}

This makes it simple to choose which events and from what controls should
trigger the events provided by the user control. One warning though, not all
controls support the same events, the Click event for instance does not exist
as part of the ListBox class, but does as part of many others.
 
O

Oddball

But what this doesn't do is allow me to work with 'sender' as if it where the user control
or work with the index of the listbox (because the listbox has not been clicked on.

I've wasted two days trying to work this out now - I'm very, very new to this so I don't
know how everything fits together properly.

Is there NO WAY to use the ListBox as it should be used? Can I definatly not get my
control to draw as if it were within the listbox? It seems like listboxes have been
designed to display items of almost any size, and have then had that ability left out at
the last second. I'm getting stressed now and I'm close to just going back to working
on websites... ASP.net is easyer that all this Windows Forms gubbins!

"=?Utf-8?B?QnJlbmRhbiBHcmFudA==?="
For my example, I am using the MouseDown event from 3 buttons, the listbox
and the user control. Within the InitializeComponent() function I have the
following to setup my listening to the specific events within the user
control.

this.listBox1.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMouseDownHandler);
this.button1.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMouseDownHandler);
this.button2.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMouseDownHandler);
this.button3.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMouseDownHandler);
this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMouseDownHandler);

You notice that all of them call the same function, which is written as:

private void MyMouseDownHandler(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.OnMouseDown(sender, e);
}

This makes it simple to choose which events and from what controls should
trigger the events provided by the user control. One warning though, not all
controls support the same events, the Click event for instance does not exist
as part of the ListBox class, but does as part of many others.


------------------------------------

Another unchecked rambeling brought to you by:

Oddball
joshua@bf#N0SP4M#wd.co.uk
 
G

Guest

I apologize for my non timely response on this.

The major issue is that the ListBox control does not support the kind of
functionality you want to use it for. I agree, it stinks. I too have run into
quite a few things that I wanted to use it for, but that it simply didn’t
support and I didn’t have the time to build my own from scratch. Each time,
my only available option was to scale back what I wanted to do, make it work
and hope to find a better way later.

What it keeps sounding like you need is a ListBox style control that is also
a container, that is, permits controls to be placed within it, not unlike how
a Button or ListBox is placed within a form. I fear that I do not currently
know of such a control, meaning your best bet for having such a thing would
be to have it built, either by you or someone else.

I’m sorry that I wasn’t able to help you find the answers you are looking
for on this.

Brendan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top