Operate FlowLayoutPanel Scroll - c#

I have several pictureboxes added in FlowLayutPanel, There is and index which have a number of picture box im lookin at, i can change this index by buttons.
How can i configure my FlowLayoutPanel.VerticalScroll that i could look only at the picture box with name of my index, and i couldnt look at another picture boxes.
I suggest that i have to change FlowLayoutPanel.VerticalScroll.Minimum and Maximum, but i can`t, if i change minimum - it countinue to be 0. here is a code:
flowLayoutPanel1.Dock = DockStyle.Fill;
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.Controls.Add(pictureBox3);
flowLayoutPanel1.Controls.Add(pictureBox2);
flowLayoutPanel1.Controls.Add(pictureBox1);
flowLayoutPanel1.Controls.Add(pictureBox4);
flowLayoutPanel1.AutoScroll = true;
flowLayoutPanel1.WrapContents = false;
MessageBox.Show( flowLayoutPanel1.VerticalScroll.Maximum.ToString());
this.flowLayoutPanel1.VerticalScroll.Minimum = 300;
MessageBox.Show(flowLayoutPanel1.VerticalScroll.Minimum.ToString());
flowLayoutPanel1.PerformLayout();
How can i do that?

Related

How to reproduce a forum/twitter-like UI on Winform

As a classic forum, threads and replys are displayed on a page, with dark and light and dark and light backcolor.
I am trying to write a client of a forum on windows using winform. It is
At first, I have tried this way:
Add a big panel to the form, let's call it the PARENT PANEL.
Add small panels to the big panel like this:
panel1.Visible = false;
for (int i=0; i<5;i++)
{
Panel parent = new Panel();
parent.Height = 800;
Random ra = new Random();
TextBox p = new TextBox();
p.Text = "fehsuifq";
p.Multiline = true;
p.WordWrap = true;
p.Dock = DockStyle.Fill;
parent.BackColor = Color.FromArgb(ra.Next(0, 254), ra.Next(0, 254), ra.Next(0, 254));
p.BorderStyle = BorderStyle.None;
p.ReadOnly = true;
p.TabStop = false;
p.BackColor = this.BackColor;
parent.Controls.Add(p);
parent.Dock = DockStyle.Top;
panel1.Controls.Add(parent);
}
panel1.Visible = true;
Every panel(tenicially, a control) displays a thread's text and images and others details(like authors or avator).
Images are not shown until it is clicked.
when the image is clicked, it is loaded and the controls's height will change as result.
The PARENT PANEL will contains hundreds of these controls since there will be so many threads. It is and have to be scrollable, obviously.
But if I put a textbox in the control, the scroll wheel no longer work on the PARENT PANEL.If I use a label, it is not selectable.
I think this way can't be more stupid, completely.
So I am looking for a better to do this job, to display hundreds or even thousands threads/replys on winform, which is:
the height is dynamic, because the images inside will not load until it is clicked.
The text inside is selectable (I edited this just to disambiguate)
the PARENT PANEL can response to the mouse's wheel, just like twitter, forums.
So that I can use my scroll wheel to browse all the replys at one time. The loading is a background work.
Look at the picture, when the text is selected, the whole panel is still response to the wheel(just like normal webPage). This is a uwp app and I am not sure if winform can do this.

How do you make a button click event bring about random labels?

I have a project in which when the user clicks the button and a label becomes visible and I have 20 labels and I want to make it display labels randomly on each click
You would first need to get all labels. This is the easiest using LINQ:
var labels = Controls.OfType<Label>().ToArray();
And then randomly make on of them visible:
var random = new Random();
var label = labels[random.Next(0, labels.Count - 1)];
label.Visible = true;

Groupbox adjust width based on text length

I have a c# app (winform) that generates Groupboxes from an xml file and based on what's in the file, populates it with Radio Buttons or CheckBoxes. Each of these groupboxes have a name, some of them are longer and they get cut off half way through.
This is how they are generated.
int nc = groupNodes.Count;
for (int i = 0; i < nc; i++)
{
node = groupNodes[i];
GroupBox box = new GroupBox();
box.AutoSize = true;
box.AutoSizeMode = AutoSizeMode.GrowAndShrink;
box.Text = node.Attributes["name"].Value;
//......
}
I tried using the following,
Size textSize = TextRenderer.MeasureText(box.Text,box.Font);
box.Width = (int)textSize.Width;
and tried the following
box.width = (int)box.text.length;
but none of this made any difference.
I also came across This thread. But since I don't use the PaintEventArgs I'm not sure how this applies to me.
Setting the groupbox width is only one of your problems.
It probably should be done like this:
groupBox.AutoSize = true;
int oWidth = groupBox1.Width;
int tWidth = (int)groupBox.CreateGraphics().
MeasureString(groupBox.Text, groupBox.Font).Width;
if (tWidth > oWidth)
{
groupBox.AutoSize = false;
groupBox.Width = tWidth;
}
Note:
This code make use of the AutoSize property. Which will make the GB wide enough to hold its content ie. the RadioButtons&CheckBoxes with their texts. Therefore these should be set before adjusting the GB sizes.
The result will have AutoSize true for some GB and false for others..
After changing the width of the GBs they will need to be repositioned unless they sit in a FlowLayoutPanel!
Their contents (The RadioButtons etc ought to still sit correctly as you placed them before). You did Add them to the GBs, right? If some don't show, post the code you create them with!
So the order is this:
Create GBs with their Text
Add contents with their Texts
Resize GBs
Reposition GBs (shouldn't be necessary with FLP)

Measuring a string vertically

I have a C# Winform application. In my form, I have a panel and in my panel, I have a label. The label gets created dynamically. I have the following code:
Label label1 = new Label();
label1.MaximumSize = new Size(400, 0);
label1.Location = new Point(posX, posY);
label1.Text = myText;
label1.AutoSize = true;
posY += 15;
Okay, everything is working. The text of the label automatically wraps after 400 pixels. The problem is, I need to create a second label, but how do I know what to set the the Location to? This new label need to be placed just below the first label and the first label might be 1 line long or 5 lines long. any help would be appreciated.
try to place your label within FlowLayoutPanel, set the FlowDirection to Top Down.
I would support the answer which provided by Int3, and another solution is to read the Height of label1 before set the Top of label2.
For example:
label2.Top = label1.Top + label1.Height + 10;
A GridLayout with some rows might be a solution

Winforms, creating padding when using Dock properties

How do I add padding, or some space between the textboxes when using dockstyle.top property?
for(int i =0; i< 10; i++) {
textboxes[i] = new TextBox();
textboxes[i].Dock = DockStyle.Top;
mypanel.Controls.Add(textboxes[i]);
}
The code above puts textboxes right beneath each other. Can't figure this out without using mass panels or fixed positioning. How to do the following?
1) I would like to add around 10-20pixels between boxes.
2) How to change size (height,width) of the textboxes, since when using dockstyle.top it ignores the size commands ?
With DockStype.Top you can't change the width of your TextBoxes, cause they are docked. You can only change the height. But to change the height of a TextBox you have to set the Multiline = true beforehand.
To get the space between the different boxes you have to put each TextBox within a panel, set the TextBox.Dock = Fill, the Panel.Dock = Top and the Panel.Padding = 10. Now you have some space between each TextBox.
Sample Code
for (int i = 0; i < 10; i++)
{
var panelTextBox = CreateBorderedTextBox();
this.Controls.Add(panelTextBox);
}
private Panel CreateBorderedTextBox()
{
var panel = CreatePanel();
var textBox = CreateTextBox();
panel.Controls.Add(textBox);
return panel;
}
private Panel CreatePanel()
{
var panel = new Panel();
panel.Dock = DockStyle.Top;
panel.Padding = new Padding(5);
return panel;
}
private TextBox CreateTextBox()
{
var textBox = new TextBox();
textBox.Multiline = true;
textBox.Dock = DockStyle.Fill;
return textBox;
}
What i forgot, you can also give a try to the FlowLayoutPanel. Just remove the DockStyle.Top from the panels and put them into the FlowLayoutPanel. Also you should set the FlowDirection to TopDown. Maybe this can also help you to solve your problem, too.
Another work around that suits smaller layouts is to just add a Label control afterwards also docked to the Top, which is not AutoSized, Text=" ", Height=your padding. This is quite useful for the odd bit of padding when using the designer.
I know where you're coming from, this is especially frustrating after returning to WinForms from WPF.
I would suggest using a TableLayoutPanel, in which each TextBox would get its own cell, and adjusting the properties of the cells. This should solve both your padding and size problems.
Another alternative would be to use some more complex layout controls, such as the DevExpress ones (not free).

Categories

Resources