using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace MoveControlLocation
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// comboBox1
// 
this.comboBox1.Location = new System.Drawing.Point(32, 32);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = "comboBox1";
this.comboBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.comboBox1_MouseMove);
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(200, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
this.comboBox1.Location=new Point(2,76);
} private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.comboBox1.Location=new Point(e.X,e.Y);
} private void comboBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.comboBox1.Location=new Point(e.X+this.comboBox1.Left,e.Y+this.comboBox1.Top);
}
}
}

解决方案 »

  1.   

    这样大概是没错了,但是如何让它在mouse按下后拖动,txtbox控件移动,当mouse抬起后让txtbox 不移动?
      

  2.   

    在鼠标事件中修改position属性就可以了
      

  3.   

    http://expert.csdn.net/Expert/topic/2969/2969094.xml?temp=.2266504
    我给别人写的代码;和你的问题差不多; 把那个画的长方形改成你要拖动的控件就成!!
    要自己多实践;using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication37
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
    private Point location;
    private Point offset;
    private bool flag; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(136, 32);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(56, 24);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
    this.Load += new System.EventHandler(this.Form1_Load);
    this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    int iLeft,iTop;

    iLeft = 0;
    iTop =  0; Graphics dc = this.CreateGraphics();


    //Pen RedPen = new Pen(Color.Red,1);
    dc.FillRectangle(System.Drawing.Brushes.Red ,iLeft,iTop,100,50);

    location.X =iLeft;
    location.Y =iTop;
    }
    private void Form1_MouseDown(object sender, System.Windows .Forms .MouseEventArgs e)
    { if ((location.X <  e.X) && (e.X  < (location.X +100)))
    {
    if (((location.Y+50) > e.Y)  && (e.Y  > location.Y))
    {
    flag=true;

    offset.X =e.X ;
    offset.Y =e.Y ;
    }
    }
    else
    {
    flag=false;
    }
    }
    private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if (flag==true)
    {
    offset.X =e.X-offset.X  ;
    offset.Y =e.Y- offset.Y ;
    Graphics dc =this.CreateGraphics();
        location.X =location.X +offset.X ;
    location.Y =location.Y +offset.Y ;
    //MessageBox.Show (location.ToString ());

    //this.Invalidate ();
    dc.FillRectangle(System.Drawing.Brushes.Red ,location.X  ,location.Y ,100,50 );
      

    } } private void Form1_Load(object sender, System.EventArgs e)
    {
    location=new Point();
    offset=new Point ();
    }
    }
    }