向面板添加垂直滚动条

发布于 2024-11-09 00:48:08 字数 111 浏览 0 评论 0原文

我正在尝试使 Panel 可滚动,但只能垂直滚动(因此 AutoScroll 不起作用,因为子控件必须越过左边缘)。

那么这是如何做到的呢?

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).

So how is this done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

撕心裂肺的伤痛 2024-11-16 00:48:08

尝试“仅”垂直滚动。
(自动滚动需要为 false 才能接受更改)

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

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

mypanel.AutoScroll = false;
mypanel.HorizontalScroll.Enabled = false;
mypanel.HorizontalScroll.Visible = false;
mypanel.HorizontalScroll.Maximum = 0;
mypanel.AutoScroll = true;
说谎友 2024-11-16 00:48:08

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

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

详细讨论 此处

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:

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

Detailed discussion here.

疏忽 2024-11-16 00:48:08

AutoScroll 确实是解决方案!
您只需将 AutoScrollMargin 设置为 0, 1000 或类似的值,然后使用它向下滚动并在那里添加按钮和项目!

AutoScroll is really the solution!
You just have to set AutoScrollMargin to 0, 1000 or something like this, then use it to scroll down and add buttons and items there!

绿光 2024-11-16 00:48:08

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

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

躲猫猫 2024-11-16 00:48:08

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

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();

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();
无需解释 2024-11-16 00:48:08

3 个步骤:

1- 只需将 AutoScroll 属性设置为 true

2- 在 Form load() 中添加以下内容:

   my Panel Vertical Scroll Maximum = 10000     

3- 在我的面板控件 Add(item) 之后添加以下内容:
无效();
完毕!

3 steps:

1- just set AutoScroll property to true

2- in Form load()add the following:

   my Panel Vertical Scroll Maximum = 10000     

3- after my Panel controls Add(item) add the following:
Invalidate();
Done!

迟到的我 2024-11-16 00:48:08

添加 Kamgman 的答案确实有效。

假设我们要添加一个标签作为面板的子控件:

  • 首先添加面板。将 AutoScroll 设置为 True,将 AutoSize 设置为 False。
  • 在面板中添加标签。将 AutoSize 设置为 true。如果您愿意,您可以给它一个 MinimumSize 宽度,以便它至少保持其水平“形状”。
  • 然后将标签控件的 Anchor 设置为仅 Top。删除其上的左锚点。这可确保标签仅垂直滚动而不是水平滚动。

如果您采用这条路线,则不必添加线条来隐藏水平滚动条。

如果您使用 System.ComponentModel.ComponentResourceManager.ApplyResources.resx 文件而不是 .Designer.cs< 加载它也可能会更好/代码> .因为就我而言,每当我对此特定表单进行编辑时,我都会丢失 Designer.cs 文件中的更改。但这取决于你的项目是如何设置的

Adding on to the answer by Kamgman which does work.

Let's say we were adding a label as the child control to the panel:

  • First add the panel. Set AutoScroll to True and AutoSize to False.
  • Add a label into the panel. Set AutoSize to true. You could give it a MinimumSize for the Width if you like so that it at least keeps its "shape" horizontally.
  • Then set the Anchor for the label control to Top only. Remove the Left anchor on it. This ensures the label only scrolls vertically but not horizontally.

If you go this route you don't have to add the lines to hide the horizontal scroll bar.

It might also be better if you're using System.ComponentModel.ComponentResourceManager.ApplyResources to load it from the .resx file instead of the .Designer.cs . Because in my case whenever I make edits to this particular form I lose the changes in the Designer.cs file. But that will come down to how your project is set up

没︽人懂的悲伤 2024-11-16 00:48:08

添加到面板的样式代码中,如下所示:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文