By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Drawing to a ListBox

P: n/a

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
Nov 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.

"Oddball" wrote:

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

Nov 17 '05 #2

P: n/a

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==?="
<Br**********@discussions.microsoft.com> wrote:
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.

"Oddball" wrote:

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

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

Another unchecked rambeling brought to you by:

Oddball
joshua@bf#N0SP4M#wd.co.uk
Nov 17 '05 #3

P: n/a
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.
"Oddball" wrote:

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==?="
<Br**********@discussions.microsoft.com> wrote:
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.

"Oddball" wrote:

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

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

Another unchecked rambeling brought to you by:

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

Nov 17 '05 #4

P: n/a

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
Nov 17 '05 #5

P: n/a

Could you give me an example of how I would do this?
"=?Utf-8?B?QnJlbmRhbiBHcmFudA==?="
<Br**********@discussions.microsoft.com> wrote:
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.
"Oddball" wrote:

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==?="
<Br**********@discussions.microsoft.com> wrote:
>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.
>
>"Oddball" wrote:
>
>>
>> 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
>>

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

Another unchecked rambeling brought to you by:

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

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

Another unchecked rambeling brought to you by:

Oddball
joshua@bf#N0SP4M#wd.co.uk
Nov 17 '05 #6

P: n/a
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.MyMous eDownHandler);
this.button1.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMous eDownHandler);
this.button2.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMous eDownHandler);
this.button3.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMous eDownHandler);
this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMous eDownHandler);

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.
"Oddball" wrote:

Could you give me an example of how I would do this?
"=?Utf-8?B?QnJlbmRhbiBHcmFudA==?="
<Br**********@discussions.microsoft.com> wrote:
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.
"Oddball" wrote:

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==?="
<Br**********@discussions.microsoft.com> wrote:

>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.
>
>"Oddball" wrote:
>
>>
>> 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
>>
------------------------------------

Another unchecked rambeling brought to you by:

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

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

Another unchecked rambeling brought to you by:

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

Nov 17 '05 #7

P: n/a

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==?="
<Br**********@discussions.microsoft.com> wrote:
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.MyMou seDownHandler);
this.button1.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMou seDownHandler);
this.button2.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMou seDownHandler);
this.button3.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMou seDownHandler);
this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMou seDownHandler);

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.
"Oddball" wrote:

Could you give me an example of how I would do this?
"=?Utf-8?B?QnJlbmRhbiBHcmFudA==?="
<Br**********@discussions.microsoft.com> wrote:
>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.
>
>
>"Oddball" wrote:
>
>>
>> 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==?="
>> <Br**********@discussions.microsoft.com> wrote:
>>
>> >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.
>> >
>> >"Oddball" wrote:
>> >
>> >>
>> >> 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
>> >>
>>
>>
>> ------------------------------------
>>
>> Another unchecked rambeling brought to you by:
>>
>> Oddball
>> joshua@bf#N0SP4M#wd.co.uk
>>

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

Another unchecked rambeling brought to you by:

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

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

Another unchecked rambeling brought to you by:

Oddball
joshua@bf#N0SP4M#wd.co.uk
Nov 17 '05 #8

P: n/a
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
"Oddball" wrote:

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==?="
<Br**********@discussions.microsoft.com> wrote:
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.MyMou seDownHandler);
this.button1.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMou seDownHandler);
this.button2.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMou seDownHandler);
this.button3.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMou seDownHandler);
this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyMou seDownHandler);

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.
"Oddball" wrote:

Could you give me an example of how I would do this?
"=?Utf-8?B?QnJlbmRhbiBHcmFudA==?="
<Br**********@discussions.microsoft.com> wrote:

>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.
>
>
>"Oddball" wrote:
>
>>
>> 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==?="
>> <Br**********@discussions.microsoft.com> wrote:
>>
>> >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.
>> >
>> >"Oddball" wrote:
>> >
>> >>
>> >> 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
>> >>
>>
>>
>> ------------------------------------
>>
>> Another unchecked rambeling brought to you by:
>>
>> Oddball
>> joshua@bf#N0SP4M#wd.co.uk
>>
------------------------------------

Another unchecked rambeling brought to you by:

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

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

Another unchecked rambeling brought to you by:

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

Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.