隐藏winform textBox滚动条并继续滚动

时间:2018-07-31 23:59:10

标签: c# winforms textbox scrollbar

如何隐藏textBox滚动条,但在C#winform应用程序中继续滚动,这种滚动方式不可用:

 textBox.ScrollBars = ScrollBars.None;

我已经尝试过了:

 AutoScroll = false;
 VerticalScroll.Visible = false;

和:

   int textBoxLinesVisible;

        public Form1()
        {
            InitializeComponent();
            textBoxLinesVisible = textBox1.ClientSize.Height / textBox1.Font.Height;
        }

        void textBox1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (textBox1.Lines.Length > textBoxLinesVisible)
            {
                if (e.Delta > 0 && true) SendKeys.Send("{UP}");
                else SendKeys.Send("{DOWN}");
            }
        }

但不会滚动

1 个答案:

答案 0 :(得分:0)

我自己进行搜索之后,最终将TextBox放在Panel中。请记住,普通TextBox的滚动条宽度为17像素,因此,如果Panel的宽度小于17像素,则滚动条应该可以正常工作。但这只是一个简单的解决方法,希望对您有所帮助。

代码:

    private void Form1_Load(object sender, EventArgs e)
    {
        // Panel
        Panel panel = new Panel();
        panel.Location = new Point(12, 12);
        panel.Size = new Size(200, 300);
        panel.BackColor = Color.Orange;

        // TextBox
        TextBox textBox = new TextBox();
        textBox.Location = new Point(0, 0);
        textBox.Size = new Size(217, 300);
        textBox.Multiline = true;
        textBox.ScrollBars = ScrollBars.Vertical;
        textBox.BackColor = Color.FromArgb(70, 70, 80);
        textBox.Text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\nl\r\nm\r\nn\r\no\r\np\r\nq\r\nr\r\ns\r\nt\r\nu\r\nv\r\nw\r\nx\r\ny\r\nz";
        textBox.BorderStyle = BorderStyle.None; // optional

        panel.Controls.Add(textBox);
        this.Controls.Add(panel);
    }

输出:

Image for textBox.BorderStyle = BorderStyle.Fixed3D

Image for textBox.BorderStyle = BorderStyle.None