我正在实现一个需要在面板中拖放图像框的应用程序.图像框是从程序中动态添加的,因此我在面板中将autoscroll属性设置为true.但是当我拖出底部的框时面板尺寸减小.我在面板中放置了autosize属性false.面板停靠在另一个面板上.我想在运行时设置面板的大小.我怎样才能实现这一点.
public form1(int[,] dummy,int columnSize,int rowSize) { this.dummy= dummy; numOfColumns = columnSize; numOfRows = rowSize; getData(); addIds = addIdArray; data = mylist; InitializeComponent(); //panel1.MinimumSize = new Size(columnSize * 40,rowSize * 40); //panel1.Height = rowSize * 40; //panel1.Width = columnSize * 40; //panel4.Height = rowSize * 40; //panel4.Width = columnSize * 40; int x,y; Structures.EmptyRectSpace space = new Structures.EmptyRectSpace(); for (int i = 0; i < data.Count; i++)// set picture Boxes { space = (Structures.EmptyRectSpace)data[i]; x = space.startingJ; y = space.startingI; int h,w; h = space.length; w = space.width; p = new PictureBox(); p.Width = w * 40; p.Height = h * 40; p.BackColor = Color.DarkGreen; p.Image = Properties.Resources.v; p.BorderStyle = BorderStyle.FixedSingle; p.Name = addIdArray[i].ToString(); p.Location = new Point((x + 1 - w) * 40,(y + 1 - h) * 40); this.panel1.Controls.Add(p); } foreach (Control c in this.panel1.Controls) { if (c is PictureBox) { c.MouseDown += new MouseEventHandler(pictureBox1_MouseDown); } } this.panel1.DragOver += new System.Windows.Forms.DragEventHandler(this.panel1_DragOver); panel1.DragOver += new DragEventHandler(panel1_DragOver); panel1.DragDrop += new DragEventHandler(panel1_DragDrop); panel1.AllowDrop = true; panel2.AllowDrop = true; foreach (Control c in this.panel2.Controls) { c.MouseDown += new MouseEventHandler(pictureBox1_MouseDown); } this.panel2.DragOver += new System.Windows.Forms.DragEventHandler(this.panel2_DragOver); panel2.DragOver += new DragEventHandler(panel2_DragOver); panel2.DragDrop += new DragEventHandler(panel2_DragDrop); }
这是包含面板的表单的构造函数.当它加载时,必须将图片框添加到面板中并实现面板的拖放事件.
请帮帮我..
解决方法
除非我在代码中以编程方式设置最大宽度,否则我无法工作.设计师的最大宽度是多少并无关紧要(或者如果设置了最大宽度).
int newWidth = 200; panel.MaximumSize = new Size(newWidth,panel.Height); panel.Size = new Size(newWidth,panel.Height);