Multiple Fonts in Listboxs. Grids or other screen controls

M

Merrows

I need to build a screen control which can display different words in
different fonts, eg

<bold>This is Bold</bold><notbold>and this is not</notbold>

I have seen that listboxes allow changes per line, but I would need
changes within a line. My guess is that a grid is best option but I
find the events in grids not that easy.

Does anyone have any experience of this?
 
Z

ZokiManas

It's a bit tricky, and you should validate if this solution is solving your
problem.
First you need to change the DrawMode of the list box, and set it to
DrawMode.OwnerDrawVariable.

Then add two event handlers for listBox.DrawItem and listBox.MeasureItem.
In the measure item event handler just set the size, for example:
e.ItemHeight = 22;

Now let see the DrawItem event handler...
void lBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawString("Red", new Font(FontFamily.GenericSansSerif, 8,
FontStyle.Bold), new SolidBrush(Color.Red), e.Bounds);
//insteade of 50, do measure string depending on font size
RectangleF recF = new RectangleF(e.Bounds.Location.X + 50,
e.Bounds.Location.Y, e.Bounds.Width, e.Bounds.Height);
e.Graphics.DrawString("Green", new Font(FontFamily.GenericSansSerif, 8,
FontStyle.Bold), new SolidBrush(Color.Green), recF);
}

But this code on some button click:
lBox.Items.Add(new object());

See what will happen
Hope this helps!
 
M

Merrows

It's a bit tricky, and you should validate if this solution is solving your
problem.
First you need to change the DrawMode of the list box, and set it to
DrawMode.OwnerDrawVariable.

Then add two event handlers for listBox.DrawItem and listBox.MeasureItem.
In the measure item event handler just set the size, for example:
e.ItemHeight = 22;

Now let see the DrawItem event handler...
void lBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawString("Red", new Font(FontFamily.GenericSansSerif, 8,
FontStyle.Bold), new SolidBrush(Color.Red), e.Bounds);
//insteade of 50, do measure string depending on font size
RectangleF recF = new RectangleF(e.Bounds.Location.X + 50,
e.Bounds.Location.Y, e.Bounds.Width, e.Bounds.Height);
e.Graphics.DrawString("Green", new Font(FontFamily.GenericSansSerif, 8,
FontStyle.Bold), new SolidBrush(Color.Green), recF);

}

But this code on some button click:
lBox.Items.Add(new object());

See what will happen
Hope this helps!








- Show quoted text -

What about the equivalent of RTF (windows) on a webpage and the shown
in a grid?
 

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