开发者

in winforms, can i drag a control with a bunch of controls (like listboxes) in it into another control and resize / move them from there?

I'm running into some situations whe开发者_StackOverflow社区re it has been nice to make a control (Control A) with a bunch of things in it like buttons, listboxes, datagridviews etc so I can drag it into other controls (Control B) whenever I like, so I don't have to write the logic for the stuff within Control A over and over again.

The only annoying thing with this approach has been that I can't seem to find a way in the designer to move/resize the buttons, listboxes, datagridviews etc of Control A around from the designer of Control B. All it seems to let me do is resize the entirety of Control A.

Does anyone know how to make these custom controls containing multiple controls in such a way that they support design time resizing/moving?

Thanks

Isaac


There is no built in way to do this. You would essentially have to implement event handlers in order to handle the resizing of individual components within your control. One alternative for you is to expose the Size and Location properties of each individual component to the control's clients. For example, within the Control class you could do something like this:

public Size Component1Size
{
     get { return component1.Size; }
     set { component1.Size = value; }
}

public Point Component1Location
{
    get { return component1.Location; }
    set { component1.Location = value; }
}

and do this for each component of your control. I think this would be your best option, even though the user won't be able to physically click/drag the components to move and resize them.


Yes you can my friend ,you need to create a SizeAble and DragAndDrop pannel where you can Insert Controls and by Moving That Pannel you can reach that .And for the Resizing Issue you can play with Anchor of Control's that you already added .

using System;
using System.Drawing;
using System.Windows.Forms;

public class SizeablePanel : Panel {
  private const int cGripSize = 20;
  private bool mDragging;
  private Point mDragPos;

  public SizeablePanel() {
    this.DoubleBuffered = true;
    this.SetStyle(ControlStyles.ResizeRedraw, true);
    this.BackColor = Color.White;
  }

  protected override void OnPaint(PaintEventArgs e) {
    ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor,
      new Rectangle(this.ClientSize.Width - cGripSize, this.ClientSize.Height - cGripSize, cGripSize, cGripSize));
    base.OnPaint(e);
  }

  private bool IsOnGrip(Point pos) {
    return pos.X >= this.ClientSize.Width - cGripSize &&
           pos.Y >= this.ClientSize.Height - cGripSize;
  }

  protected override void OnMouseDown(MouseEventArgs e) {
    mDragging = IsOnGrip(e.Location);
    mDragPos = e.Location;
    base.OnMouseDown(e);
  }

  protected override void OnMouseUp(MouseEventArgs e) {
    mDragging = false;
    base.OnMouseUp(e);
  }

  protected override void OnMouseMove(MouseEventArgs e) {
    if (mDragging) {
      this.Size = new Size(this.Width + e.X - mDragPos.X,
        this.Height + e.Y - mDragPos.Y);
      mDragPos = e.Location;
    }
    else if (IsOnGrip(e.Location)) this.Cursor = Cursors.SizeNWSE;
    else this.Cursor = Cursors.Default;
    base.OnMouseMove(e);
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜