
Untitled
By: a guest on
May 17th, 2012 | syntax:
None | size: 1.84 KB | hits: 11 | expires: Never
Keyboard events while dragging
void panel1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
if (Keyboard.IsKeyDown(Key.Space))
{
if (label1.Text == "foo") label1.Text = "bar"; else label1.Text = "foo";
}
}
public class MyForm : Form
{
private Label label;
public MyForm()
{
KeyPress += new KeyPressEventHandler(Form_KeyPress);
label = new Label();
label.Text = "foo";
label.MouseMove += new MouseEventHandler(label_MouseMove);
Controls.Add(label);
}
private void label_MouseMove(object sender, MouseEventArgs e)
{
if (MouseButtons == MouseButtons.Left)
{
Point loc = label.Location;
loc.Offset(e.X, e.Y);
label.Location = loc;
}
}
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == ' ')
{
if (label.Text == "foo")
label.Text = "bar";
else
label.Text = "foo";
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.MouseDown += new MouseEventHandler(label1_MouseDown);
textBox1.AllowDrop = true;
textBox1.DragEnter += new DragEventHandler(textBox1_DragEnter);
textBox1.DragDrop += new DragEventHandler(textBox1_DragDrop);
}
void label1_MouseDown(object sender, MouseEventArgs e)
{
DoDragDrop(label1.Text, DragDropEffects.Copy);
}
void textBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
}
void textBox1_DragDrop(object sender, DragEventArgs e)
{
textBox1.Text = (string)e.Data.GetData(DataFormats.Text);
}
}