I am using a flowlayoutpanel which have a lot of buttons per logic sake. I'm having an issue of when I resize the window, I'm not I'm not able to see all the buttons lined up horizontally when the window gets smaller. Instead as the window gets smaller, the buttons drops down to the next line. Can anyone help me on how to resolve this issue? I just want the buttons to line up horizontally, when the window gets smaller, have a horizontal scrollbar. Below is what I have.
fLayoutPnl.Controls.Add(btn1);
// snipped adding buttons from 2 to 15
fLayoutPnl.Controls.Add(btn16);
fLayoutPnl.Dock = System.Windows.Forms.DockStyle.Top;
fLayoutPnl.Location = new System.Drawing.Point(0, 10);
fLayoutPnl.Name = "fLayoutPnl";
fLayoutPnl.Size = new System.Drawing.Size(1245, 30);
If you dock the flowlayoutpanel on the top, it take the size of the parent control.
So if you want a horizontal scroll, you need to set the AutoScrollMinSize of the form (or usercontrol).
Otherwise, you can do this :
this.AutoScroll = true;
this.fLayoutPnl.Dock = System.Windows.Forms.DockStyle.None;
this.fLayoutPnl.AutoSize = true;
this.fLayoutPnl.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.fLayoutPnl.Location = new System.Drawing.Point(0, 10);
this.fLayoutPnl.Name = "fLayoutPnl";
this.fLayoutPnl.Size = new System.Drawing.Size(1245, 30);
fLayoutPnl.WrapContents = false;
This would solve the issue. If a scroll bar is needed, set the MinimumSize property of the panel, after which the scroll bar should appear
To view all the contents of flow layout panel by scrolling vertically, set AutoScroll property to True and don't forget to set WrapContents property to True.
If the contents are to be viewed by scrolling horizontally, set AutoScroll property to True and don't forget to set WrapContents property to False.
Related
I'm writing an image viewer control in C# inheriting from ScrollableControl. It allows zooming and panning the view on the image and has scroll bars reflecting the visible image area.
I'm using the integrated HorizontalScroll and VerticalScroll and could implement updating them to appear as expected (setting Visible, Maximum, LargeChange andValue most prominently):
SizeF viewSize = new SizeF(ClientSize.Width / Zoom, ClientSize.Height / Zoom);
SizeF freeTopLeft = new SizeF(-_offset.X, -_offset.Y);
SizeF freeBotRight = new SizeF(
viewSize.Width - ImageSize.Width - freeTopLeft.Width,
viewSize.Height - ImageSize.Height - freeTopLeft.Height);
// Horizontal
if (freeTopLeft.Width < 0 || freeBotRight.Width < 0)
{
HorizontalScroll.Visible = true;
HorizontalScroll.Maximum = (int)ImageSize.Width;
HorizontalScroll.LargeChange = (int)viewSize.Width;
HorizontalScroll.Value = (int)-freeTopLeft.Width;
}
else
{
HorizontalScroll.Visible = false;
}
// Vertical same as horizontal using Height, removed for brevity.
When the user drags the scrollbar thumb, I correctly update the displayed part of the image as follows in OnScroll(ScrollEventArgs se):
switch (se.ScrollOrientation)
{
case ScrollOrientation.HorizontalScroll:
_offset.X = se.NewValue;
break;
case ScrollOrientation.VerticalScroll:
_offset.Y = se.NewValue;
break;
}
Refresh();
However, as soon as the user releases the mouse button he dragged the thumb with, both scroll bars jump back to Value 0. It does not create another OnScroll event, so my image still shows the expected part, but the scroll bars "desynced" with what is displayed:
I checked many times if my code somehow manipulates the Value unexpectedly while and after scrolling, but I never set that property erroneously.
I also tried setting the ScrollBar values manually in OnScroll, but it did not help.
What do I need to do to correctly keep the thumb in place? What am I doing wrong?
In my code I draw a rectangle and usually the rectangle is too large for the screen, even when maximised. I have set the form property AutoScroll to true and this doesn't seem to do anything. There won't be anything else on my form except the rectangle painting, how can I implement a vertical and horizontal scroll?
PrintingDesignForm form = new PrintingDesignForm();
form.Paint += (se, pe) => {
var r = new Rectangle(parameters.RectangleXPosition, parameters.RectangleYPosition, (int)Math.Ceiling(parameters.RectangleWidth) * 72, (int)Math.Ceiling(parameters.RectangleLength) * 72);
var brush = new SolidBrush(Color.FromArgb(255, 255, 204));
pe.Graphics.FillRectangle(brush, r);
using (var pen = new Pen(brush.Color, 2))
pe.Graphics.DrawRectangle(pen, r);
};
form.WindowState = FormWindowState.Maximized;
form.Show();
Setting AutoScroll = true on a Control/Form alone will only ensure that all Controls you add/nest to the parent will either show or can be reached by the scrollbars which will show up as needed.
This doesn't do anything for stuff you draw.
To make the drawing scrollable you need to set the AutoScrollMinSize to large enough values.
If you don't know in advance then at least while drawing you should be able to determine them from your data.
In Form Properties Set
AutoScroll = True
My form consists of one splitContainer with two horitzontal panels, several buttons on the top panel and charts on the bottom panel.
When the form loads, the top panel is cut and some of the elements are hidden/cut. Moreover if I resize the form, none of the elements or splitContainer resize.
How can properly do it?
I tried with the autoresize property in Form_Load()
//this.AutoSize = true;
//this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
These are the splitContainer properties
//
// splitContainer1
//
this.splitContainer1.BackColor = System.Drawing.SystemColors.ControlDarkDark;
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.btnPlay);
this.splitContainer1.Panel1.Controls.Add(this.grpOptions);
this.splitContainer1.Panel1.Controls.Add(this.grpDisplay);
this.splitContainer1.Panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.splitContainer1_Panel1_Paint);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.splitContainer1.Size = new System.Drawing.Size(784, 561);
this.splitContainer1.SplitterDistance = 69;
this.splitContainer1.TabIndex = 0;
This is the screenshot:
I suggest you anchor the controls on the splitcontainer
I will recommend putting in a TableLayoutPanel on the top panel and put buttons in the cells. You can then set resize behaviour of rows and columns in TableLayoutPanel either percentage or absolute value. Ensure that Dock property for buttons is set to Fill. This is will ensure smooth and proper resizing of controls. You can go for similar approach for bottom panel as well.
I am not able to hide the Horizontal Scroll-bar of my FlowLayout panel. I am adding this panel dynamically.
I have read the below 3 posts on stack overflow. but not able to get success.
flowlayoutpanel and horizontal scrollbar issue
How do I disable the horizontal scrollbar in a Panel
Scrolling panel using horizontal scroll bar
I have tried the following code.
TableLayoutPanel pannel = new TableLayoutPanel();
pannel.Parent = pnlChart;
pannel.Dock = DockStyle.Fill;
pannel.AutoScroll = true;
pannel.HorizontalScroll.Visible = false;
Did you try this
int vertScrollWidth = SystemInformation.VerticalScrollBarWidth;
pannel.Padding = new Padding(0, 0, vertScrollWidth, 0);
In WinForms I am using a Label to display different messages like success, failure, etc.
I'd like to center that label in the center form. I want a solution that will keep it centered whether there's just one word or a whole sentence in the label.
Set Label's AutoSize property to False, TextAlign property to MiddleCenter and Dock property to Fill.
You will achive it with setting property Anchor: None.
Some minor additional content for setting programmatically:
Label textLabel = new Label() {
AutoSize = false,
TextAlign = ContentAlignment.MiddleCenter,
Dock = DockStyle.None,
Left = 10,
Width = myDialog.Width - 10
};
Dockstyle and Content alignment may differ from your needs. For example, for a simple label on a wpf form I use DockStyle.None.
If you don't want to dock label in whole available area, just set SizeChanged event instead of TextChanged. Changing each letter will change the width property of label as well as its text when autosize property set to True. So, by the way you can use any formula to keep label centered in form.
private void lblReport_SizeChanged(object sender, EventArgs e)
{
lblReport.Left = (this.ClientSize.Width - lblReport.Size.Width) / 2;
}
The accepted answer didn't work for me for two reasons:
I had BackColor set so setting AutoSize = false and Dock = Fill causes the background color to fill the whole form
I couldn't have AutoSize set to false anyway because my label text was dynamic
Instead, I simply used the form's width and the width of the label to calculate the left offset:
MyLabel.Left = (this.Width - MyLabel.Width) / 2;
I wanted to do something similar, but on a form with a background image, I found that when the text in the label changed the repaints were obvious with this method, so I did the following:
* Set the label AutoSize to true and TextAlign to MiddleCenter
Then, each time the text changed (mine was done using a timer) I called the following method:
private Point GetPosition()
{
int y = (this.Height / 2) - (label1.Height / 2);
int x = (this.Width / 2) - (label1.Width / 2);
return new Point(x, y);
}
And set the label's Location property to this return value. This ensured that the label was always in the center of the form when the text changed and the repaints for a full-screen form weren't obvious.
You could try out the following code snippet:
private Point CenterOfMenuPanel<T>(T control, int height=0) where T:Control {
Point center = new Point(
MenuPanel.Size.Width / 2 - control.Width * 2,
height != 0 ? height : MenuPanel.Size.Height / 2 - control.Height / 2);
return center;
}
It's Really Center