B. *_*non 4 c# textbox drag winforms
我希望能够拖动一堆控件并选择某种类型的控件(TextBoxes).
拖动操作完成后,我想显示一个输入框(是的,我必须引用/使用VB .dll),提示用户输入将在每个选定的TextBox中输入的值.
可以这样做吗?(当然,但是怎么样?)
或者是否有其他方法可以完成同样的事情(允许用户快速选择多个控件,然后立即对所有控件执行操作)?
我有这种工作 - "警告"或"陷阱"是我必须向用户弹出MessageBox.Show()才能使其工作.基本上,我:
如果选择了鼠标右键,则在容器(我的情况下为FlowLayoutPanel)MouseDown事件中将布尔值设置为true.
如果选择了鼠标右键,则在容器的MouseUp事件上将相同的布尔值设置为false.
然后,我为该表单上的所有TextBox提供了一个共享的MouseHover事件处理程序,如果布尔值为true,则更改BackColor(在我的情况下,从Window中更改为Gainsboro).
在容器的MouseUp事件中,我还使用一个InputBox(引用/导入/使用VB .dll),请求用户输入"突出显示"的TextBox中常见的值.然后我循环遍历它们,查找具有BackColor的那些,并将用户提供的值分配给它们的Text属性.
瞧!
不幸的是,当你以这种方式赋值时,TextBoxes的Modified属性似乎没有被改变,所以我不得不解决这个问题(明确地将"Save"按钮设置为启用),我不得不添加更多代码来复制我的KeyPressed代码,用于限制用户输入的值.
所以,它当然是可能的,虽然有点笨拙.我还没有决定MessageBox.Show()是一个"bug"还是一个功能,不过......
一个相关的帖子是:为什么只有在之前发生messagebox.show()或断点的情况下才会调用MouseHover事件?
这其实很简单.我假设您想要"选择"控件,就像您在绘画程序中"选择"一些像素一样.这是我写给你的一个例子,它只是选择了TextBox控件.它忽略了其他控件.
namespace WindowsFormsApplication5
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
/// <summary>
/// Main application form
/// </summary>
public partial class Form1 : Form
{
/// <summary>
/// Initializes a new instance of the WindowsFormsApplication5.Form1 class
/// </summary>
public Form1() {
InitializeComponent();
DoubleBuffered = true;
}
private Point selectionStart;
private Point selectionEnd;
private Rectangle selection;
private bool mouseDown;
private void GetSelectedTextBoxes() {
List<TextBox> selected = new List<TextBox>();
foreach (Control c in Controls) {
if (c is TextBox) {
if (selection.IntersectsWith(c.Bounds)) {
selected.Add((TextBox)c);
}
}
}
// Replace with your input box
MessageBox.Show("You selected " + selected.Count + " textbox controls.");
}
protected override void OnMouseDown(MouseEventArgs e) {
selectionStart = PointToClient(MousePosition);
mouseDown = true;
}
protected override void OnMouseUp(MouseEventArgs e) {
mouseDown = false;
SetSelectionRect();
Invalidate();
GetSelectedTextBoxes();
}
protected override void OnMouseMove(MouseEventArgs e) {
if (!mouseDown) {
return;
}
selectionEnd = PointToClient(MousePosition);
SetSelectionRect();
Invalidate();
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
if (mouseDown) {
using (Pen pen = new Pen(Color.Black, 1F)) {
pen.DashStyle = DashStyle.Dash;
e.Graphics.DrawRectangle(pen, selection);
}
}
}
private void SetSelectionRect() {
int x, y;
int width, height;
x = selectionStart.X > selectionEnd.X ? selectionEnd.X : selectionStart.X;
y = selectionStart.Y > selectionEnd.Y ? selectionEnd.Y : selectionStart.Y;
width = selectionStart.X > selectionEnd.X ? selectionStart.X - selectionEnd.X : selectionEnd.X - selectionStart.X;
height = selectionStart.Y > selectionEnd.Y ? selectionStart.Y - selectionEnd.Y : selectionEnd.Y - selectionStart.Y;
selection = new Rectangle(x, y, width, height);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在目前存在局限性.显而易见的是,这不会在嵌套容器控件中选择TextBox(例如,包含TextBox的表单上的面板).如果是这种情况,将在面板下面绘制选择,并且不会选择TextBox,因为我编写的代码不检查嵌套容器.
您可以轻松更新代码以执行所有这些操作,但这应该会给您一个坚实的开始.