先说下使用场景,我这里是要求有个弹出框,然后弹出框里的值是根据一个字段的值动态添加的,然后每一个值改变的时候都要修改 `规格型号`,然后点击确认按钮后,将 `规格型号` 里的值传到父级窗口 大致就是这样的一个使用场景
## 整个代码如下
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
string strGModel = "";
public Form1()
{
InitializeComponent();
// 防止闪屏
this.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(this, true, null);
}
private void Form1_Load(object sender, EventArgs e)
{
addComponent();
}
private void addComponent()
{
string str = "品牌类型,出口享惠情况,张三,李四,王五,马六,小明,小白,其他" + ",规格型号";//动态值,可以从任何地方传过来
string[] strs = str.Split(',');
int count = strs.Length;
TableLayoutPanel tableLayoutPanel1 = new TableLayoutPanel();
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
tableLayoutPanel1.ColumnCount = 2;
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 70F));
tableLayoutPanel1.Dock = DockStyle.Top;
tableLayoutPanel1.Location = new Point(0, 60);
tableLayoutPanel1.Name = "tableLayoutPanel1";
tableLayoutPanel1.RowCount = count; // 动态
for (int i = 0; i < count; i++)
{
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
}
tableLayoutPanel1.TabIndex = 100;
tableLayoutPanel1.Size = new Size(800, 30 * (count - 1));//动态
for (int j = 0; j < count; j++)
{
Label label = new Label();
label.Anchor = (((AnchorStyles.Left | AnchorStyles.Right)));
label.AutoSize = true;
label.Name = "label";
label.TabIndex = 2 + j * 2;
label.Text = strs[j];
label.Parent = tableLayoutPanel1;
tableLayoutPanel1.Controls.Add(label, 0, j);
if (strs[j] == "品牌类型")
{
ComboBox cmb = new ComboBox();
cmb.Anchor = (((AnchorStyles.Left | AnchorStyles.Right)));
cmb.FormattingEnabled = true;
cmb.Items.AddRange(new object[] {
"0-无品牌",
"1-境内自主品牌",
"2-境内收购品牌",
"3-境外品牌(贴牌生产)",
"4-境外品牌(其它)"});
cmb.AutoSize = true;
cmb.Name = "cmb" + j;
cmb.TabIndex = 3 + j * 2;
cmb.Parent = tableLayoutPanel1;
if (strs[j] != "GTIN" && strs[j] != "CAS" && strs[j] != "其他" && strs[j] != "规格型号")
{
cmb.BackColor = cmb.BackColor = Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
}
cmb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cmb.AutoCompleteSource = AutoCompleteSource.ListItems;
cmb.SelectedValueChanged += new EventHandler(Cmb_SelectedValueChanged);
cmb.Parent = tableLayoutPanel1;
tableLayoutPanel1.Controls.Add(cmb, 1, j);
}
else if (strs[j] == "出口享惠情况")
{
ComboBox cmb = new ComboBox();
cmb.Anchor = (((AnchorStyles.Left | AnchorStyles.Right)));
cmb.FormattingEnabled = true;
cmb.Items.AddRange(new object[] {
"0-出口货物在最终目的国(地区)不享受优惠关税",
"1-出口货物在最终目的国(地区)享受优惠关税",
"2-不能确定在最终目的国(地区)享受优惠关税",
"3-不适用于进口报关单"});
cmb.AutoSize = true;
cmb.Name = "cmb_" + j;
cmb.TabIndex = 3 + j * 2;
cmb.Parent = tableLayoutPanel1;
if (strs[j] != "GTIN" && strs[j] != "CAS" && strs[j] != "其他" && strs[j] != "规格型号")
{
cmb.BackColor = cmb.BackColor = Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
}
cmb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cmb.AutoCompleteSource = AutoCompleteSource.ListItems;
cmb.SelectedValueChanged += new EventHandler(Cmb_SelectedValueChanged);
cmb.Parent = tableLayoutPanel1;
tableLayoutPanel1.Controls.Add(cmb, 1, j);
}
else
{
TextBox textBox = new TextBox();
textBox.Anchor = (((AnchorStyles.Left | AnchorStyles.Right)));
textBox.AutoSize = true;
textBox.Name = "textBox" + j;
textBox.TabIndex = 3 + j * 2;
if (strs[j] != "GTIN" && strs[j] != "CAS" && strs[j] != "其他" && strs[j] != "规格型号")
{
textBox.BackColor = Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
}
if (strs[j] == "规格型号") {
textBox.Name = "textBox_gModel";
}
if (strs[j] != "规格型号")
{
textBox.TextChanged += new EventHandler(TextBox_TextChanged);
}
textBox.Parent = tableLayoutPanel1;
tableLayoutPanel1.Controls.Add(textBox, 1, j);
}
}
Controls.Add(tableLayoutPanel1);
Label labe10 = new Label();
labe10.Anchor = (((AnchorStyles.Left | AnchorStyles.Right)));
labe10.AutoSize = true;
labe10.Name = "label";
labe10.TabIndex = 0;
labe10.Font = new Font("宋体", 9F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(134)));
labe10.Dock = DockStyle.Top;
labe10.Text = "规格型号(根据海关规定,以下要素应全部填报)。";
Controls.Add(labe10);
TableLayoutPanel tableLayoutPanel2 = new TableLayoutPanel();
tableLayoutPanel2.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
tableLayoutPanel2.ColumnCount = 2;
tableLayoutPanel2.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
tableLayoutPanel2.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 70F));
tableLayoutPanel2.Dock = DockStyle.Top;
tableLayoutPanel2.Location = new Point(0, 0);
tableLayoutPanel2.Name = "tableLayoutPanel2";
tableLayoutPanel2.RowCount = count; // 动态
for (int i = 0; i < 2; i++)
{
tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
}
tableLayoutPanel2.TabIndex = 100;
tableLayoutPanel2.Size = new Size(800, 60);//动态
for (int k = 0; k < 2; k++)
{
Label label = new Label();
label.Anchor = (((AnchorStyles.Left | AnchorStyles.Right)));
label.AutoSize = true;
label.Name = "label";
label.TabIndex = 0;
label.Text = k == 0 ? "返填规则" : "商品信息";
label.Parent = tableLayoutPanel2;
tableLayoutPanel2.Controls.Add(label, 0, k);
if (k == 1)
{
TextBox textBox = new TextBox();
textBox.Anchor = (((AnchorStyles.Left | AnchorStyles.Right)));
textBox.AutoSize = true;
textBox.Name = "textBox100" + k;
textBox.TabIndex = 0;
textBox.BackColor = SystemColors.ScrollBar;
textBox.Enabled = false;
textBox.Parent = tableLayoutPanel2;
tableLayoutPanel2.Controls.Add(textBox, 1, k);
}
else
{
Panel panel = new Panel();
panel.Anchor = (((AnchorStyles.Left | AnchorStyles.Right)));
panel.Location = new Point(237, 3);
panel.Name = "panel1";
panel.Size = new Size(542, 24);
panel.TabIndex = 1;
RadioButton radioButton1 = new RadioButton();
radioButton1.AutoSize = true;
radioButton1.Location = new Point(3, 3);
radioButton1.Name = "radioButton1";
radioButton1.TabIndex = 0;
radioButton1.TabStop = true;
radioButton1.Text = "税号";
radioButton1.UseVisualStyleBackColor = true;
RadioButton radioButton2 = new RadioButton();
radioButton2.AutoSize = true;
radioButton2.Location = new Point(60, 3);
radioButton2.Name = "radioButton2";
radioButton2.TabIndex = 1;
radioButton2.TabStop = true;
radioButton2.Text = "GTIN";
radioButton2.UseVisualStyleBackColor = true;
RadioButton radioButton3 = new RadioButton();
radioButton3.AutoSize = true;
radioButton3.Location = new Point(120, 3);
radioButton3.Name = "radioButton3";
radioButton3.TabIndex = 2;
radioButton3.TabStop = true;
radioButton3.Text = "汽车零件号";
radioButton3.UseVisualStyleBackColor = true;
RadioButton radioButton4 = new RadioButton();
radioButton4.AutoSize = true;
radioButton4.Location = new Point(220, 3);
radioButton4.Name = "radioButton4";
radioButton4.TabIndex = 3;
radioButton4.TabStop = true;
radioButton4.Text = "CAS";
radioButton4.UseVisualStyleBackColor = true;
panel.Controls.Add(radioButton1);
panel.Controls.Add(radioButton2);
panel.Controls.Add(radioButton3);
panel.Controls.Add(radioButton4);
panel.Parent = tableLayoutPanel2;
tableLayoutPanel2.Controls.Add(panel, 1, k);
}
}
Controls.Add(tableLayoutPanel2);
Button bnt_ok = new Button();
bnt_ok.Anchor = AnchorStyles.Left;
bnt_ok.Location = new Point(288, 19);
bnt_ok.Name = "bnt_ok";
bnt_ok.Size = new Size(75, 23);
bnt_ok.TabIndex = 0;
bnt_ok.Text = "确认";
bnt_ok.UseVisualStyleBackColor = true;
bnt_ok.Click += new EventHandler(bnt_ok_Click);
Button bnt_close = new Button();
bnt_close.Anchor = AnchorStyles.Left;
bnt_close.Location = new Point(378, 19);
bnt_close.Name = "bnt_close";
bnt_close.Size = new Size(75, 23);
bnt_close.TabIndex = 1;
bnt_close.Text = "关闭";
bnt_close.UseVisualStyleBackColor = true;
bnt_close.Click += new EventHandler(bnt_close_Click);
Panel panel2 = new Panel();
panel2.Dock = DockStyle.Bottom;
panel2.Location = new Point(0, 614);
panel2.Name = "panel2";
panel2.Size = new Size(782, 63);
panel2.TabIndex = 2;
panel2.Controls.Add(bnt_close);
panel2.Controls.Add(bnt_ok);
Controls.Add(panel2);
}
private void TextBox_TextChanged(object sender, EventArgs e)
{
monitoringVal();
}
private void Cmb_SelectedValueChanged(object sender, EventArgs e)
{
monitoringVal();
}
private void bnt_ok_Click(object sender, EventArgs e)
{
string strerror = "";
//动态值,可以从任何地方传过来
string str = "品牌类型,出口享惠情况,张三,李四,王五,马六,小明,小白,其他" + ",规格型号";
string[] strs = str.Split(',');
int count = strs.Length;
string[] strlist = strGModel.Split('|');
for (int i = 0; i < count - 4; i++)
{
if (string.IsNullOrWhiteSpace(strlist[i]))
{
strerror += strs[i] + "不能为空;";
}
}
if (strerror.Length > 0)
{
MessageBox.Show(strerror);
}
else {
this.Close();
//frmentry.setVal1(strGModel); 赋值
}
}
private void bnt_close_Click(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// 监听值(根据自己的业务修改就行)
/// </summary>
private void monitoringVal() {
string str = "";
string strGModel = "";
foreach (Control control in this.Controls)
{
if (control is TableLayoutPanel)
{
foreach (var item1 in control.Controls)
{
if (item1 is ComboBox)
{
str += ((ComboBox)item1).Text.Split('-')[0] + "|";
}
if (item1 is TextBox && ((TextBox)item1).Name != "textBox_gModel")
{
str += ((TextBox)item1).Text + "|";
}
}
if (str.Length > 0)
{
str = str.Substring(0, str.Length - 1);
}
strGModel = str;
}
if (control is TableLayoutPanel)
{
foreach (var item1 in control.Controls)
{
if (item1 is TextBox && ((TextBox)item1).Name == "textBox_gModel")
{
((TextBox)item1).Text = "";
((TextBox)item1).Text = strGModel;
}
}
}
}
}
}
}