GroupBox with panel scroll bar [Resolved]

Posted by Murugavelmsc under C# on 9/2/2013 | Points: 10 | Views : 9066 | Status : [Member] | Replies : 1
Hi Experts,

I have develop a windows form with panel inside the groupbox.

With the button click event generate 100 label box dynamically with in panel (scroll bar).

this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.panel1);
this.groupBox1.Location = new System.Drawing.Point(35, 26);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(237, 108);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "LabelTest";
//
// button1
//
this.button1.Location = new System.Drawing.Point(35, 153);
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);
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.AutoSize = true;
this.panel1.Location = new System.Drawing.Point(7, 20);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(224, 82);
this.panel1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(370, 255);
this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);

Button Click

private void button1_Click(object sender, EventArgs e)
{
int pt = 5;

for (int i = 1; i <= 10; i++)
{
Label lbl = new Label();
lbl.Name = "lSector" + i.ToString();
lbl.Text = "lSector" + i.ToString();

lbl.AutoSize = true;
lbl.Location = new System.Drawing.Point(10, pt);
lbl.Size = new System.Drawing.Size(115, 14);
lbl.Visible = true;
panel1.Controls.Add(lbl);
panel1.AutoSize = true;
panel1.AutoScroll = true;

pt = pt + 10;

}

groupBox1.Controls.Add(panel1);
}

But I could not able to produce the scroll bar.

Please help me out.

Regards,
Murugavel S
murugavel.sadagopan@gmail.com
http://murugavelmsc.blogspot.in/



Responses

Posted by: Bandi on: 9/2/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Just run the following code once...

place a groupbox on your form then add this code
--Form1.cs
using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GroupBoxWithPanel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
int btnPos = 1;
Panel pnl = new Panel();
pnl.AutoScroll = true;
pnl.Top = 15;
pnl.Left = 2;
pnl.Width = groupBox1.Width - 8;
for (int i = 0; i < 22; i++)
{
Button _btn = new Button();
_btn.Text = "lbl";
_btn.Top = btnPos;
btnPos += 23;
pnl.Controls.Add(_btn);
}
groupBox1.Controls.Add(pnl);
}
}
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Murugavelmsc, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response