窗体传值,子窗体,父窗体,反射,reflection,windows,组策略,gpedit.msc,动态创建窗体,谢谢,系列...

本文探讨了Windows应用程序中主窗体与子窗体间的数据传递机制,提出了使用字符串统一数据格式的方法,以简化代码并提高灵活性。
针对BTQ在系列一中提出的建议,上午我思考了一下,同时仔细研究了windwos组策略的子窗体返回值类型,发现不是数字,就是bool,居多,我想应该可以用string来统一,把数据的处理放在子窗体,这样主窗体只是传递string过去,接收string回来,就不用为每个窗体写传值和返回值了,希望可以有用。
下面是BTQ在系列一中的建议,谢谢!
#7楼 219.128.156.* 2008-05-27 08:53 BTQ [未注册用户]
两个Form之类数据的传递方式不是太好。
光一个PasswordLength的子窗体就写了这么多的代码的话,那么当更多类型的子窗体出现时,你的主窗体中代码将很长,判断也会很多。

我觉得统一一种机制去传递数据,子窗体在收到数据后,自行解析数据再具体处理会好一些。

1.JPG
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Reflection;
None.gif
None.gif
namespace  PersonalUdisk
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public partial class frmModifyConfig : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private TreeNode _selectedNode;
InBlock.gif        
private readonly string _policyType;
InBlock.gif
InBlock.gif        
public frmModifyConfig()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _selectedNode 
= e.Node;
InBlock.gif            lvConfig.Items.Clear();
InBlock.gif            Fill_lvConfig(_selectedNode);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void Fill_lvConfig(TreeNode treeNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//MessageBox.Show(Convert.ToString(PolicyType.日志策略));
InBlock.gif
            ListViewItem lvItem1;
InBlock.gif            
switch (treeNode.Text)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "PIN码策略":
InBlock.gif                    lvItem1 
= new ListViewItem();
InBlock.gif                    lvItem1.SubItems.Clear();
InBlock.gif                    lvItem1.SubItems[
0].Text = "PIN码长度最小值";
InBlock.gif                    lvItem1.SubItems.Add(
"5 个字符");
InBlock.gif                    lvItem1.SubItems.Add(
"frmPasswordLength");
InBlock.gif                    lvConfig.Items.Add(lvItem1);
InBlock.gif
InBlock.gif                    lvItem1 
= new ListViewItem();
InBlock.gif                    lvItem1.SubItems.Clear();
InBlock.gif                    lvItem1.SubItems[
0].Text = "PIN码最长存留期";
InBlock.gif                    lvItem1.SubItems.Add(
"42 天");
InBlock.gif                    lvItem1.SubItems.Add(
"frmPINOvertime");
InBlock.gif                    lvConfig.Items.Add(lvItem1);
InBlock.gif
InBlock.gif                    lvItem1 
= new ListViewItem();
InBlock.gif                    lvItem1.SubItems.Clear();
InBlock.gif                    lvItem1.SubItems[
0].Text = "PIN码复杂性要求";
InBlock.gif                    lvItem1.SubItems.Add(
"字母");
InBlock.gif                    lvItem1.SubItems.Add(
"frmPasswordComplexity");
InBlock.gif                    lvConfig.Items.Add(lvItem1);
InBlock.gif                    
InBlock.gif                
InBlock.gif                    lvItem1 
= new ListViewItem();
InBlock.gif                    lvItem1.SubItems.Clear();
InBlock.gif                    lvItem1.SubItems[
0].Text = "PIN码历史记录次数";
InBlock.gif                    lvItem1.SubItems.Add(
"5 个记住的密钥");
InBlock.gif                    lvItem1.SubItems.Add(
"frmPINHistoryNumber");
InBlock.gif                    lvConfig.Items.Add(lvItem1);
InBlock.gif
InBlock.gif                   
break;
InBlock.gif                
case "日志策略":
InBlock.gif                    lvItem1 
= new ListViewItem();
InBlock.gif                    lvItem1.SubItems.Clear();
InBlock.gif                    lvItem1.SubItems[
0].Text = "禁用日志";
InBlock.gif                    lvItem1.SubItems.Add(
"禁用");
InBlock.gif                    lvItem1.SubItems.Add(
"frmForbidLog");
InBlock.gif                    lvConfig.Items.Add(lvItem1);
InBlock.gif                    
break;
InBlock.gif                
case "登录策略":
InBlock.gif                    lvItem1 
= new ListViewItem();
InBlock.gif                    lvItem1.SubItems.Clear();
InBlock.gif                    lvItem1.SubItems[
0].Text = "登录方式";
InBlock.gif                    lvItem1.SubItems.Add(
"dot.gif");
InBlock.gif                    lvItem1.SubItems.Add(
"frmLoginManner");
InBlock.gif                    lvConfig.Items.Add(lvItem1);
InBlock.gif                    
break;
InBlock.gif                
case "进程及文件策略":
InBlock.gif                    lvItem1 
= new ListViewItem();
InBlock.gif                    lvItem1.SubItems.Clear();
InBlock.gif                    lvItem1.SubItems[
0].Text = "进程白名单";
InBlock.gif                    lvItem1.SubItems.Add(
"dot.gif");
InBlock.gif                    lvItem1.SubItems.Add(
"frmProcessWhiteList");
InBlock.gif                    lvConfig.Items.Add(lvItem1);
InBlock.gif
InBlock.gif                    lvItem1 
= new ListViewItem();
InBlock.gif                    lvItem1.SubItems.Clear();
InBlock.gif                    lvItem1.SubItems[
0].Text = "文件拷贝类型黑名单";
InBlock.gif                    lvItem1.SubItems.Add(
"dot.gif");
InBlock.gif                    lvItem1.SubItems.Add(
"frmFileTypeBlackList");
InBlock.gif                    lvConfig.Items.Add(lvItem1);
InBlock.gif
InBlock.gif                    lvItem1 
= new ListViewItem();
InBlock.gif                    lvItem1.SubItems.Clear();
InBlock.gif                    lvItem1.SubItems[
0].Text = "文件打开类型黑名单";
InBlock.gif                    lvItem1.SubItems.Add(
"dot.gif");
InBlock.gif                    lvItem1.SubItems.Add(
"frmExecuteFileTypeBlackList");
InBlock.gif                    lvConfig.Items.Add(lvItem1);
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
private string _childValue;
InBlock.gif        
public string ChildValue
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this._childValue; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis._childValue = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private string _parentValue;
InBlock.gif        
public string ParentValue
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this._parentValue; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis._parentValue = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
private void lvConfig_DoubleClick(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Type type 
= Type.GetType("PersonalUdisk." + lvConfig.SelectedItems[0].SubItems[2].Text);
InBlock.gif          
InBlock.gif            
this._parentValue = lvConfig.SelectedItems[0].SubItems[1].Text;
InBlock.gif            
object obj = Activator.CreateInstance(type);
InBlock.gif
InBlock.gif            Form form 
= (Form)obj;
InBlock.gif            form.Owner 
= this;
InBlock.gif            form.ShowDialog();
InBlock.gif            
if (form.DialogResult == DialogResult.OK)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                
this.lvConfig.SelectedItems[0].SubItems[1].Text = Convert.ToString(type.GetProperty("ChildValue").GetValue(form, null));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void frmModifyConfig_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
public enum PolicyType
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        PIN码策略,
InBlock.gif        登录策略,
InBlock.gif        日志策略,
InBlock.gif        进程及文件策略
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
2.JPG
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
namespace  PersonalUdisk
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public partial class frmPasswordLength : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
private frmModifyConfig frmParent;
InBlock.gif        
InBlock.gif        
private string _parentValue;
InBlock.gif        
public string ParentValue
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this._parentValue; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis._parentValue = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string _childValue;
InBlock.gif
InBlock.gif        
public string ChildValue
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this._childValue; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis._childValue = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public frmPasswordLength()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
InBlock.gif            btnOK.DialogResult 
= DialogResult.OK;
InBlock.gif            btnCancel.DialogResult 
= DialogResult.Cancel;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int PINLength
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn Convert.ToInt32(this.numericPINLength.Value); }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis.numericPINLength.Value = value; ;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void numericPINLength_ValueChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ModifylblPINLengthInfo();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ModifylblPINLengthInfo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (this.numericPINLength.Value == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.lblPINLengthInfo.Text = "不要求PIN码。";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.lblPINLengthInfo.Text = "PIN码必须至少是:";
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
private void frmPasswordLength_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            frmParent 
= (frmModifyConfig)this.Owner;
InBlock.gif            
this.numericPINLength.Value =Convert.ToInt32( frmParent.ParentValue.Substring(0, frmParent.ParentValue.IndexOf(" ")));
InBlock.gif            ModifylblPINLengthInfo();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btnOK_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this._childValue = Convert.ToString(this.numericPINLength.Value)+ " 个字符";
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
3.JPG
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
namespace  PersonalUdisk
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public partial class frmPasswordComplexity : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private frmModifyConfig _frmParent;
InBlock.gif        
private string _childValue;
InBlock.gif        
private string _parentValue;
InBlock.gif
InBlock.gif        
public string ParentValue
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _parentValue; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _parentValue = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string ChildValue
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this._childValue; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis._childValue = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public frmPasswordComplexity()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
InBlock.gif            
this.btnOK.DialogResult = DialogResult.OK;
InBlock.gif            
this.btnCancel.DialogResult = DialogResult.Cancel;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private void frmPasswordComplexity_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this._frmParent = (frmModifyConfig)this.Owner;
InBlock.gif            
this._parentValue = _frmParent.ParentValue;
InBlock.gif            modifyChecked(_parentValue);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void modifyChecked(string parentValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
switch (parentValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "字母":
InBlock.gif                    
this.rbtnChar.Checked = true;
InBlock.gif                    
break;
InBlock.gif                
case "字母+数字":
InBlock.gif                    
this.rbtnCharNum.Checked = true;
InBlock.gif                    
break;
InBlock.gif                
case "字母+数字+特殊字符":
InBlock.gif                    
this.rbtnCharNumSpecial.Checked = true;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btnOK_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (rbtnChar.Checked == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this._childValue = "字母";
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (rbtnCharNum.Checked)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this._childValue = "字母+数字";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if ( this.rbtnCharNumSpecial .Checked)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this._childValue = "字母+数字+特殊字符";
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值