但我不确定如何在被调整大小吹走之前获得文本选择
您可以通过 ResizeBegin 事件的形式收集此信息。
private int _start;
private int _length;
private void Form1_ResizeBegin(object sender, EventArgs e)
{
_start = comboBox1.SelectionStart;
_length = comboBox1.SelectionLength;
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
comboBox1.SelectionStart = _start;
comboBox1.SelectionLength = _length;
}
或在组合框调整大小时不断将其设置回来(也可以通过调整大小事件本身的形式来完成,取决于您的喜好)。使用此选项,用户将看不到任何更改。在上面的示例中,在调整表单大小时将选择整个文本。在下面的它不会。
private int _start;
private int _length;
private void Form1_ResizeBegin(object sender, EventArgs e)
{
_start = comboBox1.SelectionStart;
_length = comboBox1.SelectionLength;
}
private void comboBox1_Resize(object sender, EventArgs e)
{
comboBox1.SelectionStart = _start;
comboBox1.SelectionLength = _length;
}
或者,您可以针对不同的事件执行此操作,例如当表单失去焦点时等。
private int _start;
private int _length;
private void Form1_ResizeBegin(object sender, EventArgs e)
{
SaveComboBoxSelectionState(comboBox1);
}
private void comboBox1_Resize(object sender, EventArgs e)
{
SetComboBoxSelection(comboBox1, _start, _length);
}
private void Form1_Deactivate(object sender, EventArgs e)
{
SaveComboBoxSelectionState(comboBox1);
}
private void Form1_Activated(object sender, EventArgs e)
{
SetComboBoxSelection(comboBox1, _start, _length);
}
private void SaveComboBoxSelectionState(ComboBox comboBox)
{
_start = comboBox.SelectionStart;
_length = comboBox.SelectionLength;
}
private void SetComboBoxSelection(ComboBox comboBox, int start, int length)
{
comboBox.SelectionStart = start;
comboBox.SelectionLength = length;
}