c# – 动态更改面板的大小

前端之家收集整理的这篇文章主要介绍了c# – 动态更改面板的大小前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在实现一个需要在面板中拖放图像框的应用程序.图像框是从程序中动态添加的,因此我在面板中将autoscroll属性设置为true.但是当我拖出底部的框时面板尺寸减小.我在面板中放置了autosize属性false.面板停靠在另一个面板上.我想在运行时设置面板的大小.我怎样才能实现这一点.
  1. public form1(int[,] dummy,int columnSize,int rowSize)
  2. {
  3. this.dummy= dummy;
  4. numOfColumns = columnSize;
  5. numOfRows = rowSize;
  6. getData();
  7. addIds = addIdArray;
  8. data = mylist;
  9. InitializeComponent();
  10. //panel1.MinimumSize = new Size(columnSize * 40,rowSize * 40);
  11. //panel1.Height = rowSize * 40;
  12. //panel1.Width = columnSize * 40;
  13. //panel4.Height = rowSize * 40;
  14. //panel4.Width = columnSize * 40;
  15. int x,y;
  16. Structures.EmptyRectSpace space = new Structures.EmptyRectSpace();
  17. for (int i = 0; i < data.Count; i++)// set picture Boxes
  18. {
  19. space = (Structures.EmptyRectSpace)data[i];
  20. x = space.startingJ;
  21. y = space.startingI;
  22. int h,w;
  23. h = space.length;
  24. w = space.width;
  25.  
  26. p = new PictureBox();
  27. p.Width = w * 40;
  28. p.Height = h * 40;
  29. p.BackColor = Color.DarkGreen;
  30. p.Image = Properties.Resources.v;
  31. p.BorderStyle = BorderStyle.FixedSingle;
  32. p.Name = addIdArray[i].ToString();
  33. p.Location = new Point((x + 1 - w) * 40,(y + 1 - h) * 40);
  34.  
  35. this.panel1.Controls.Add(p);
  36. }
  37.  
  38. foreach (Control c in this.panel1.Controls)
  39. {
  40. if (c is PictureBox)
  41. {
  42. c.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
  43. }
  44. }
  45. this.panel1.DragOver += new System.Windows.Forms.DragEventHandler(this.panel1_DragOver);
  46. panel1.DragOver += new DragEventHandler(panel1_DragOver);
  47. panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
  48. panel1.AllowDrop = true;
  49. panel2.AllowDrop = true;
  50. foreach (Control c in this.panel2.Controls)
  51. {
  52. c.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
  53. }
  54. this.panel2.DragOver += new System.Windows.Forms.DragEventHandler(this.panel2_DragOver);
  55. panel2.DragOver += new DragEventHandler(panel2_DragOver);
  56. panel2.DragDrop += new DragEventHandler(panel2_DragDrop);
  57. }

这是包含面板的表单的构造函数.当它加载时,必须将图片添加到面板中并实现面板的拖放事件.

请帮帮我..

解决方法

除非我在代码中以编程方式设置最大宽度,否则我无法工作.设计师的最大宽度是多少并无关紧要(或者如果设置了最大宽度).
  1. int newWidth = 200;
  2. panel.MaximumSize = new Size(newWidth,panel.Height);
  3. panel.Size = new Size(newWidth,panel.Height);

猜你在找的C#相关文章