473,597 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing the font in a listbox

Dom
In some programs, I see that certain items in a listbox have been
grayed, to indicate that they are "there" but can not be chosen. This
is very helpful. Can you do this in CSharp? Is there a
lstMain.Selecte dItem.Enabled?

Dom

Jul 27 '07 #1
6 4301
On Fri, 27 Jul 2007 21:44:24 +0200, Dom <do********@gma il.comwrote:
In some programs, I see that certain items in a listbox have been
grayed, to indicate that they are "there" but can not be chosen. This
is very helpful. Can you do this in CSharp? Is there a
lstMain.Selecte dItem.Enabled?

Dom

No, but you can make it appear non selectable by ownerdrawing the ListBox.

[pseudocode]
OnDrawItem(Draw ItemEventArgs e)
{
MyObject o = (MyObject)listM ain.Items[e.Index];
if( o.NotSelectable )
// DrawGrayedOut
else
// DrawNormal
}

--
Happy coding!
Morten Wennevik [C# MVP]
Jul 27 '07 #2
Dom wrote:
In some programs, I see that certain items in a listbox have been
grayed, to indicate that they are "there" but can not be chosen. This
is very helpful. Can you do this in CSharp? Is there a
lstMain.Selecte dItem.Enabled?

I don't believe that the ListBox class supports this directly. I
haven't tried this, but I think you might be able to do it yourself by
deriving a new class from ListBox and in that class:

* Keep your own list of disabled items.

* Override OnDrawItem, and when drawing a disabled item, change the
font color before calling the base.OnDrawItem () method and then
restoring the font color to what it was before.

* Override WndProc, and before allowing a mouse click to be passed
to the base.WndProc() method, use the IndexFromPoint( ) method to
determine the item being clicked, and don't call the base.WndProc()
method if it's a disabled item.

* Override ProcessDialogKe y, and just as you intercept mouse
events, intercept the keyboard events that might select a disabled item,
and do something to prevent that (IMHO the most user-friendly way would
be to repeat the keyboard events as necessary to skip over disabled
items, but there are a variety of ways you could choose to do this).

That's the general idea...I may have missed some specifics, or it may
turn out that there are better ways to do some of the above. As you can
see, it would be non-trivial to implement the behavior you want, but it
is probably less difficult to do that than to write a whole new listbox
class that does what you want. :)

Pete
Jul 27 '07 #3
Dom
On Jul 27, 3:57 pm, "Morten Wennevik [C# MVP]"
<MortenWenne... @hotmail.comwro te:
On Fri, 27 Jul 2007 21:44:24 +0200, Dom <dolivas...@gma il.comwrote:
In some programs, I see that certain items in a listbox have been
grayed, to indicate that they are "there" but can not be chosen. This
is very helpful. Can you do this in CSharp? Is there a
lstMain.Selecte dItem.Enabled?
Dom

No, but you can make it appear non selectable by ownerdrawing the ListBox.

[pseudocode]
OnDrawItem(Draw ItemEventArgs e)
{
MyObject o = (MyObject)listM ain.Items[e.Index];
if( o.NotSelectable )
// DrawGrayedOut
else
// DrawNormal

}

--
Happy coding!
Morten Wennevik [C# MVP]
This led me to something in the help files. Thanks. Before I get
started, how do I trigger the OnDrawItem? Let me give you a fuller
picture.

I have two listboxes, side by side. One is the "Not included" list,
and the other is the "Included" list. The user can move an item back
and forth between these listboxes by buttons. After an item moves
from the "Not included" list to the "included list", I want it to
appear grayed out, indicating that it is already used up.

So I guess I have to get the button to trigger the OnDrawItem event.
How is that done?

Dom

Jul 27 '07 #4
Dom wrote:
I have two listboxes, side by side. One is the "Not included" list,
and the other is the "Included" list. The user can move an item back
and forth between these listboxes by buttons. After an item moves
from the "Not included" list to the "included list", I want it to
appear grayed out, indicating that it is already used up.

So I guess I have to get the button to trigger the OnDrawItem event.
How is that done?
The easiest way is to simply invalidate the entire control. See the
Invalidate() method. OnDrawItem is a method, not an event, and it gets
called when the control needs to be redrawn, in response to invalidation
of the control.

Personally, as a UI paradigm I think it's better to remove the items
altogether rather than disabling them. This fits better with the "move"
paradigm that you seem to be using already. And as an added benefit,
implementing that would be a LOT easier than dealing with disabling the
list items (I've done the former myself, and it's only 20-30 lines of
code, none of them nearly as tricky as overriding basic Window message
event logic in the control).

Pete
Jul 27 '07 #5
On Fri, 27 Jul 2007 22:33:41 +0200, Dom <do********@gma il.comwrote:
On Jul 27, 3:57 pm, "Morten Wennevik [C# MVP]"
<MortenWenne... @hotmail.comwro te:
>On Fri, 27 Jul 2007 21:44:24 +0200, Dom <dolivas...@gma il.comwrote:
In some programs, I see that certain items in a listbox have been
grayed, to indicate that they are "there" but can not be chosen. This
is very helpful. Can you do this in CSharp? Is there a
lstMain.Selecte dItem.Enabled?
Dom

No, but you can make it appear non selectable by ownerdrawing the ListBox.

[pseudocode]
OnDrawItem(Dra wItemEventArgs e)
{
MyObject o = (MyObject)listM ain.Items[e.Index];
if( o.NotSelectable )
// DrawGrayedOut
else
// DrawNormal

}

--
Happy coding!
Morten Wennevik [C# MVP]

This led me to something in the help files. Thanks. Before I get
started, how do I trigger the OnDrawItem? Let me give you a fuller
picture.

I have two listboxes, side by side. One is the "Not included" list,
and the other is the "Included" list. The user can move an item back
and forth between these listboxes by buttons. After an item moves
from the "Not included" list to the "included list", I want it to
appear grayed out, indicating that it is already used up.

So I guess I have to get the button to trigger the OnDrawItem event.
How is that done?

Dom

OnDrawItem is enabled when you set DrawMode to either of the OwnerDraw modes (OwnerDrawFixed is what you need here). In addition you need to subscribe to the ListBox' DrawItem event. The code below demonstrates theprinciples. You can still select the item, though, but it won't appearselected. To simulate an item that should be disabled I used three string items "one", "two", and "three" and item "two" appears with gray background and text.

protected override void OnLoad(EventArg s e)
{
listBox1.DrawMo de = DrawMode.OwnerD rawFixed;
listBox1.DrawIt em += new DrawItemEventHa ndler(listBox1_ DrawItem);
}

void listBox1_DrawIt em(object sender, DrawItemEventAr gs e)
{
if (listBox1.Items[e.Index].ToString() == "Two")
{
e.Graphics.Fill Rectangle(Brush es.Gray, e.Bounds);
e.Graphics.Draw String(listBox1 .Items[e.Index].ToString(), listBox1.Font, SystemBrushes.I nactiveCaptionT ext, e.Bounds);
}
else
{
e.DrawBackgroun d();
if ((e.State & DrawItemState.F ocus) 0)
e.Graphics.Draw String(listBox1 .Items[e.Index].ToString(), listBox1.Font, SystemBrushes.H ighlightText, e.Bounds);
else
e.Graphics.Draw String(listBox1 .Items[e.Index].ToString(), listBox1.Font, SystemBrushes.C ontrolText, e.Bounds);
}
}
--
Happy coding!
Morten Wennevik [C# MVP]
Jul 27 '07 #6
Dom
On Jul 27, 4:55 pm, Peter Duniho <NpOeStPe...@Nn OwSlPiAnMk.comw rote:
Dom wrote:
I have two listboxes, side by side. One is the "Not included" list,
and the other is the "Included" list. The user can move an item back
and forth between these listboxes by buttons. After an item moves
from the "Not included" list to the "included list", I want it to
appear grayed out, indicating that it is already used up.
So I guess I have to get the button to trigger the OnDrawItem event.
How is that done?

The easiest way is to simply invalidate the entire control. See the
Invalidate() method. OnDrawItem is a method, not an event, and it gets
called when the control needs to be redrawn, in response to invalidation
of the control.

Personally, as a UI paradigm I think it's better to remove the items
altogether rather than disabling them. This fits better with the "move"
paradigm that you seem to be using already. And as an added benefit,
implementing that would be a LOT easier than dealing with disabling the
list items (I've done the former myself, and it's only 20-30 lines of
code, none of them nearly as tricky as overriding basic Window message
event logic in the control).

Pete
Yes, I'm starting to think it is better just to remove the item. It's
a shame though, we really should be easy to do
"lstMain.Select edItem.Enabled = false".

Jul 27 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6633
by: Cengiz Ulku | last post by:
Hi all, I know now how to change the font name of a RTF file in a RTB control using, for example: RTextBox1.SelFontName = cmbFonts.Text I have a RTF file -created with Word- displayed in a RTB control. Whenever I change the font type during the runtime by selecting a font name from a combobox -of all system fonts, the text changes its font correctly but I loose some formatting such as a bold text and/or titles become normal text.
2
540
by: Jim M | last post by:
I am having trouble changing any properties of the web form listbox from client side javascript. As an example, all of the lines below seem to cause an error... temp = document.getElementById("dropCategory1").length window.alert("temp = " + temp) document.getElementById("dropCategory1").Items.Clear() Can the web form listbox be seen from client side javascript?
2
2575
by: iainscott | last post by:
Does anyone know how to make a multi-attribute property show in the design time property window of VS IDE in the same way as .Net controls e.g. the Font property has a + sign against it and can be expanded to reveal the child proerties. I am trying to implement a LineStyle property in a control derived form UserControl which has child properties such as LineWidth, Colour, etc. Category attribute is not suitable because this only groups...
2
3421
by: Luca | last post by:
Hello, I'm using a windows form in which there is a standard ListBox control. I want to add/remove objects from the ArrayList associated to the ListBox, and I want the ListBox immediately shows graphically the change: e.g. if I remove an element from the ArrayList object, then that element should not be no more displayed in the list box. Actually i'm using this code below:
1
10557
by: Do | last post by:
Hi, In my listbox, I'd like to change the color of some listitems depending on user security. I tried wrapping the <font color=red></font> around my listitme's .text property, but the tags show up in the page load. What can I do here?
6
1539
by: Brett Miller | last post by:
Hi, I need to change the colour of certain items in a listbox depending on criteria I receive from my dataset? How would this be accomplished? Thanks in advance... BM
7
2951
by: Sakharam Phapale | last post by:
Hi All, How to preserve the old font properties while changing new one? I posted same question 2 months back, but I had very small time then. eg. "Shopping for" is a text in RichTextBox and already formatted as "Shopping" ---Bold "for" -----Regular Now I want to underline whole text by preserving old style i.e. Bold and
0
1680
by: Dave | last post by:
Hi all, I have a listbox that is complex bound by an arraylist. The problem is that when I delete an object from the arraylist, the listbox does not reflect those changes. I tried refreshing the listbox with no luck and, because it is already bound, I can't just delete the item directly from the listbox without an error (because it already has a datasource, you are not allowed to delete/add anything to the listbox directly). The...
2
1328
by: Dom | last post by:
I often see a listbox in which certain items are grayed, indicating that the item is "there" but disabled. It's a very helpful feature. Can you do that in cSharp? I don't find lstMain.SelectedItem.enabled? Dom
0
7962
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6681
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5844
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3880
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3921
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.