Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a made a calendar in VB.net. It is a FlowLayoutPanel filled with usercontrols. I have the autoscroll set which shows vertically.

Screenshot of the calendar:
Calendar[^]

After all the usercontrols are populated into the flowlayoutpanel, I have the below code running. For testing purposed I'm just trying to get it to move at all. The first MessageBox appears to be showing the correct position and maximum (0 9221) as the maximum increases or decrease as expected after removing controls. However when I set the scroll, it stays positioned at top once loaded and the next messagebox that shows still says 0.



VB
MessageBox.Show(FlowLayoutPanel1.VerticalScroll.Value & " " & FlowLayoutPanel1.VerticalScroll.Maximum)

FlowLayoutPanel1.VerticalScroll.Value = FlowLayoutPanel1.VerticalScroll.Value + 500

MessageBox.Show(FlowLayoutPanel1.VerticalScroll.Value)


Any help is appreciated!

What I have tried:

I tried disabling autoscroll on the flowlayoutpanel and adding the scrollbars through code but the bar would not show up at all.

End goal is for it to auto scroll to the current week when loaded.
Posted
Updated 25-May-23 5:10am

Answering my own question. I added the Form_Shown Event and appears to work now. Also added in my scroll position code for my end goal which scrolls to current week(or close to). Thanks!

Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
     Dim dateNow = DateTime.Now
     Dim dfi = DateTimeFormatInfo.CurrentInfo
     Dim calendar = dfi.Calendar

     Dim weekOfyear = calendar.GetWeekOfYear(
 dateNow,
 dfi.CalendarWeekRule,
 DayOfWeek.Sunday)

     Dim scrollPos As Integer = Convert.ToInt32(FlowLayoutPanel1.VerticalScroll.Maximum * (weekOfyear / 57))
     FlowLayoutPanel1.VerticalScroll.Value = scrollPos


 End Sub
 
Share this answer
 
ScrollControlIntoView method will do what you want. Here is a test:
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        for (int i = 0; i < 100; i++)
        {
            Label label = new Label();
            label.Size = new Size(100, 100);
            label.BackColor= Color.Aqua;
            label.ForeColor=Color.Black;
            label.Margin = new Padding(10);
            label.Text= i.ToString();
            label.Font = new Font("Arial", 20);

            flowLayoutPanel1.Controls.Add(label);
        }

        flowLayoutPanel1.AutoScroll = true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        flowLayoutPanel1.ScrollControlIntoView(flowLayoutPanel1.Controls[0]);
    }
}

and the form designer:
C#
private void InitializeComponent()
{
    this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // flowLayoutPanel1
    // 
    this.flowLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
    this.flowLayoutPanel1.Location = new System.Drawing.Point(12, 12);
    this.flowLayoutPanel1.Name = "flowLayoutPanel1";
    this.flowLayoutPanel1.Size = new System.Drawing.Size(776, 374);
    this.flowLayoutPanel1.TabIndex = 0;
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(329, 404);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(75, 23);
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";
    this.button1.UseVisualStyleBackColor = true;
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(800, 450);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.flowLayoutPanel1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);

}

Scroll down, then click the button to go back to the first control.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900