定义一个类,
namespace HB_ClassLibrary
{
public class Class_ListBoxProduct
{
public System.Windows.Forms.ListBox ListBoxProduct1;
public void ListBoxShow(int G)
{
this.ListBoxProduct1 = new System.Windows.Forms.ListBox();
this.ListBoxProduct1.Height = G - 100;
}
}
}然后用一下:
namespace HB_WindowsFormsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Shown(object sender, EventArgs e)
{
HB_ClassLibrary.Class_ListBoxProduct Class_ListBoxProduct1 = new HB_ClassLibrary.Class_ListBoxProduct();
Class_ListBoxProduct1.ListBoxShow(this.Size.Height);
this.Controls.Add(Class_ListBoxProduct1.ListBoxProduct1);
}
}
}我想问的是在ListBoxShow方法中ListBoxProduct1.Height的值其实是固定的,就是被使用的窗体高度this.Size.Height - 30;所以我能不能不声明(int G),而直接在Class_ListBoxProduct中就将赋值做好?
namespace HB_ClassLibrary
{
public class Class_ListBoxProduct
{
public System.Windows.Forms.ListBox ListBoxProduct1;
public void ListBoxShow(int G)
{
this.ListBoxProduct1 = new System.Windows.Forms.ListBox();
this.ListBoxProduct1.Height = G - 100;
}
}
}然后用一下:
namespace HB_WindowsFormsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Shown(object sender, EventArgs e)
{
HB_ClassLibrary.Class_ListBoxProduct Class_ListBoxProduct1 = new HB_ClassLibrary.Class_ListBoxProduct();
Class_ListBoxProduct1.ListBoxShow(this.Size.Height);
this.Controls.Add(Class_ListBoxProduct1.ListBoxProduct1);
}
}
}我想问的是在ListBoxShow方法中ListBoxProduct1.Height的值其实是固定的,就是被使用的窗体高度this.Size.Height - 30;所以我能不能不声明(int G),而直接在Class_ListBoxProduct中就将赋值做好?
public class MyListBox : System.Windows.Forms.ListBox
{
protected override void OnParentChanged(EventArgs e)
{
base.OnParentChanged(e);
if (Parent != null)
{
this.Height = Parent.Height - 100;
}
}
}调用:
private void Form1_Shown(object sender, EventArgs e)
{
this.Controls.Add(new MyListBox());
}