当前位置: 首页 > news >正文

怎么获取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;}

http://www.lryc.cn/news/67279.html

相关文章:

  • Windows安装Maven并配置环境
  • 致力于中小企业JavaEE企业级快速开发平台、后台框架平台
  • 【神经网络】tensorflow实验9--分类问题
  • LeetCode2. 两数相加
  • 基于无线传感网络(WSN)的目标跟踪技术(Matlab代码实现)
  • 百度发布首个可信AI工具集TrustAI,助力数据分析与增强
  • 电力系统负荷与电价预测优化模型(Matlab代码实现)
  • asp.net+C#超市商品进销存管理系统
  • 轻量级K8s发行版的五大优势,助力企业快速拥抱边缘计算
  • 【深入理解redis】数据结构
  • 《计算机网络—自顶向下方法》 第三章Wireshark实验:DNS协议分析
  • JUC(十二)-线程中断相关问题(LockSupport,sleep,InterruptException)
  • Kotlin高级协程
  • 车载软件架构——闲聊几句AUTOSAR BSW(四)
  • Linux:rpm查询安装 yum安装
  • Android音视频开发之音频录制和播放
  • Java之单例模式
  • 【分组码系列】线性分组码的网格图和维特比译码
  • 代码命名规范是真优雅呀!代码如诗
  • 你不知道的自动化?使用自动化测试在项目中创造高业务价值...
  • 通过实现一个简单的 JavaScript 猜数字大小的游戏,介绍如何进行布局样式处理
  • Java设计模式(二十二)策略模式
  • 【沐风老师】一步一步教你在3dMax中进行UVW贴图和展开UVW的方法
  • Redis主从复制(搭建集群的一种方式)【故障转移,内存,回收】
  • 专业专注,极致体验,高端隐形智能晾衣机品牌邦先生官宣浙江卫视知名主持人沈涛为品牌代言人
  • SpringCloud使用SkyWalking实现分布式链路追踪1
  • 【牛客刷题专栏】0x28:JZ30 包含min函数的栈(C语言编程题)
  • 聚焦丨酷雷曼荣列XRMA联盟成员单位
  • 物联网架构和技术:如何实现物物互联和智能化控制
  • Linux系统查看CPU信息命令cat /proc/cpuinfo详细说明