I have an application that changes the font of every control to SegoeUI when it is run in Vista. It works fine, except for headings of tabpages (the buttons to click when switching from one tab to another).
Tab page headings do not grow vertically to accommodate a larger font size, they always remain the same height.
Is there a property that will allow the TabControl to handle this? (I have tried AutoSizeMode, but it only deals with a tab's width)
If not, what is the best way programmatically to resize the tabpage headings based on the font size?
There is an ItemSize property on the tab control that you can set to change the size of the tabs themselves. Also, to help you out with getting the size of the text, there's a MeasureString() method on the Graphics object that will give you back a SizeF struct with the size of the given text. That can help you determine if you need to change the ItemSize property. Some rough code:
Graphics g = this.tabControl1.TabPages[0].CreateGraphics();
SizeF s = g.MeasureString(this.tabControl1.TabPages[0].Text, this.tabControl1.TabPages[0].Font);
Related
Is it possible to change size of winform and then according to that change also all objects on that form?
For example I have winform where I placed some datagridview, textboxes & labels. The window is for example 800x600. And then let's say he hit MaximizeBox and winform will change size to 1600x900, I would like then make all objects bigger according to that change. I don't want to scale down, so user can't change size below 800x600.
My question is if that's even possible to scale that somehow?
Every control has an Anchor property which can be set to follow the form's size top, bottom, left, and right.
https://msdn.microsoft.com/en-us/library/8y52cxte(v=vs.110).aspx
Forms also have a minimum height and minimum width property that will help you restrict the minimum size of the form.
You can do it via the designer or via code like this:
this.MinimumSize = new Size(800, 600);
I have a form with a tab control.
I want each tab to have its own interface with its own size, so that I can have button layouts as necessary.
I am, in this example, only altering height.
Currently I have the default form height and default tab height set (set by tab index 0).
I need a programmatic way to set each tab's height individually, and on event selectedIndexChanged, I am able to resize the form as needed relative to the currently selected tab, but I don't know how to change each tab's height individually.
How can I achieve this?
It sounds like you are talking more about the height of the form based on the selected tab than the height of each individual tab item.
Assuming the TabControl is Dock-Filled on a parent form, you can try this code to resize the form's height based on the content of the TabPage:
void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
var controls = tabControl1.SelectedTab.Controls.Cast<Control>();
if (controls.Any()) {
this.Height = controls.Max(x => x.Bottom) + 72;
}
}
The routine finds the lowest based control on the TabPage and then adds a fudge number of 72 to account for the height of the form's non-client area and other miscellaneous spacing issues.
But note, constantly changing the height of the form based on a tab selection can be a bit jarring to the end user, and is probably not considered a popular UX implementation.
I'm using TabControl and I have DrawFixed. I just want only draw the tabs not the panel under it.
How can I remove it?
Also I'd like to ask, can I change tabs size? I've long text which I'd like see all if it's selected but I'd like see it cropped if it's not active.
I've following in draw event, but it always draws the tab in the same size.
if (e.State == DrawItemState.Selected)
{
e.Graphics.FillRectangle(Brushes.White, e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
}
else
{
e.Graphics.FillRectangle(Brushes.LightGray, e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
text = text.Length > 10 ? text.Substring(0, 10) + "..." : text;
}
e.Graphics.DrawString(text, e.Font, Brushes.Black, e.Bounds.Left + 17, e.Bounds.Top + 3);
Thanks in advance.
Chronologically in your question, you have asked how to get rid of that bar at the top. If you insisted on using 'faux' tab pages where the tabs merely control content of the fixed set of controls, then shrinking the height of the tab control to a point where that isn't visible is probably an acceptable solution. I just tried it and with some tweaking it looks mostly what I think you are after. For the record I'd recommend actually using the tab pages as intended, that is as hosts to controls, even if you make a custom control that brings together all the controls you want visible. This will fit the tab paradigm much better.
For the second point you'd like to resize the tabs. Impossible. The framework gives two options for DrawStyle, Normal and OwnerDrawFixed. Normal allows Windows to set the tab size based on the text and font, OwnerDrawFixed means the tab size is completely fixed. There is no more control over this. OwnerDrawFixed however gives you access to the OnDrawItem event which is what you are wanting to use for painting the tabs themselves.
It now seems you've bitten the bullet and set UserPaint to True which means you are now doing all of the drawing. I recommend at this point to set DrawStyle back to Normal, then you can kludge some behind-the-scenes text to have Windows control the tab widths automatically. I will warn this won't be very robust since everyone has different font settings and a few pixels off and nothing will draw right.
So here I'll point out TabControl.GetTabRect(index As Integer), the method you can use to get the bounding rectangle of a given tab. I use this in a loop over all the tab indices and then do all the drawing I need for the tab within the rectangle provided from each tab. This means I don't need to use OwnerDrawFixed to get the bounds to paint within.
However still if you want better control, you're 80% the way to just implementing exactly whatever control you want to see, starting from either Control or UserControl. A similar look could be achieved from overlapping buttons with some logic to paint and lay them out. Then you could get all the text appearance you want also. I considered the same myself but didn't because I am still hosting TabPages. Since you're free from that it would be even easier...
Just use page text default property it will auto fix tab size for you according to the size of text.. then paint your text by your self .. if you still want additional space for painting image or some thing else then use padding which is the property of tab control not the tab page. i hope it will help full for you.
I have a user control with picturebox on it. I want the picturebox size to remain the same, but the form that contains the user control changes the hight of the control and the picturebox's height changes. Can I somehow keep the height (and width) constant?
If the picture box is docked or has anchors set, it "follows" the parent's height (and width, if the correct anchor/docking is configured).
Please check whether you set the Dock and Anchor properties correctly. See also this tutorial.
So in winforms, every dropdown combobox has this little arrow thingy to the right that tells the user it's a dropdown, kinda like this:
Now how do I figure out how wide that is in pixels? Reason is, I'm using ControlDrawToBitmap, this doesn't draw the text properly for the combo boxes, and I can redraw the contents, I just whack some of the arrows (which are drawn properly).
First idea that comes to mind: Check to see if the combobox button width tracks with the scrollbar width. The scrollbar width can be modified in user preferences. Use GetSystemMetrics() API to get the width of the various scrollbar pieces. If you change your system scrollbar width and it does not affect the size of a normal combobox, then ignore this.
Second idea: use the edit control's formatting rect to find out what the edit control thinks is the usable display area (minus the combo box). See EM_GETRECT in MSDN.
However, it sounds like this is just a hack workaround for your real problem: If you could get the controls to draw correctly to bitmap, then you wouldn't need this hackery.
I calculated it to be 9 pixels wide in photoshop