I have a Windows Forms application which contains a couple of labels, a button and a combobox all wrapper inside a Panel.
this.pnlSuboptions.Controls.Add(this.label1);
this.pnlSuboptions.Controls.Add(this.cboPtSize);
this.pnlSuboptions.Controls.Add(this.label2);
this.pnlSuboptions.Controls.Add(this.btnSelect);
I facing an issue with my labels when I try loading localized strings for my labels. The localized strings for some languages are larger than the English strings. In such cases, a part of the label gets hidden under the combo box or the button.
I want the label to increase in size towards the left instead of right. I've set my labels' AutoSize property to true and also played around with the Anchor property but nothing seems to work.
I found an SO link which contains a solution to this problem when the label text changes but I'm sure how I can apply this in my scenario where the label is read only once during the form load.
Any suggestions?
You could put them into a TableLayoutPanel with 2 columns and two rows. Each label goes in left side of each row and both combo box/buttons goes in the others cells (right side of each row).
Then you must dock both elements (Dock Fill) and set the columns to AutoSize. (As you can see in the image)
You also may want to dock the TablePanelLayout to your common panel.
As you can see in the image below, both TablePanelLayout have the same components. But in the secoend I just changed the label3's text.
Hope it helps. (Also sorry for my bad english, it isn't my native language. Please feel free to correct any wrong spelling, thanks!)
First suggestion from me would be:
set the AutoSize property to False
Make sure that the width is alot larger than it needs to be by resizing the label.
The label should now fit nicely regardless of what content it receive.
Second suggestion, you can use the GDI+ to determine the size of the text and then resize the label accordingly See http://msdn.microsoft.com/en-us/library/6xe5hazb(v=vs.110).aspx
Graphics gfx = this.label1.CreateGraphics(); // I think its called that, cant remember :)
Font stringFont = new Font("Arial", 16);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = gfx.MeasureString(this.label1.Text, stringFont);
this.label1.Size = new Size((int)stringSize.Width, (int)stringSize.Height);
Oh yeah I almost forgot. Make sure that you're panel is not the cause of the clipping. I mean, check if the panel is large enough for the labels to fit :-)
Hope this helps!
Best regards,
Zerratar
Related
I have a scrollable DataGridView with text and images. But if it contains more rows then fit the screen scrolling doesn't work properly. If you scroll the part of the text that should become hidden remains on top of the new text. So the top and the bottom of my DataGridView contains double text written on top of each other. The middle part is fine, though.
After the creation of the view I resize both the textsize and the cell size. That might be the problem. I honestly don't really know where to start loking for a solution. Only thing I can come up with is to try a repaint on a scroll event. I've tried this similar issue, but it didn't fix it for me. I also see that a lot of people have issues if they paint in the datagridview, but I don't (So please don't mark this as a duplicate of one of those issues). I didn't write this part of the code, but the picture is just passed on by setting it in DataGridViewCellFormattingEventArgs.Value in dgvAlarms_CellFormatting.
When I tried making it DoubleBuffered everything except the headers and the images (that I cut out in the last picture) in the DataGridView turned black like this:
Thank you for your help.
Ok, I got It to work with a combination of TaW's anwser in the comment and adding
e.CellStyle.BackColor = Color.White;
e.CellStyle.SelectionBackColor = Color.White;
to the beginning of the CellFormatting method.
Funny that the backgroundcolor gets set to black if you use the double buffer, but remains white (like I defined) if you I do not.
Fore note: I know you cannot use a margin with dock, but I am trying to figure a way around this.
I have two objects, a GroupBox (containing loads of buttons and stuff that will always be the same size no matter how big / small the form) and a WebBrowser. The former will take up roughly 100 pixels at the top, and the latter will take up the rest of the space. I have tried multiple ways to get around this, including Panels, GroupBoxs, changing Anchors and Docks, but nothing is working. I know there is a simple solution for this, but I cannot work it out. Could someone point me in the right direction for what I should be using?
P.S. New to WinForms so not very knowledgeable of things.
Start with a TableLayoutPanel control on your Form and set its Dock() property to Fill. Now change the ColumnCount() property to 1, and leave the RowCount() property at 2.
Add your GroupBox to the Top Row and adjust its size. Add your WebBrowser control to the Bottom Row and set its Dock() property to Fill.
Finally, select the TableLayoutPanel, find the Rows() property, and click on the "..." dots to the right. Select Row1 and change its Size Type to AutoSize.
Done!
Alternative Approach...
Add your GroupBox to the Form and set its Dock() property to Top. Add your WebBrowser control and set its Dock() property to Fill. Note with this approach, however, that the GroupBox will extend to fill the full width of the Form.
i'm currently modifying an existing C# WinForm project. I try to arrange some controls inside a GroupBox. However on runtime, they seem to be aligned differently and the Groupbox has a lot more space. Is there some option checked or is this the standard behavior? Any hints are highly appreciated! Thanks!
Here is what it looks like, as you can see there is no way except trial and error to arrange the checkboxes. On runtime there is easily enough space to have four colums in one row, in designer i can hardly fit three without having them overlap.
To prevent the groupbox to resize according to its content, you should make GroupBox.AutoSize to false.
GroupBox groupBox = new GroupBox();
groupBox.AutoSize= false;
check to Size property, and check if someone change it, for the checkbox Location to stay the same in different sizes of the form use anchor property and set it to left or left top.
it's basically must be the size, if the autoSize is off. check what's the starting size, and the size after the form is shown. it shouldn't be the same, but if it is, you can set it to smaller
Check the Anchor property on the checkboxes. Looks like some of them might be anchored to the right.
Not sure it applies to these checkboxes, but AutoScaleMode can impact the scaling of a form. Set to None to ensure it doens't get scaled.
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.
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