c# - 如何在 TabPages/TabControl 中滚动

标签 c# winforms

我有 3 个页面的 tabcontrol。选项卡页中放置了 ListView 。 ListView 可以比标签页本身更大。

我想在标签页上添加滚动条

我尝试使用以下来源解决此问题:

  lvwAlbums.Parent = pctlDatabeheer.TabPages[1];
            lvwAlbums.Left = 0;
            lvwAlbums.Top = 0;
            lvwAlbums.Width = pctlDatabeheer.TabPages[1].Width - 35;
            lvwAlbums.Height = 1000;// pctlDatabeheer.TabPages[1].Height;
            lvwAlbums.SmallImageList = iltListView;
            lvwAlbums.FullRowSelect = true;
            lvwAlbums.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;

 foreach (TabPage _Page in pctlDatabeheer.TabPages)
            {
                _Page.AutoScroll = true;
                _Page.AutoScrollMargin = new System.Drawing.Size(20, 20);
                _Page.AutoScrollMinSize = new System.Drawing.Size(_Page.Width, _Page.Height);
            }

但是滚动条没有显示。我做错了什么?

有人可以帮我吗?

感谢yopu的帮助。

最佳答案

我创建了一个新的 Visual Studio WinForms 项目。将表单设计器完全清空并使用您的代码:

public Form1()
{
    InitializeComponent();

    // Make TabControl
    TabControl tabControl1 = new TabControl();
    tabControl1.TabPages.Add(new TabPage());
    tabControl1.TabPages.Add(new TabPage());
    tabControl1.Dock = DockStyle.Fill;
    this.Controls.Add(tabControl1);

    // Make long ListView and add to first tab
    ListView listView1 = new ListView();
    listView1.Location = new Point(0, 0);
    listView1.Height = 1000;
    tabControl1.TabPages[0].Controls.Add(listView1);

    // Your code
    foreach (TabPage _Page in tabControl1.TabPages)
    {
        _Page.AutoScroll = true;
        _Page.AutoScrollMargin = new System.Drawing.Size(20, 20);
        _Page.AutoScrollMinSize = new System.Drawing.Size(_Page.Width, _Page.Height);
    }
}

工作得很好。我怀疑您还有其他问题,但在没有看到您的代码的情况下我无法看到该问题或对其进行故障排除。

编辑:现在您发布了更多代码,您的问题出在列表框上:

lvwAlbums.Parent = pctlDatabeheer.TabPages[1];
lvwAlbums.Left = 0;
lvwAlbums.Top = 0;
lvwAlbums.Width = pctlDatabeheer.TabPages[1].Width - 35;
lvwAlbums.Height = 1000;
lvwAlbums.SmallImageList = iltListView;
lvwAlbums.FullRowSelect = true;
// Here is the issue!
// Do not anchor to the bottom or scrolling won't work
lvwAlbums.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; 

不要将控件固定在底部。这就是导致你出现问题的原因。您无法锚定到底部然后滚动。其他 anchor 都很好。

关于c# - 如何在 TabPages/TabControl 中滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12583235/

相关文章:

c# - 如何在窗口窗体的datagridview单元格中放置自定义控件

vb.net - 默认表单实例何时创建?

winforms - 我应该使用 Winforms 组合框的 SelectedItem、SelectedText 还是 SelectedValue?

c# - 顶级窗口、子窗口、对话框(模式)窗口和非对话框(无模式)窗口之间有什么区别?

c# - 抽象工厂模式

c# - 程序集不允许部分信任的调用者

c# - 主要权限(属性)如何工作?

c# - 在 C# Windows 应用程序中进行 map 编程

c# - ASP.Net MVC 应用程序看似忽略数据库连接字符串

c# - 我可以在没有 Silverlight 的情况下访问 ExecuteQueryAsync 吗?