简体   繁体   English

C# WinForms:使面板滚动条不可见

[英]C# WinForms: Make panel scrollbar invisible

I have a panel1 with AutoScroll = true .I have to make panel1 scroll with btnUp and btnDown .我有一个带有AutoScroll = truepanel1 。我必须使用btnUpbtnDown使panel1滚动。 So far I've made what I was asked for到目前为止,我已经完成了我的要求

private void btnUpClicked(Object sender, EventArgs e)
{
    if (panel1.VerticalScroll.Value - 55 > 0)
        panel1.VerticalScroll.Value -= 55;
    else  panel1.VerticalScroll.Value = 0;
}

private void btnDownClicked(Object sender, EventArgs e)
{
    panel1.VerticalScroll.Value += 55;
}

But now I need to hide Scrollbar or make it invisible.但现在我需要隐藏Scrollbar或使其不可见。 I tried我试过

panel1.VerticalScroll.Visible = false;

but it doesn't work.但它不起作用。 Any ideas guys?有什么想法吗?

Ok, I've done the working example of this for you.好的,我已经为您完成了这个工作示例。 All you have to do is to change the max value depending on the total size of all the items inside your panel.您所要做的就是根据面板内所有项目的总大小更改最大值。


Form code:表格代码:

public partial class Form1 : Form
{
    private int location = 0;

    public Form1()
    {
        InitializeComponent();

        // Set position on top of your panel
        pnlPanel.AutoScrollPosition = new Point(0, 0);

        // Set maximum position of your panel beyond the point your panel items reach.
        // You'll have to change this size depending on the total size of items for your case.
        pnlPanel.VerticalScroll.Maximum = 280;
    }

    private void btnUp_Click(object sender, EventArgs e)
    {
        if (location - 20 > 0)
        {
            location -= 20;
            pnlPanel.VerticalScroll.Value = location;
        }
        else
        {
            // If scroll position is below 0 set the position to 0 (MIN)
            location = 0;
            pnlPanel.AutoScrollPosition = new Point(0, location);
        }
    }

    private void btnDown_Click(object sender, EventArgs e)
    {
        if (location + 20 < pnlPanel.VerticalScroll.Maximum)
        {
            location += 20;
            pnlPanel.VerticalScroll.Value = location;
        }
        else
        {
            // If scroll position is above 280 set the position to 280 (MAX)
            location = pnlPanel.VerticalScroll.Maximum;
            pnlPanel.AutoScrollPosition = new Point(0, location);
        }
    }
}

Picture example:图片示例:

图1图2

You have to set AutoScroll option to False on your panel.您必须在面板上将AutoScroll选项设置为False I hope you understand what I've done and will get your panel running the way you want.我希望您理解我所做的并让您的面板按您想要的方式运行。 Feel free to ask if you have any questions.如果您有任何问题,请随时提问。

The Panel control takes on the duty you gave it by setting AutoScroll to true pretty serious. Panel 控件通过将 AutoScroll 设置为 true 来承担您赋予它的职责。 This always includes displaying the scrollbar gadget if it is necessary.始终包括在必要时显示滚动条小工具。 So what you tried cannot work, hiding the vertical scrollbar forces Panel to recalculate layout since doing so altered the client area.因此,您尝试的操作无法正常工作,隐藏垂直滚动条会强制面板重新计算布局,因为这样做会更改客户区。 It will of course discover that the scrollbar is required and promptly make it visible again.它当然会发现需要滚动条并立即使其再次可见。

The code that does this, Panel inherits it from ScrollableControl, is internal and cannot be overridden.执行此操作的代码,Panel 从 ScrollableControl 继承它,是内部的,不能被覆盖。 This was intentional.这是故意的。

So using AutoScroll isn't going to get you anywhere.所以使用 AutoScroll 不会让你到任何地方。 As an alternative, do keep in mind what you really want to accomplish.作为替代方案,请记住您真正想要完成的任务。 You simply want to move controls up and down.您只想上下移动控件。 Easy to do, just change their Location property.很容易做到,只需更改他们的 Location 属性。 That in turn is easiest to do if you put the controls on another panel, big enough to contain them.如果您将控件放在另一个面板上,这又是最容易做到的,面板足够大以包含它们。 Set its AutoSize property to True.将其 AutoSize 属性设置为 True。 And implement you buttons' Click event handlers by simply changing that panel's Location property:并通过简单地更改该面板的 Location 属性来实现按钮的 Click 事件处理程序:

private const int ScrollIncrement = 10;

private void ScrollUpButton_Click(object sender, EventArgs e) {
    int limit = 0;
    panel2.Location = new Point(0, 
        Math.Min(limit, panel2.Location.Y + ScrollIncrement));
}

private void ScrollDownButton_Click(object sender, EventArgs e) {
    int limit = panel1.ClientSize.Height - panel2.Height;
    panel2.Location = new Point(0, 
        Math.Max(limit, panel2.Location.Y - ScrollIncrement));
}

Where panel1 is the outer panel and panel2 is the inner one that contains the controls.其中 panel1 是外部面板, panel2 是包含控件的内部面板。 Be careful when you use the designer to put controls on it, it has a knack for giving them the wrong Parent.当您使用设计器在其上放置控件时要小心,它有给它们错误的父级的诀窍。 Be sure to use the View + Other Windows + Document Layout helper window so you can see this going wrong.请务必使用查看 + 其他窗口 + 文档布局帮助窗口,以便您可以看到这是否出错。 After you filled it, set its AutoSizeMode property to GrowAndShrink so it snaps to its minimum size.填充后,将其 AutoSizeMode 属性设置为 GrowAndShrink,使其对齐到最小尺寸。

您可以使用

FlowLayoutPanel.AdjustFormScrollbars(false);

Try this:尝试这个:

panel.AutoScroll = true;
panel.VerticalScroll.Enabled = false;
panel.VerticalScroll.Visible = false;

Edit:编辑:

Actually when AutoScroll = true;实际上当 AutoScroll = true; It will take care of hscroll and vscroll automatically and you wont be able to change it.它将自动处理 hscroll 和 vscroll,您将无法更改它。 I found this on Panel.AutoScroll Property on MSDN我在MSDN上的 Panel.AutoScroll 属性上找到了这个

 AutoScroll maintains the visibility of the scrollbars automatically. Therefore, setting the HScroll or VScroll property to true has no effect when AutoScroll is enabled.

You may try this to workaround this problem, I have copied it from this Link .你可以试试这个来解决这个问题,我已经从这个链接复制了它。

Behavior Observations 1:行为观察 1:

If AutoScroll is set to true, you can't modify anything in VerticalScroll or HorizontalScroll.如果 AutoScroll 设置为 true,则无法修改 VerticalScroll 或 Horizo​​ntalScroll 中的任何内容。 AutoScroll means AutoScroll; AutoScroll的意思是AutoScroll; the control decides when scrollbars are visible, what the min/max is, etc. and you can't change a thing.控件决定滚动条何时可见、最小值/最大值是多少等等,您无法更改任何内容。 So if you want to customize the scrolling (eg hide scrollbars), you must set AutoScroll to false.因此,如果您想自定义滚动(例如隐藏滚动条),您必须将 AutoScroll 设置为 false。

Looking at the source code for the ScrollableControl with Lutz Roeder's .NET Reflecter, you can see that if AutoScroll is set to true, it ignores your attempts to change property values within the VerticalScroll or HorizontalScroll properties such as MinValue, MaxValue, Visible etc.查看带有 Lutz Roeder 的 .NET 反射器的 ScrollableControl 的源代码,您可以看到,如果 AutoScroll 设置为 true,它会忽略您尝试更改 VerticalScroll 或 Horizo​​ntalScroll 属性(例如 MinValue、MaxValue、Visible 等)中的属性值的尝试。

Behavior Observations 2:行为观察 2:

With AutoScroll set to false, you can change VerticalScroll.Minimum, VerticalScroll.Maximum, VerticalScroll.Visible values.将 AutoScroll 设置为 false,您可以更改 VerticalScroll.Minimum、VerticalScroll.Maximum 和 VerticalScroll.Visible 值。 However, you cannot change VerticalScroll.Value!!!但是,您不能更改 VerticalScroll.Value !!! Wtf!哇! If you set it to a non-zero value, it resets itself to zero.如果您将其设置为非零值,它会将自身重置为零。 Instead, you must set AutoScrollPosition = new Point( 0, desired_vertical_scroll_value );相反,您必须设置 AutoScrollPosition = new Point( 0, desired_vertical_scroll_value ); And finally, SURPRISE, when you assign positive values, it flips them to negative values, so if you check AutoScrollPosition.X, it will be negative!最后,令人惊讶的是,当您分配正值时,它会将它们翻转为负值,因此如果您检查 AutoScrollPosition.X,它将为负值! Assign it positive, it comes back negative.将其分配为正值,它返回为负值。 So yeah, if you want custom scrolling, set AutoScroll to false.所以是的,如果您想要自定义滚动,请将 AutoScroll 设置为 false。 Then set the VerticalScroll and HorizontalScroll properties (except Value).然后设置 VerticalScroll 和 Horizo​​ntalScroll 属性(Value 除外)。 Then to change the scroll value, you need to set AutoScrollPosition, even though you aren't using auto scrolling!然后要更改滚动值,您需要设置 AutoScrollPosition,即使您没有使用自动滚动! Finally, when you set the AutoScrollPosition, it will take on the opposite (ie negative) value that you assign to it, so if you want to retrieve the current AutoScrollPosition later, for example if you want to offset the scroll value by dragging the mouse to pan, then you need to remember to negate the value returned by AutoScrollPosition before reassigning it to AutoScrollPosition with some offset.最后,当您设置 AutoScrollPosition 时,它将采用您分配给它的相反(即负)值,因此如果您想稍后检索当前 AutoScrollPosition,例如如果您想通过拖动鼠标来偏移滚动值要平移,那么您需要记住在将 AutoScrollPosition 返回的值重新分配给 AutoScrollPosition 并带有一些偏移量之前否定它。 WOW.哇。 Wtf.哇。

One other thing, if you are trying to pan with the mouse, use the values of Cursor.Position rather than any mouse locations returned by the mouse events parameters.另一件事,如果您尝试使用鼠标平移,请使用 Cursor.Position 的值而不是鼠标事件参数返回的任何鼠标位置。 Scrolling the control will cause the event parameter values to be offset as well, which will cause it to start firing mouse move events complete with undesired values.滚动控件也会导致事件参数值发生偏移,这将导致它开始触发鼠标移动事件,并带有不需要的值。 Just use Cursor.Position, because it will use mouse screen coordinates as a fixed frame of reference, which is what you want when you're trying to pan/offset the scroll value.只需使用 Cursor.Position,因为它将使用鼠标屏幕坐标作为固定参考框架,这正是您尝试平移/偏移滚动值时所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM