自定义 TextBox 仅显示一行

Custom TextBox is showing only one line

提问人:user366312 提问时间:7/11/2023 更新时间:7/11/2023 访问量:13

问:

以下自定义文本框控件未显示其全高。

这条线

this.textBox.Height = this.Height + 100;

给出了这个结果:

enter image description here

但是,如果我不添加 100,它如下所示:

enter image description here

这条线

this.Height = this.textBox.Height;

给出了这个结果:

enter image description here

如何使文本框显示其全高?

namespace WindowsFormsControlLibrary1
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public class CustomTextBox : Control
    {
        private TextBox textBox;
        private RichTextBox lineNumberBox;

        public CustomTextBox()
        {
            // Set properties for the line number box
            this.lineNumberBox = new RichTextBox();
            this.lineNumberBox.ReadOnly = true;
            this.lineNumberBox.BackColor = Color.LightGray;
            this.lineNumberBox.BorderStyle = BorderStyle.None;
            this.lineNumberBox.Width = 30;
            this.lineNumberBox.ScrollBars = RichTextBoxScrollBars.None;

            // Set properties for the text box
            this.textBox = new TextBox();
            this.textBox.Multiline = true;
            this.textBox.BorderStyle = BorderStyle.None;
            this.textBox.ScrollBars = ScrollBars.Vertical;
            this.textBox.TextChanged += TextBox_TextChanged;

            // Add controls to the custom control
            this.Controls.Add(this.lineNumberBox);
            this.Controls.Add(this.textBox);

            // Set the height of the control
            this.textBox.Height = this.Height+100;

            // Set the properties to prevent the control from being docked or auto-sized
            this.Dock = DockStyle.None;
            this.AutoSize = false;

            // Set the anchor and minimum size properties
            this.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
            this.MinimumSize = new Size(0, this.textBox.Height);

            // Set the properties for the text box
            this.textBox.Multiline = true;
            this.textBox.BorderStyle = BorderStyle.None;
        }

        private void TextBox_TextChanged(object sender, EventArgs e)
        {
            // Update the line number box when the text changes
            UpdateLineNumbers();
        }

        private void UpdateLineNumbers()
        {
            // Clear the line number box
            this.lineNumberBox.Clear();

            // Add line numbers to the line number box
            int lineCount = this.textBox.GetLineFromCharIndex(this.textBox.TextLength) + 1;
            for (int i = 1; i <= lineCount; i++)
            {
                this.lineNumberBox.AppendText(i + "\n");
            }

            // Update the position of the line number box
            this.lineNumberBox.Top = this.textBox.Top;
            this.lineNumberBox.Height = this.textBox.Height;
        }

        protected override void OnResize(EventArgs e)
        {
            // Resize the controls when the custom control is resized
            base.OnResize(e);
            this.lineNumberBox.Height = this.textBox.Height;
            this.textBox.Left = this.lineNumberBox.Width;
            this.textBox.Width = this.Width - this.lineNumberBox.Width;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            // Draw a border around the custom control
            base.OnPaint(e);
            ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Black, ButtonBorderStyle.Solid);
        }

        protected override void OnClick(EventArgs e)
        {
            // Add a debug point when the line number box is clicked
            base.OnClick(e);
            if (this.lineNumberBox.ClientRectangle.Contains(this.lineNumberBox.PointToClient(Cursor.Position)))
            {
                int lineNumber = this.lineNumberBox.GetLineFromCharIndex(this.lineNumberBox.GetCharIndexFromPosition(this.lineNumberBox.PointToClient(Cursor.Position))) + 1;
                AddDebugPoint(lineNumber);
            }
        }

        private void AddDebugPoint(int lineNumber)
        {
            // Add a debug point to the text box
            string lineText = this.textBox.Lines[lineNumber - 1];
            if (lineText.StartsWith("//"))
            {
                this.textBox.SelectionStart = this.textBox.GetFirstCharIndexFromLine(lineNumber - 1) + lineText.IndexOf("//") + 2;
            }
            else
            {
                this.textBox.SelectionStart = this.textBox.GetFirstCharIndexFromLine(lineNumber - 1);
            }
            this.textBox.SelectionLength = 0;
            this.textBox.SelectedText = "// ";
        }
    }
}

C# WinForms 自定义控件

评论


答: 暂无答案