将垂直滚动条添加到面板

时间:2022-12-05 22:08:37

I am trying to make a Panel scrollable, but only vertically (so AutoScroll won't work because the child controls go past the left edge and must).

我试图使一个Panel可滚动,但只是垂直(因此AutoScroll将无法工作,因为子控件超过左边缘并且必须)。

So how is this done?

那怎么办?

5 个解决方案

#1


27  

Assuming you're using winforms, default panel components does not offer you a way to disable the horizontal scrolling components. A workaround of this is to disable the auto scrolling and add a scrollbar yourself:

假设您正在使用winforms,默认面板组件不会为您提供禁用水平滚动组件的方法。解决方法是禁用自动滚动并自己添加滚动条:

ScrollBar vScrollBar1 = new VScrollBar();
vScrollBar1.Dock = DockStyle.Right;
vScrollBar1.Scroll += (sender, e) => { panel1.VerticalScroll.Value = vScrollBar1.Value; };
panel1.Controls.Add(vScrollBar1);

Detailed discussion here.

详细讨论在这里。

#2


18  

Try this instead for 'only' scrolling vertical.
(auto scroll needs to be false before it will accept changes)

试试这个“仅”滚动垂直。 (自动滚动在接受更改之前必须为false)

mypanel.AutoScroll = false;
mypanel.HorizontalScroll.Enabled = false;
mypanel.HorizontalScroll.Visible = false;
mypanel.HorizontalScroll.Maximum = 0;
mypanel.AutoScroll = true;

#3


4  

Panel has an AutoScroll property. Just set that property to True and the panel will automatically add a scroll bar when needed.

Panel具有AutoScroll属性。只需将该属性设置为True,面板将在需要时自动添加滚动条。

#4


2  

Below is the code that implements custom vertical scrollbar. The important detail here is to know when scrollbar is needed by calculating how much space is consumed by the controls that you add to the panel.

下面是实现自定义垂直滚动条的代码。这里的重要细节是通过计算添加到面板的控件消耗的空间来了解何时需要滚动条。

panelUserInput.SuspendLayout();
panelUserInput.Controls.Clear();
panelUserInput.AutoScroll = false;
panelUserInput.VerticalScroll.Visible = false;

// here you'd be adding controls

int x = 20, y = 20, height = 0;
for (int inx = 0; inx < numControls; inx++ )
{
    // this example uses textbox control
    TextBox txt = new TextBox();
    txt.Location = new System.Drawing.Point(x, y);
    // add whatever details you need for this control
    // before adding it to the panel
    panelUserInput.Controls.Add(txt);
    height = y + txt.Height;
    y += 25;
}
if (height > panelUserInput.Height)
{
    VScrollBar bar = new VScrollBar();
    bar.Dock = DockStyle.Right;
    bar.Scroll += (sender, e) => { panelUserInput.VerticalScroll.Value =  bar.Value; };
    bar.Top = 0;
    bar.Left = panelUserInput.Width - bar.Width;
    bar.Height = panelUserInput.Height;
    bar.Visible = true;
    panelUserInput.Controls.Add(bar);
}
panelUserInput.ResumeLayout();

// then update the form
this.PerformLayout();

#5


-2  

Add to your panel's style code something like this:

添加到面板的样式代码如下:

<asp:Panel ID="myPanel" runat="Server" CssClass="myPanelCSS" style="overflow-y:auto; overflow-x:hidden"></asp:Panel>

#1


27  

Assuming you're using winforms, default panel components does not offer you a way to disable the horizontal scrolling components. A workaround of this is to disable the auto scrolling and add a scrollbar yourself:

假设您正在使用winforms,默认面板组件不会为您提供禁用水平滚动组件的方法。解决方法是禁用自动滚动并自己添加滚动条:

ScrollBar vScrollBar1 = new VScrollBar();
vScrollBar1.Dock = DockStyle.Right;
vScrollBar1.Scroll += (sender, e) => { panel1.VerticalScroll.Value = vScrollBar1.Value; };
panel1.Controls.Add(vScrollBar1);

Detailed discussion here.

详细讨论在这里。

#2


18  

Try this instead for 'only' scrolling vertical.
(auto scroll needs to be false before it will accept changes)

试试这个“仅”滚动垂直。 (自动滚动在接受更改之前必须为false)

mypanel.AutoScroll = false;
mypanel.HorizontalScroll.Enabled = false;
mypanel.HorizontalScroll.Visible = false;
mypanel.HorizontalScroll.Maximum = 0;
mypanel.AutoScroll = true;

#3


4  

Panel has an AutoScroll property. Just set that property to True and the panel will automatically add a scroll bar when needed.

Panel具有AutoScroll属性。只需将该属性设置为True,面板将在需要时自动添加滚动条。

#4


2  

Below is the code that implements custom vertical scrollbar. The important detail here is to know when scrollbar is needed by calculating how much space is consumed by the controls that you add to the panel.

下面是实现自定义垂直滚动条的代码。这里的重要细节是通过计算添加到面板的控件消耗的空间来了解何时需要滚动条。

panelUserInput.SuspendLayout();
panelUserInput.Controls.Clear();
panelUserInput.AutoScroll = false;
panelUserInput.VerticalScroll.Visible = false;

// here you'd be adding controls

int x = 20, y = 20, height = 0;
for (int inx = 0; inx < numControls; inx++ )
{
    // this example uses textbox control
    TextBox txt = new TextBox();
    txt.Location = new System.Drawing.Point(x, y);
    // add whatever details you need for this control
    // before adding it to the panel
    panelUserInput.Controls.Add(txt);
    height = y + txt.Height;
    y += 25;
}
if (height > panelUserInput.Height)
{
    VScrollBar bar = new VScrollBar();
    bar.Dock = DockStyle.Right;
    bar.Scroll += (sender, e) => { panelUserInput.VerticalScroll.Value =  bar.Value; };
    bar.Top = 0;
    bar.Left = panelUserInput.Width - bar.Width;
    bar.Height = panelUserInput.Height;
    bar.Visible = true;
    panelUserInput.Controls.Add(bar);
}
panelUserInput.ResumeLayout();

// then update the form
this.PerformLayout();

#5


-2  

Add to your panel's style code something like this:

添加到面板的样式代码如下:

<asp:Panel ID="myPanel" runat="Server" CssClass="myPanelCSS" style="overflow-y:auto; overflow-x:hidden"></asp:Panel>