3

I have two labels on a form. I want to be able to drag one label over the other and while the mouse left button is still down I want to be able to press space key to toggle target label's text between "foo" and "bar".

Seems like all input events are suppressed while mouse left button is not released.

Am I missing something? Any samples?

Otiel
  • 18,404
  • 16
  • 78
  • 126
Ramunas
  • 3,853
  • 1
  • 30
  • 31

3 Answers3

2

Check out the GiveFeedback event. Maybe you can check from there if a key is being pressed.

EDIT:

void panel1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
    if (Keyboard.IsKeyDown(Key.Space))
    {
        if (label1.Text == "foo") label1.Text = "bar"; else label1.Text = "foo";
    }
}

and add a reference to PresentaionCore and: WindowBase (You'll find that in: C:\Program Files (x86)\ReferenceAssemblies\Microsoft\Framework\v3.0\ .)

You'll have to play with this a little.

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • This one is close. In conjunction with QueryContinueDrag it could do the trick but I still do not get space key state – Ramunas Dec 06 '11 at 20:09
  • @Ramunas Maybe(!) these might help: http://stackoverflow.com/questions/1100285/how-to-detect-the-currently-pressed-key and: http://stackoverflow.com/questions/1752494/detect-if-any-key-is-pressed-in-c-sharp-not-a-b-but-any – ispiro Dec 06 '11 at 20:48
0

If dragged element never leaves original form consider interpreting mouse events instead of using D&D mechanism. It won't be as good, but it will let you interpret other messages during dragging.

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";
        }
    }
}
MagnatLU
  • 5,967
  • 1
  • 22
  • 17
-1

//try something like this and change things where needed to fit your working example

  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);
      }
  }
MethodMan
  • 18,625
  • 6
  • 34
  • 52