I have a combo box that has a list of font families in it. As you can guess I'm making a toolstrip for editing fonts in a rich text box control. The problem is when I change fonts it's resizing my combobox.
scrolling through different fonts causes the combo box to become "jumpy" and some fonts have a huge height which is causing for some hilarious problems.
Exhibit A:
Exhibit B:
Yeh... I'll show the code that I have so far... by the way the combobox is just bound to the font families collection.
void box_SelectedIndexChanged(object sender, EventArgs e)
{
String text = ((Font)box.SelectedItem).Name;
Font font = (Font)box.SelectedItem;
BeginInvoke(new Action(() => box.Text = text));
BeginInvoke(new Action(() => box.Font = font));
}
Anyone have any ideas, if I can't find a solution I can just stop the font from changing and just display the name in the default font.
Using a ToolStripComboBox is the problem here I think. The .NET 2.0 ToolItem classes have a lot of residual, erm, features that never got addressed. WPF sucked the resources away. The tool strip is obviously not handling the resize very well. Nor does it make the rest of the form move down when it gets bigger which is by design.
The canonical font combobox uses owner draw to display the fonts in the dropdown list in their regular style. Without changing the font of the box itself. You really don't want the toolstrip to resize, that's just not a great UI.
The only way I can think of doing it is by creating a custom combobox control and deriving from said control. This will give you access to the variable ownerdraw which gives us a little more flexibility without having to mess around with the ItemHeight property. Hooking into one of the events, which dictate that the value of the control has changed.
You could then have a function like the following to calculate the new layout size:
using (Font font = new Font(this.Font.FontFamily, (float)this.PreviewFontSize))
{
Size textSize;
textSize = TextRenderer.MeasureText("yY", font);
_itemHeight = textSize.Height + 2;
}
I tried all these approaches with little success sadly. However I didn't realize it until today when I looked at how microsoft office implements it. They actually use the same font in the combo box for the selected item no matter what font is selected. So as much as I want to make it more custom I'm just going to use a uniform font for whatever font is shown in the selected index.
Related
I have a Infragistics UltraTextEditor (v.18.2): I set the font property to "Consolas", "10pts" both in the "Appearance" and in the "Font" sections of the control's properties.
The UltraTextEditor displays correctly (i.e. with my font properties) its content, but when I edit the content, font is set to 9pts, which seems to be it's default. I (and my users) would like the font to stay set at 10pts, of course. Is there something I am missing?
Thanks in advance to anyone who will help
The quick answer is that you can avoid this by doing one of two things:
Set the UltraTextBox property of AlwaysInEditMode = True
Set the UltraTextBox property of TextRemderingMode = GDI
The change in spacing (between edit and non-edit mode) is caused by the underlying .NET TextBox, which was created before GDI+. .NET uses GDI+ as it's native mechanism for drawing and the UltraTextBox uses GDI+ when displaying the UltraTextBox in non-edit mode. When entering edit mode, the drawing mechanism changes to the old GDI, which causes a difference in spacing.
Credit: Infragistics Community Forum
I'm developing a small application. I'm trying to use AutoScaleMode = Font and it works like a charm for all my intentions except one, I want some specific controls to have bold text, but then, they don't autoscale when the font size is changed.
Is it possible to change the default font of a control but still AutoScale as the rest of the controls?
Thanks in advance
You are probably using font scaling to do a job it was not intended to do. It was designed to compensate for a different video DPI on the target machine. And yes, you can also use it to rescale your Form by changing the form's Font property. But then you'll run into trouble with controls that don't "inherit" their Parent's font. You have to update their Font property yourself.
Doing this automatically requires iterating the controls inside-out, updating only those that don't inherit their parent's font. This worked well:
public static void ScaleFonts(Control ctl, float multiplier) {
foreach (Control c in ctl.Controls) ScaleFonts(c, multiplier);
if (ctl.Parent == null || ctl.Parent.Font != ctl.Font) {
ctl.Font = new Font(ctl.Font.FontFamily,
ctl.Font.Size * multiplier, ctl.Font.Style);
}
}
Sample usage:
private void Form1_Load(object sender, EventArgs e) {
ScaleFonts(this, 1.25f);
}
A possible failure-mode is triggering layout events while doing this, getting the layout messed up. That's hard to reason through, you may need to call Suspend/ResumeLayout() to fix this.
I'm trying to use the RichTextBox (that I've modified a bit with some additions found here and there) so that when I print, my white text becomes black.
To be more precise, I have a RichTextBox with a PrintDocument, PrintPreviewDialog, and so on. I can print without problem with this setup. The only thing is that my application has a dark theme (it is made to be used mainly by night) and the RichTextBox has a black background and the default text is white.
Therefore, when I print (or preview), the white text stays white and it can't be seen when printed...
I would then like to know how I would need to modify my components to change the font color from white to black upon printing. I do not care about other colors (they are the assumed choice of the user) that will be printed fine anyway.
Thanks so much for your pointers on this!
Put this code in your print handler,
var selection = myRichTextBox.Selection;
if (!selection.IsEmpty)
richTextBox1.SelectionColor = Color.Black;
With reference to my previous question here: Changing Font Size for ListView Column in C#. I would like to know how to change the height of the column within a listView with OwnerDraw enabled.
I have succeeded in changing the font using the following code.
using (Font headerFont =
new Font("Helvetica", 10, FontStyle.Bold)) //Font size!!!!
{
e.Graphics.DrawString(e.Header.Text, headerFont,
Brushes.Black, e.Bounds, sf);
}
Although I cannot change the size of the column, giving it a cut-off effect. I have been playing around with the Rectangle.Bounds property but this appears to be read only.
Any suggestions?
Thanks
I have decided to take an alternative approach for my application. I chose to remove the header altogether and replace it with labels within a Container Panel.
This was achieved by changing the HeaderStyle property of the listView to "None". The result allowed me to dock the labels to the top of my listView, giving me the larger text I've been after!
Granted this is a little different to the question asked but provides a simple solution to what appears to be a complex problem! Additionally this would make the column headers static so may not be useful for developing applications requiring numerous changes
Thanks for all your help, and let me know if you would like more detail
Euan
You can try Better ListView Express. It allows changing column header height to arbitrary sizes. It is also free and the usage is 99% same as ListView.
Is there a way to color parts of ListBox Items (not only whole line)?
For example listbox item consists of 5 words and only one is colored or 3 of 5.
Is there a way to do the same with ListView? (I know that ListView can be colored per column but i would like to have multiple colors in one column).
I am interested in only free solutions, and preferred that they are not heavy to implement or change current usage (the least effort to introduce colored ListBox in place of normal one the better).
With regards,
MadBoy
This article tells how to use DrawItem of a ListBox with DrawMode set to one of the OwnerDraw values. Basically, you do something like this:
listBox1.DrawMode = OwnerDrawFixed;
listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
// TODO: Split listBox1.Items[e.Index].ToString() and then draw each separately in a different color
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold),new SolidBrush(color[e.Index]),e.Bounds);
}
Instead of the single DrawString call, Split listBox1.Items[e.Index].ToString() into words and make a separate call to DrawString for each word. You'll have to replace e.bounds with an x,y location or a bounding rectangle for each word.
The same approach should work for ListView.
There is no built-in API which supports this type of modification to a WinForms ListBox or ListView. It is certainly possible to achieve this but the solution will involve a lot of custom painting and likely overriding WndProc. This will be a very involved and heavy solution.
If this type of experience is important to your application I think you should very seriously consider WPF as a solution. WPF is designed to provide this type of eye candy and there are likely many samples on the web to get you up and running.