问题
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.
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.
回答1:
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
.
Here is a working concept:
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;
}
}
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.
And do not forget to set TextBox.Multiline
to true
.
回答2:
Better approach is to use the Anchor
and Dock
properties to scale controls based on their parent controls. Have a read Manage WinForm controls using the Anchor and Dock properties
Other options are TableLayoutPanel and FlowLayoutPanel base on your requirement.
TableLayoutPanel
The TableLayoutPanel control arranges its contents in a grid. Because the layout is performed both at design time and run time, it can change dynamically as the application environment changes. This gives the controls in the panel the ability to proportionally resize, so it can respond to changes such as the parent control resizing or text length changing due to localization.
Any Windows Forms control can be a child of the TableLayoutPanel control, including other instances of TableLayoutPanel. This allows you to construct sophisticated layouts that adapt to changes at runtime.
FlowLayoutPanel
The FlowLayoutPanel control arranges its contents in a horizontal or vertical flow direction. Its contents can be wrapped from one row to the next, or from one column to the next. Alternatively, its contents can be clipped instead of wrapped.
You can specify the flow direction by setting the value of the FlowDirection property. The FlowLayoutPanel control correctly reverses its flow direction in right-to-left (RTL) layouts. You can also specify whether the contents of the FlowLayoutPanel control are wrapped or clipped by setting the value of the WrapContents property.
来源:https://stackoverflow.com/questions/31845986/resizable-textbox-in-winforms