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.
Related
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
I wrote this code to add my Labels:
JArray a = JArray.Parse(temp);
Label[] labels = new Label[100];
foreach (JObject o in a.Children<JObject>())
{
foreach (JProperty p in o.Properties())
{
string name = p.Name;
string value = p.Value.ToString();
if (name == "name")
{
labels[counter] = new Label();
//Image i = Image.FromFile("item.jpg");
labels[counter].Text = value;
labels[counter].Image =Image.FromFile("item.jpg");
//labels[counter].Image
//labels[counter].BackColor = Color.Blue;
labels[counter].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
labels[counter].Top = height;
height += 50;
Controls.Add(labels[counter]);
}
}
}
The Image should stretch to the Label Size. How can I do this?
The abilities to show and manipulate images and text are spread out in a rather wild fashion among Winforms controls.
A Label can not stretch its Image.
A PictureBox and a Panel can but they don't show their Text
A Button can do both but will always be a Button, no matter how you style it..
So to get a combination you will need to either owner-draw something:
DrawImage in an overload to get the right size of the image, then add Image to Label
Or DrawString the Text onto a Panel to show it alongside the Image
or you could combine two controls with the right abilities:
You can create a Panel and set its BackgroundImage to your Image and its BackgroundImageLayout=Stretch. Then you can add your Label with its Text set to the Panel's controls collection:
// preparation for testing:
Image image = Image.FromFile("D:\\stop32.png");
Size size = new Size(77, 77);
// create the combined control
// I assume your Label is already there
Panel pan = new Panel();
pan.Size = size;
// or, since the Label has the right size:
pan.Size = label.Size; // use Clientsize, if borders are involved!
pan.BackgroundImage = image;
pan.BackgroundImageLayout = ImageLayout.Stretch;
label.Parent = pan; // add the Label to the Panel
label.Location = Point.Empty;
label.Text = "TEXT";
label.BackColor = Color.Transparent;
// add to (e.g.) the form
pan.Parent = this;
Set Borders as you like..
One more option: If all Images should have the same Size and if it is 256x256 pixels or less you could add them to an ImageList. This will stretch them to the ImageList.ImageSize in a very simple way and you can add them to your Label..
Very simple:
VB
Label1.Image = New Bitmap(Image.FromFile("Screenshot.jpg"), Label1.Size)
C#
Label1.Image = new Bitmap(Image.FromFile("Screenshot.jpg"), Label1.Size);
If you are using WinForms you try try below:
labels[counter].Size =
new Size(labels[counter].Image.Width, labels[counter].Image.Height);
This works perfect for me:
Just set the image in design mode (don't use imagelist) use the "Image" option
if we have label1 as the label where we want the image, put the next line inside the constructor:
label1.Image = new Bitmap(label1.Image, label1.Size);
I was trying the solution of Zibri, but deform the image in my case.
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);
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.
In trying to get some labels in a TableLayoutPanel to move from the top left of their cells to the center of the cells, I'm trying to experiment with adding padding and/or margins.
However, nothing I've tried works. Here's the code I've tried and the results:
// Setting the padding just cuts off the bottom part of the text
//lbl.Padding = new System.Windows.Forms.Padding(1);
// How to set Margin?
//lbl.Margin = new System.Windows.Forms.Margin(1); <- This mimics "Padding" but is not recognized
//lbl.Margin = new Thickness(6); <- This is the only example I could find, but it's for WPF
Try:
lbl.Margin = new Padding(1);
You might also want to do:
lbl.Dock = DockStyle.Fill;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.AutoSize = false;
labelName.Style.Add("Margin", "10px");