Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 17th, 2012  |  syntax: None  |  size: 1.84 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Keyboard events while dragging
  2. void panel1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
  3. {
  4.     if (Keyboard.IsKeyDown(Key.Space))
  5.     {
  6.         if (label1.Text == "foo") label1.Text = "bar"; else label1.Text = "foo";
  7.     }
  8. }
  9.        
  10. public class MyForm : Form
  11. {
  12.     private Label label;
  13.  
  14.     public MyForm()
  15.     {
  16.         KeyPress += new KeyPressEventHandler(Form_KeyPress);
  17.         label = new Label();
  18.         label.Text = "foo";
  19.         label.MouseMove += new MouseEventHandler(label_MouseMove);
  20.         Controls.Add(label);
  21.     }
  22.  
  23.     private void label_MouseMove(object sender, MouseEventArgs e)
  24.     {
  25.         if (MouseButtons == MouseButtons.Left)
  26.         {
  27.             Point loc = label.Location;
  28.             loc.Offset(e.X, e.Y);
  29.             label.Location = loc;
  30.         }
  31.     }
  32.  
  33.     private void Form_KeyPress(object sender, KeyPressEventArgs e)
  34.     {
  35.         if (e.KeyChar == ' ')
  36.         {
  37.             if (label.Text == "foo")
  38.                 label.Text = "bar";
  39.             else
  40.                 label.Text = "foo";
  41.         }
  42.     }
  43. }
  44.        
  45. public partial class Form1 : Form
  46.   {
  47.       public Form1()
  48.       {
  49.         InitializeComponent();
  50.         label1.MouseDown += new MouseEventHandler(label1_MouseDown);        
  51.         textBox1.AllowDrop = true;
  52.         textBox1.DragEnter += new DragEventHandler(textBox1_DragEnter);
  53.         textBox1.DragDrop += new DragEventHandler(textBox1_DragDrop);
  54.       }
  55.  
  56.       void label1_MouseDown(object sender, MouseEventArgs e)
  57.       {
  58.         DoDragDrop(label1.Text, DragDropEffects.Copy);
  59.       }
  60.  
  61.       void textBox1_DragEnter(object sender, DragEventArgs e)
  62.       {
  63.         if (e.Data.GetDataPresent(DataFormats.Text))
  64.          e.Effect = DragDropEffects.Copy;
  65.       }
  66.  
  67.       void textBox1_DragDrop(object sender, DragEventArgs e)
  68.       {
  69.         textBox1.Text = (string)e.Data.GetData(DataFormats.Text);
  70.       }
  71.   }