Winforms 中可调整大小的文本框 [英] Resizable Textbox in Winforms

查看:70
本文介绍了Winforms 中可调整大小的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何让用户在运行时自己调整文本框的大小.最好是文本框边框上的小标签会弹出,它们可以根据大多数应用程序拖动来调整大小.

I want to know how to let a user resize a textbox themselves at runtime. Preferably the little tabs on the borders of the textbox would popup and they can drag to resize as per most apps.

是否可以使用 winforms 本地执行此操作?如果没有,是否有图书馆可以帮助做到这一点?

Is it possible to do this natively using winforms? if not is there a library to help do it?

如果可能,我宁愿使用本机组件.我所有的谷歌搜索都出现了误报.

I would rather use native components if possible. All my google searches turn up false positives.

推荐答案

使用本机组件最简单的解决方案是使用 textbox 并添加 MouseEvents.这是一个示例,可让您在垂直方向拖动 TextBox 的底部区域.当然,如果你想制作一个pop-up,你应该实现一些更像是改变光标的句柄和重新绘制一些区域的东西.

The simpliest solution by using the native components would be implementing you own custom-control using the textbox and adding MouseEvents. Here is a sample that lets you drag the TextBox's bottom area, in vertical direction. Of course you should implement something more like changing the cursor's handle and repainting some areas if you would like to make a pop-up.

这是一个工作概念:

bool isDrag = false;
int lastY = 0;
private void textBox1_MouseEnter(object sender, EventArgs e)
{
    //Change cursor to dragging handle or implement a pop-up
}

private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
    //Just add 5px padding
    if (e.Y >= (textBox1.ClientRectangle.Bottom - 5) &&
        e.Y <= (textBox1.ClientRectangle.Bottom + 5))
    {
        isDrag = true;
        lastY = e.Y;
    }
}

private void textBox1_MouseMove(object sender, MouseEventArgs e)
{
    if( isDrag)
    {
        textBox1.Height += (e.Y - lastY);
        lastY = e.Y;
    }
}

private void textBox1_MouseUp(object sender, MouseEventArgs e)
{
    if (isDrag)
    {
        isDrag = false;
    }
}

要尝试代码,请在新表单上创建一个名为 textBox1TextBox 并连接所有 MouseEvents.尝试将鼠标放在 TextBox 的底部并拖动到顶部或底部.

To try the code, on a new form, create a TextBox named textBox1 and wire all the MouseEvents. Try to bring your mouse on the bottom of the TextBox and drag either going top or bottom.

并且不要忘记将 TextBox.Multiline 设置为 true.

And do not forget to set TextBox.Multiline to true.

这篇关于Winforms 中可调整大小的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆