【问题标题】:How to do I scroll to side with buttons in WinForms in Visual Studio C#?如何在 Visual Studio C# 中使用 WinForms 中的按钮滚动到一侧?
【发布时间】:2021-04-05 10:17:25
【问题描述】:

我知道如何使用按钮垂直/水平滚动,但我不知道如何向左/向右滚动。

我想要的是,当我单击按钮 LeftScrlBtn 时,我应该向左滚动,当我单击 RightScrlBtn 时,我应该向右滚动。

我的 FlowLayoutPanel 的 AutoScroll 属性设置为 false,因为我不想要滚动条,只想要按钮。

编辑

为了让这篇文章更容易理解,这里是代码:

using System;
using [...];

//rest of the code

private void LeftScrlBtn_Click(object sender, EventArgs e) {
    //Code to scroll to Left
}

private void RightScrlBtn_Click(object sender, EventArgs e) {
    //Code to scroll to Right
}

//I want to know the code to scroll left and the code to scroll right

注意

我想说this question 没有帮助我。

【问题讨论】:

  • 请添加到目前为止的代码,以便我们了解您尝试做什么..
  • 滚动仅在对象大于视图时有效。一个控件有四个属性 1) 顶部 2) 左侧 3) 高度 4) 宽度。因此,当您滚动时,您正在使用这四个属性将对象的一部分插入到视图中。
  • @NoNAME,有更新吗?请检查我的答案是否适合您。

标签: c# .net visual-studio winforms


【解决方案1】:

您可以尝试以下代码,使用按钮滚动 FlowLayoutPanel。

public Form1()
        {
            InitializeComponent();
            // need to disable AutoScroll, otherwise disabling the horizontal scrollbar doesn't work
            flowLayoutPanel.AutoScroll = false;

           // disable horizontal scrollbar
          flowLayoutPanel.HorizontalScroll.Enabled = false;


        }
        public int scrollValue = 0;
        public int ScrollValue
        {
            get
            {


                return scrollValue;
            }
            set
            {
                scrollValue = value;

                if (scrollValue < flowLayoutPanel1.HorizontalScroll.Minimum)
                {
                    scrollValue = flowLayoutPanel1.HorizontalScroll.Minimum;
                }
                if (scrollValue > flowLayoutPanel1.HorizontalScroll.Maximum)
                {
                    scrollValue = flowLayoutPanel1.HorizontalScroll.Maximum;
                }

                flowLayoutPanel1.HorizontalScroll.Value = scrollValue;
                flowLayoutPanel1.PerformLayout();

            }
        }
        private void btnleft_Click(object sender, EventArgs e)
        {
            ScrollValue -= flowLayoutPanel1.HorizontalScroll.LargeChange;

        }

        private void btnright_Click(object sender, EventArgs e)
        {
            ScrollValue += flowLayoutPanel1.HorizontalScroll.LargeChange;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            flowLayoutPanel1.Controls.Add(new Button() { Width = 67, Height = flowLayoutPanel1.Height});
        }

结果:

【讨论】:

    【解决方案2】:

    您可以将所有控件放在一个透明的面板中,并溢出表单的边界(例如,如果表单的宽度为 500 像素,高度为 600 像素,您可以使面板比表单更宽,并将控件放置在其中)。在面板下方留出一些空间并在那里添加您的按钮。

    在 LeftScrlBtn_Click 代码块中添加以下内容:this.panel1.Left--;

    将此添加到 RightScrlBtn_Click:this.panel1.Left++;

    当然,您可以更改面板的增量值,使其滚动得更快。例如,只需将 ++ 替换为 +=5 或 -- 替换为 -=5 即可在点击时滚动 5 个像素。

    这是我做的一个代码示例(进入 Form1.cs):

    using System.Collections.Generic;
    using System.Windows.Forms;
    namespace StackOfAnswer
    {
        public partial class Form1 : Form
        {
          public Form1()
          {
            InitializeComponent();
          }
          private void button1_Click(object sender, EventArgs e)
          {
            //Scrolling left
            this.panel1.Left--;
          }
          private void button2_Click(object sender, EventArgs e)
          {
            //Scrolling right
            this.panel1.Left++;
          }
        }
    }
    

    这是进入 Form1.Designer.cs 的代码:

    namespace StackOfAnswer
    {
       partial class Form1
       {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
    
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
    
        #region Windows Form Designer generated code
    
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.panel1 = new System.Windows.Forms.Panel();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.Black;
            this.panel1.Controls.Add(this.label1);
            this.panel1.Location = new System.Drawing.Point(-3, -1);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(904, 422);
            this.panel1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(24, 427);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "scroll left";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(713, 427);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "scroll right";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.ForeColor = System.Drawing.Color.White;
            this.label1.Location = new System.Drawing.Point(24, 136);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(1561, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = " example example example example example example example example example example example example example example example example example example example example example example example ";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);
    
        }
    
        #endregion
    
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
    }
    }
    

    应该这样做。

    【讨论】:

    • 我无法理解在代码之前编写的代码。你能稍微修改一下你的答案吗?
    • 我已经对答案进行了一些编辑,希望对您有所帮助。再次抱歉格式错误,我是新手。代码应该在 VS2019 中运行良好。
    • @mihaievo,有更新吗?请检查我的答案是否适合您。
    • @JackJJun-MSFT 您的回答也是一个不错的选择!有用。也许OP忘记将一个标记为答案¯_(ツ)_/¯
    猜你喜欢
    • 2019-06-20
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 2010-10-18
    • 2021-11-26
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    相关资源
    最近更新 更多