Frage

I used following code to drag and drop Button in C# and it works like charm when my Form.RightToLeftLayout=False, but when I set RightToLeftLayout=True it doesnt work and move the control in wrong direction!!!

  public partial class Form1 : Form
    {
        int xPosition;
        int yPosition;
        bool isDraged;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnMoveable_MouseDown(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.SizeAll;
            xPosition = e.X;
            yPosition = e.Y;
            isDraged = true;
        }

        private void btnMoveable_MouseUp(object sender, MouseEventArgs e)
        {
            isDraged = false;
            this.Cursor = Cursors.Default;

        }


        private void btnMoveable_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDraged)
            {

                btnMoveable.Left = btnMoveable.Left + e.X - xPosition;
                btnMoveable.Top = btnMoveable.Top + e.Y - yPosition;

            }
        }
}
War es hilfreich?

Lösung

Well, you're discovering how RightToLeft is implemented. Everything is still in their normal logical position but the coordinate system is mirror-imaged along the Y-axis. So movement along the X-axis is inverted. You'll need to accommodate that. Fix:

    int dx = e.X - xPosition;
    if (this.RightToLeft == RightToLeft.Yes) dx = -dx;
    btnMoveable.Left = btnMoveable.Left + dx;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top