I have a ListView in Details mode, and I'd like to have some of its items' text in bold (for highlighting where it finds some substring in the item text).
So something like this:
Somefilename/boldText/etc
Is there any way to do that?
Okay, so this is going to be difficult because you're going to have to OwnerDraw your ListView first by setting that property to true. Now that you've done that you're going to need to implement DrawColumnHeader and place this line in it:
e.DrawDefault = true;
Next you'll need to implement the following code, either in DrawItem or DrawSubItem depending on which area it exists in. Keep in mind the code I'm giving you isn't fully complete because it's not parsing the string or anything and further you still need to implement drawing a selected item now because your drawing the text on your own.
var boldFont = new Font(this.Font, FontStyle.Bold);
var location = new PointF(e.Bounds.Location.X, e.Bounds.Location.Y);
e.Graphics.DrawString("Somefilename/", this.Font, Brushes.Black, location);
var size = e.Graphics.MeasureString("Somefilename/", this.Font);
location.X += size.Width;
e.Graphics.DrawString("boldText", boldFont, Brushes.Black, location);
size = e.Graphics.MeasureString("boldText", boldFont);
location.X += size.Width;
e.Graphics.DrawString("/etc", this.Font, Brushes.Black, location);
Another thing to note is that you'll have to play with the offsets a little because some fonts have margins and bolded fonts take up more room.
If the Item is not a SubItem then just implement the e.DrawDefault = true; for the DrawSubItem event - and vica versa if the item is a SubItem then implement that line for DrawItem.
You'll have to override OnPaint. Try these links:
Make portion of a Label's Text to be styled bold
Custom ListView in Winforms?
But, the listview is not painted by the .net framework and is only supported. So, even if you override the OnPaint, it might not be called. So you might have to override your listview control as well.
Related
I am using a tab strip control on a WinForms form. I make use of keyboard accelerators as much as possible. When I set the Text property of a TabPage to e.g. &Documents, the text on the tab is literally &Documents instead of the letter D being underlined.
The help about TabStrip and TabPage doesn't cover this topic. The property ShowKeyboardCues is read-only. Google is helpless.
Can anyone give me a hint how to show accelerators?
How can I set up keyboard shortcuts for a Windows Forms TabControl? gives tips on setting up keyboard shortcuts for it.. In terms of showing an accelerator though, you'll have to draw them yourself
Set the tabControl's DrawMode to OwnerDrawFixed then do some code to draw the tabs, for example (I had to try hard to make it this ugly)
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
//Change appearance of tabcontrol
Brush backBrush = Brushes.Red;
Brush foreBrush = Brushes.Blue;
e.Graphics.FillRectangle(backBrush, e.Bounds);
Rectangle r = e.Bounds;
r = new Rectangle(r.X, r.Y + 3, r.Width, r.Height - 3);
e.Graphics.DrawString("my label", e.Font, foreBrush, r);
var sz = e.Graphics.MeasureString("my ", e.Font);
e.Graphics.DrawString("_", Font, foreBrush, r.X + sz.Width - 2, r.Y + 2);
}
Joking aside, setting up some ugly colors does help you see where the bounds of the drawing area are etc. I'm sure you'll tart it up..
Neither of these controls supports that functionality. It is possible to simulate it, but it is a long and complicated task that I would not recommend because it is non-standard functionality. As a result it is unlikely that anyone would expect it to occur. On the other hand, changing tabs using Ctrl+Tab is standard behaviour so is already automatically supported.
If you did want to do this, you would need to:
Subclass the Control
Override the painting and draw the text on the tab yourself
Get a preview of the keydown event and use that to determine if the key combination you wanted was pressed
Select the correct tab programmatically based on the keypress that you have intercepted
As I say, I would not recommend this because it is not normal behaviour for that control and because the complexity means that it is likely to be buggy. But it is possible...
In .NET 3.5 with winforms I'm making an image thumbnail viewer control.
The main control is derived from a FlowLayoutPanel which takes a list of images and displays them. The images which are displayed are made from a CustomControl on which I paint the and the accompanying label as well as the border of the control.
Images can be selected through clicking and yada yada as you would expect for that kind of control.
Here's a screenshote to illustrate:
That part works fine. The problem is then when I scroll the FlowLayoutPanel derived control the border doesn't redraw properly and there are lines remaining as shown in this screenshot:
I have set both the FlowLayoutPanel and the Images to double buffered. And the images and labels do not have the problem, so I suspect it is something else, but can't figure out what it is.
I think the method used to paint the border of the images might be at fault. Here's the code I use:
protected override void OnPaint(PaintEventArgs e)
{
Rectangle captionContainer;
captionContainer = new Rectangle();
if (!string.IsNullOrEmpty(this.Caption))
captionContainer = this.DrawCaption(e.Graphics);
if (this.Image != null)
this.DrawImage(e.Graphics, captionContainer);
this.Size = new Size(this.Padding.Horizontal + this.ImageSize.Width, this.Padding.Vertical + this.ImageSize.Height + captionContainer.Height);
ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, this.currentBorderColor, ButtonBorderStyle.Solid);
base.OnPaint(e);
}
I'll post more code if needed, but it is pretty lengthy, so I do not want to put too much code unless it actually is necessary.
Can anybody see where this is going wrong?
I have solved by also drawing the border using the Graphics object. Replacing
ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, this.currentBorderColor, ButtonBorderStyle.Solid);
with
e.Graphics.DrawRectangle(new Pen(this.currentBorderColor, 1F), new Rectangle(Point.Empty, new Size(this.Width - 1, this.Height - 1)));
does the trick. No idea why one works and not the other though...
I would like to be able to change the border color of ToolStripComboBox controls in some of my toolstrips, since the default border color of ComboBoxes when used with flat styling is SystemColors.Window, which is basically invisible against the default control color of the toolstrip. After a lot of digging around in Reflector, I don't see any obvious way to do this, since all the infrastructure behind ComboBox rendering is highly protected behind internal and private interfaces.
Outside of ToolStrips, a common solution I've seen proposed for fixing border color on ComboBoxes is to subclass ComboBox, override WndProc, and manually paint the border. This can't work for ToolStripComboBox controls since the internal ComboBox control is its own private subclass of ComboBox, with no way that I can see to replace the instance of the control.
An alternative solution I'm considering is putting one of the extended ComboBox objects into a ToolStripControlHost, which allows me to draw a border, but then I have to give up some of the professional renderer tweaks. A secondary drawback I've noticed is that I get occasional flicker during mouseover.
Switching my design to WPF is not an acceptable solution. Wrapping controls in parent controls for drawing borders is also not acceptable, as this gains nothing over the ToolStripControlHost alternative.
Does anyone have a clever solution to defeat this problem, or is there an existing (permissively-licensed) re-implementation of the ComboBox flat-style rendering stack out in the wild, which fixes some of the shortcomings in the existing implementation?
Here's a way to make it work ... sort of :)
Create an event handler for the Paint event of the ToolStrip. Then loop through all of the ToolStripComboBoxes and paint a rectangle around them.
private Color cbBorderColor = Color.Gray;
private Pen cbBorderPen = new Pen(SystemColors.Window);
private void toolStrip1_Paint(object sender, PaintEventArgs e)
{
foreach (ToolStripComboBox cb in toolStrip1.Items)
{
Rectangle r = new Rectangle(
cb.ComboBox.Location.X - 1,
cb.ComboBox.Location.Y - 1,
cb.ComboBox.Size.Width + 1,
cb.ComboBox.Size.Height + 1);
cbBorderPen.Color = cbBorderColor;
e.Graphics.DrawRectangle(cbBorderPen, r);
}
}
Here's what it looks like (note that you may need to adjust the Height of the ToolStrip to prevent the painted border from being cut off):
improvement:
check the type of the toolstrip item,
so the program will not crush if it is toolstipLabel for example.
foreach (var item in toolStrip1.Items)
{
var asComboBox = item as ToolStripComboBox;
if (asComboBox != null)
{
var location = asComboBox.ComboBox.Location;
var size = asComboBox.ComboBox.Size;
Pen cbBorderPen = new Pen(Color.Gray);
Rectangle rect = new Rectangle(
location.X - 1,
location.Y - 1,
size.Width + 1,
size.Height + 1);
e.Graphics.DrawRectangle(cbBorderPen, rect);
}
}
toolStrip1.ComboBox.FlatStyle = FlatStyle.System;
This sets the default, OS-styled, border around the combo box. It is a light grey and thin border on Windows 10. Although, depending on the background, this may not show. In which case, you could try the other options like FlatStyle.Popup.
If the presets aren't what you are looking for, the other answers allow you to draw a custom border. However, since the rectangle is drawn with +1 pixel size around the combo box, the border is 1 pixel larger than the combo box. Removing the +1s and -1s doesn't work either.
For a project I'm working on, a stopwatch-like timer needs to be displayed to the form. If I remember correctly in VB, text could be outputted to a picture box, but I can't find a way to do it in c#. A label would work, if there is a way to prevent the resizing of the box. Thanks.
A Label would probably be the simplest option. I'm not sure why you would need to prevent resizing of the picturebox (which is also simple to do).
If you are worried about your picturebox being resized and your Label no longer being centered, or being the wrong size, you can just put code in the resize event which dynamically updates the size and location of the Label and its font, based on the current size of the picturebox.
However, if you are determined to not use labels, you can always have your picturebox subscribe to the Paint event and then use e.Graphics to just draw your text whenever the picturebox is repainted.
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Font myFont = new Font("Microsoft Sans Serif", 10))
{
e.Graphics.DrawString("This time is...Hammertime", myFont, Brushes.Black, new Point(0,0));
}
}
However, you would also have to redraw this for every iteration of your timer.
Like I said, Label would be a better option than this. Calling e.Graphics wouldn't give you any real advantage over Label , but it would create more things for you to worry about.
Using a TextBox is probably the most appropriate thing for this; just set ReadOnly to true.
I am struggling to find a way to color the tab headers of a tabpage in WinForms. There are solutions to color the current indexed tab using the OnDrawItem event, but is it possible to color all the tabs with different colors to make them more intuitive for users for certain behavior?
Thanks in advance.
An improved version of Ash's answer:
private void tabControl_DrawItem(object sender, DrawItemEventArgs e)
{
TabPage page = tabControl.TabPages[e.Index];
e.Graphics.FillRectangle(new SolidBrush(page.BackColor), e.Bounds);
Rectangle paddedBounds = e.Bounds;
int yOffset = (e.State == DrawItemState.Selected) ? -2 : 1;
paddedBounds.Offset(1, yOffset);
TextRenderer.DrawText(e.Graphics, page.Text, e.Font, paddedBounds, page.ForeColor);
}
This code uses the TextRenderer class to draw its text (as .NET does), fixes problems with font clipping/wrapping by not negatively inflating the bounds, and takes tab selection into account.
Thanks to Ash for the original code.
Yes, there is no need for any win32 code. You just need to set the tab controls DrawMode property to 'OwnerDrawFixed' and then handle the tab control's DrawItem event.
The following code shows how:
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
// This event is called once for each tab button in your tab control
// First paint the background with a color based on the current tab
// e.Index is the index of the tab in the TabPages collection.
switch (e.Index )
{
case 0:
e.Graphics.FillRectangle(new SolidBrush(Color.Red), e.Bounds);
break;
case 1:
e.Graphics.FillRectangle(new SolidBrush(Color.Blue), e.Bounds);
break;
default:
break;
}
// Then draw the current tab button text
Rectangle paddedBounds=e.Bounds;
paddedBounds.Inflate(-2,-2);
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, this.Font, SystemBrushes.HighlightText, paddedBounds);
}
Setting the DrawMode to 'OwnerDrawnFixed' means each tab button has to be the same size (ie Fixed).
However if you want to change the size of all tab buttons, you can set the tab control's SizeMode property to 'Fixed' and then change the ItemSize property.
Using the current tab control, if it is possible you'd need to hook a lot of win-32 events (there may be a pre-wrapped implementation out there). Another alternative would be a 3rd-party tabbed control replacement; I'm sure plenty of vendors will sell you one.
IMO, you might find it less pain to look at WPF; it is a big change, but has more control over things like this. You can host WPF inside winforms if needed (if you can't justify a full make-over, which is a pretty common reality).