4

Windows フォーム アプリケーションで、1 行にテキストを入力するための高さから始まるテキスト ボックスを作成しました。しかし、ユーザーがコントロール内にラップされたテキストを入力すると、テキストボックスの高さが自動的に増加するようにしたいと思います。

現在、このテキスト ボックスのプロパティ multiline と wordwrap を true に設定しています。TextChanged イベントを使用して、テキストがいつ折り返されたかを判断しようとしましたが、これに役立つプロパティが見つかりません。Lines プロパティは、折り返されたテキストには何の助けにもなりません。ユーザーがEnterキーを押して新しい行を開始したテキストのみ。

テキストがテキストボックスの幅を超えて折り返されるたびに、テキストボックスの高さを拡張するにはどうすればよいですか?

4

6 に答える 6

7

他の人が投稿したのと同じ種類のアイデアです。これを textChanged イベントに入れます。

Dim s As SizeF = TextRenderer.MeasureText(txt.Text, txt.Font, txt.ClientRectangle.Size, TextFormatFlags.WordBreak)
txt.Height = CInt(s.Height)

ある種の最小の高さが必要で、場合によってはパディングを指定する必要がありますが、これは機能します。

于 2009-07-30T14:42:45.573 に答える
2

代わりにRichTextBoxを使用する場合(私の経験では、多くの癖が付いた不機嫌なコントロールのようなものです)、ContentsResizedイベントを使用できます。これにより、新しい必要なサイズが得られます。

private void HandleContentsResized(object sender, ContentsResizedEvenetArgs e)
{
    int newheight = e.NewRectangle.Height;
}
于 2009-07-30T14:31:22.410 に答える
2

これは、別のプロジェクトのラベルコントロール用に作成したものです。私はどこかでコードプロジェクトからコードを取得しました。テキストボックスへの変更は、ベースの変更と同じくらい簡単です。

public class GrowLabel : Label
{
    private bool _growing;
    //public bool GrowFontSize { get; set; }

    public GrowLabel()
    {
        AutoSize = false;
        //GrowFontSize = false;
    }

    public override sealed bool AutoSize
    {
        get { return base.AutoSize; }
        set { base.AutoSize = value; }
    }

    private void ResizeLabel()
    {
        if (_growing) return;
        try
        {
            _growing = true;

            var sz = new Size(Width, Int32.MaxValue);
            sz = TextRenderer.MeasureText(Text, Font, sz, TextFormatFlags.WordBreak);
            Height = sz.Height;
        }
        finally
        {
            _growing = false;
        }
    }

    protected override void OnTextChanged(EventArgs e)
    {
        base.OnTextChanged(e);
        ResizeLabel();
    }

    protected override void OnFontChanged(EventArgs e)
    {
        base.OnFontChanged(e);
        ResizeLabel();
    }

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        ResizeLabel();
    }
}
于 2009-07-30T14:33:55.837 に答える
2

AdamSane の投稿は役に立ちましたが、テキスト ボックスが大きくなりませんでした。私はいくつかの変更を加えたいと思います。私の改造は以下の通りです:

class GrowTextBox : TextBox
{
    private double m_growIndex = 0.0;
    private Timer m_timer;

    public GrowTextBox()
    {
        AutoSize = false;
        this.Height = 20;

        // Without the timer, I got a lot of AccessViolationException in the System.Windows.Forms.dll.
        m_timer = new Timer();
        m_timer.Interval = 1;
        m_timer.Enabled = false;
        m_timer.Tick += new EventHandler(m_timer_Tick);

        this.KeyDown += new KeyEventHandler(GrowTextBox_KeyDown);
    }

    void GrowTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
        {
            this.SelectAll();
        }
    }

    void m_timer_Tick(object sender, EventArgs e)
    {
        var sz = new Size(Width, Int32.MaxValue);
        sz = TextRenderer.MeasureText(Text, Font, sz, TextFormatFlags.TextBoxControl);

        m_growIndex = (double)(sz.Width / (double)Width);

        if (m_growIndex > 0)
            Multiline = true;
        else
            Multiline = false;

        int tempHeight = (int)(20 * m_growIndex);

        if (tempHeight <= 20)
            Height = 20;
        else
            Height = tempHeight;

        m_timer.Enabled = false;
    }

    public override sealed bool AutoSize
    {
        get { return base.AutoSize; }
        set { base.AutoSize = value; }
    }


    protected override void OnTextChanged(EventArgs e)
    {
        base.OnTextChanged(e);
        m_timer.Enabled = true;
    }

    protected override void OnFontChanged(EventArgs e)
    {
        base.OnFontChanged(e);
        m_timer.Enabled = true;
    }

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        m_timer.Enabled = true;
    }
}
于 2011-08-08T12:12:35.780 に答える
1

以下のコードを使用して、10行目くらいまで成功し、その後1文字ずれますが、これは私にとってはうまくいきます。- 7 や - 12 のような乱数については聞かないでください。これらはパディングと関係があります。

    private void txbDescription_TextChanged(object sender, EventArgs e)
    {
        SizeF s = TextRenderer.MeasureText(txbDescription.Text, txbDescription.Font, txbDescription.ClientRectangle.Size, TextFormatFlags.TextBoxControl);

        int lines = (int)Math.Ceiling((decimal)Convert.ToInt32(s.Width - 7) / ((decimal)txbDescription.Width - 12));

        if (lines == 0)
        {
            txbDescription.Height = 20;
        }
        else
        {
            txbDescription.Height = 20 + (lines - 1) * 13;
        }
    }
于 2013-06-10T18:21:26.747 に答える