怎么获取winform中动态代码生成的控件的状态
winform怎么获取动态代码生成窗口里面的控件的属性状态
MainForm中调用
private void ShowPropertyForm()
{
PropertyForm form = new PropertyForm(selectedShape);
form.ShowDialog();
pictureBox1.Refresh();
}
MainForm中生成
public class PropertyForm : Form{private ShapeType selectedShape;private Shape shape;private DashStyle StrokeStyle; //线型private float StrokeWidth = 1.0f; //线宽private Color StrokeColor; //边框颜色 private RadioButton IsFilledRadioButton; //是否填充private RadioButton NoFilledRadioButton;private ColorDialog FilledColorColorDialog; //填充颜色private Button renewButton;private Button cancelButton;private Color CurrentStrokeColor;public PropertyForm(Shape shape){this.shape = shape;InitializeComponents();}public void InitializeComponents(){this.Text = "图形属性";this.FormBorderStyle = FormBorderStyle.FixedDialog;//属性框边框样式this.MaximizeBox = false;//右上角是否有最大化按钮this.MinimizeBox = false;//右上角是否有最小化按钮this.StartPosition = FormStartPosition.CenterParent;this.ClientSize = new Size(200, 350);// 添加类型Labelthis.Controls.Add(new Label(){Text = "类型",Location = new Point(25, 28),Size = new Size(30, 20),});//添加下拉框this.Controls.Add(new ComboBox(){Location = new Point(60, 25),Size = new Size(115, 20),Text = shape.Type.ToString()});//添加线型Labelthis.Controls.Add(new Label(){Text = "线型",Location = new Point(25, 78),Size = new Size(30, 20)});//添加下拉框ComboBox comboBox1 = new ComboBox();comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;comboBox1.Items.Add(DashStyle.Solid);comboBox1.Items.Add(DashStyle.Dash);comboBox1.Items.Add(DashStyle.DashDot);comboBox1.Items.Add(DashStyle.DashDotDot);comboBox1.SelectedIndex = (int)selectedShape;comboBox1.Location = new Point(60, 75);comboBox1.Size = new Size(115, 20);this.Controls.Add(comboBox1);//添加线宽Labelthis.Controls.Add(new Label(){Text = "线宽",Location = new Point(25, 128),Size = new Size(30, 20)});//添加下拉框ComboBox comboBox2 = new ComboBox();comboBox2.Text = shape.StrokeWidth.ToString();comboBox2.Location = new Point(60, 125);comboBox2.Size = new Size(115, 20);comboBox2.Items.Add("0.5");comboBox2.Items.Add("1");comboBox2.Items.Add("2");comboBox2.Items.Add("4");comboBox2.Items.Add("8");comboBox2.Items.Add("16");comboBox2.Items.Add("32");this.Controls.Add(comboBox2);//添加颜色Labelthis.Controls.Add(new Label(){Text = "颜色",Location = new Point(25, 178),Size = new Size(30, 20)});//添加颜色对话框Button StrokeColorbutton = new Button();StrokeColorbutton.Location = new Point(60, 175);StrokeColorbutton.Size = new Size(115, 20);this.Controls.Add(StrokeColorbutton);//添加填充labelthis.Controls.Add(new Label(){Text = "填充",Location = new Point(25, 240),Size = new Size(30, 20)});RadioButton radioButton2 = new RadioButton();radioButton2.Text = "否";radioButton2.Location = new Point(100, 238);this.Controls.Add(radioButton2);//添加RadioButtonRadioButton radioButton1 = new RadioButton();radioButton1.Text = "是";radioButton1.Location = new Point(60, 238);this.Controls.Add(radioButton1);//添加颜色Labelthis.Controls.Add(new Label(){Text = "颜色",Location = new Point(25, 280),Size = new Size(30, 20)});//添加颜色对话框Button FillColorbutton = new Button();FillColorbutton.Location = new Point(60, 275);FillColorbutton.Size = new Size(115, 20);this.Controls.Add(FillColorbutton);//添加图形GroupBoxthis.Controls.Add(new GroupBox(){Text = "图形",Location = new Point(0, 5),Size = new Size(200, 50),});//添加类型GroupBoxthis.Controls.Add(new GroupBox(){Text = "边框",Location = new Point(0, 55),Size = new Size(200, 150)});//添加内部GroupBoxthis.Controls.Add(new GroupBox(){Text = "内部",Location = new Point(0, 205),Size = new Size(200, 100)});//添加更新ButtonrenewButton = new Button();renewButton.Location = new Point(20, 315);renewButton.Size = new Size(70, 30);renewButton.Text = "更新";renewButton.DialogResult = DialogResult.OK;this.Controls.Add(renewButton);//添加取消ButtoncancelButton = new Button();cancelButton.Location = new Point(110, 315);cancelButton.Size = new Size(70, 30);cancelButton.Text = "取消";cancelButton.DialogResult = DialogResult.Cancel;this.Controls.Add(cancelButton);renewButton.Click += renewButton_Click;comboBox2.SelectedValueChanged += ComboBox2_SelectedValueChanged;}public static float nStrokeWidth;private void renewButton_Click(object sender, EventArgs e){shape.StrokeWidth = 这里取控件中的状态属性 ;MessageBox.Show("更新成功");}}
遇到的困难
在renewButton_Click方法中,无法调用生成控件的状态,只有在InitializeComponents方法中可以使用
解决思路
1,首先想到的是在InitializeComponents中把控件的属性赋值出去给变量,然后在其他地方调用
InitializeComponents只初始化刚刚生成的时候,后面调用的变量为空值不行
2.renewButton_Click方法放入InitializeComponents中,private删除,即可调用,不影响,成功解决
解决办法
public class PropertyForm : Form{private ShapeType selectedShape;private Shape shape;private DashStyle StrokeStyle; //线型private float StrokeWidth = 1.0f; //线宽private Color StrokeColor; //边框颜色 private RadioButton IsFilledRadioButton; //是否填充private RadioButton NoFilledRadioButton;private ColorDialog FilledColorColorDialog; //填充颜色private Button renewButton;private Button cancelButton;private Color CurrentStrokeColor;public PropertyForm(Shape shape){this.shape = shape;InitializeComponents();}public void InitializeComponents(){this.Text = "图形属性";this.FormBorderStyle = FormBorderStyle.FixedDialog;//属性框边框样式this.MaximizeBox = false;//右上角是否有最大化按钮this.MinimizeBox = false;//右上角是否有最小化按钮this.StartPosition = FormStartPosition.CenterParent;this.ClientSize = new Size(200, 350);// 添加类型Labelthis.Controls.Add(new Label(){Text = "类型",Location = new Point(25, 28),Size = new Size(30, 20),});//添加下拉框this.Controls.Add(new ComboBox(){Location = new Point(60, 25),Size = new Size(115, 20),Text = shape.Type.ToString()});//添加线型Labelthis.Controls.Add(new Label(){Text = "线型",Location = new Point(25, 78),Size = new Size(30, 20)});//添加下拉框ComboBox comboBox1 = new ComboBox();comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;comboBox1.Items.Add(DashStyle.Solid);comboBox1.Items.Add(DashStyle.Dash);comboBox1.Items.Add(DashStyle.DashDot);comboBox1.Items.Add(DashStyle.DashDotDot);comboBox1.SelectedIndex = (int)selectedShape;comboBox1.Location = new Point(60, 75);comboBox1.Size = new Size(115, 20);this.Controls.Add(comboBox1);//添加线宽Labelthis.Controls.Add(new Label(){Text = "线宽",Location = new Point(25, 128),Size = new Size(30, 20)});//添加下拉框ComboBox comboBox2 = new ComboBox();comboBox2.Text = shape.StrokeWidth.ToString();comboBox2.Location = new Point(60, 125);comboBox2.Size = new Size(115, 20);comboBox2.Items.Add("0.5");comboBox2.Items.Add("1");comboBox2.Items.Add("2");comboBox2.Items.Add("4");comboBox2.Items.Add("8");comboBox2.Items.Add("16");comboBox2.Items.Add("32");this.Controls.Add(comboBox2);//添加颜色Labelthis.Controls.Add(new Label(){Text = "颜色",Location = new Point(25, 178),Size = new Size(30, 20)});//添加颜色对话框Button StrokeColorbutton = new Button();StrokeColorbutton.Location = new Point(60, 175);StrokeColorbutton.Size = new Size(115, 20);this.Controls.Add(StrokeColorbutton);//添加填充labelthis.Controls.Add(new Label(){Text = "填充",Location = new Point(25, 240),Size = new Size(30, 20)});RadioButton radioButton2 = new RadioButton();radioButton2.Text = "否";radioButton2.Location = new Point(100, 238);this.Controls.Add(radioButton2);//添加RadioButtonRadioButton radioButton1 = new RadioButton();radioButton1.Text = "是";radioButton1.Location = new Point(60, 238);this.Controls.Add(radioButton1);//添加颜色Labelthis.Controls.Add(new Label(){Text = "颜色",Location = new Point(25, 280),Size = new Size(30, 20)});//添加颜色对话框Button FillColorbutton = new Button();FillColorbutton.Location = new Point(60, 275);FillColorbutton.Size = new Size(115, 20);this.Controls.Add(FillColorbutton);//添加图形GroupBoxthis.Controls.Add(new GroupBox(){Text = "图形",Location = new Point(0, 5),Size = new Size(200, 50),});//添加类型GroupBoxthis.Controls.Add(new GroupBox(){Text = "边框",Location = new Point(0, 55),Size = new Size(200, 150)});//添加内部GroupBoxthis.Controls.Add(new GroupBox(){Text = "内部",Location = new Point(0, 205),Size = new Size(200, 100)});//添加更新ButtonrenewButton = new Button();renewButton.Location = new Point(20, 315);renewButton.Size = new Size(70, 30);renewButton.Text = "更新";renewButton.DialogResult = DialogResult.OK;this.Controls.Add(renewButton);//添加取消ButtoncancelButton = new Button();cancelButton.Location = new Point(110, 315);cancelButton.Size = new Size(70, 30);cancelButton.Text = "取消";cancelButton.DialogResult = DialogResult.Cancel;this.Controls.Add(cancelButton);renewButton.Click += renewButton_Click;void renewButton_Click(object sender, EventArgs e){shape.StrokeWidth =(float)Convert.ToInt32( comboBox2.SelectedItem);MessageBox.Show("更新成功");}}public static float nStrokeWidth;}