代码改变世界

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

2008-05-27 11:52  Virus-BeautyCode  阅读(3269)  评论(0编辑  收藏  举报
针对BTQ在系列一中提出的建议,上午我思考了一下,同时仔细研究了windwos组策略的子窗体返回值类型,发现不是数字,就是bool,居多,我想应该可以用string来统一,把数据的处理放在子窗体,这样主窗体只是传递string过去,接收string回来,就不用为每个窗体写传值和返回值了,希望可以有用。
下面是BTQ在系列一中的建议,谢谢!

#7楼 219.128.156.* 2008-05-27 08:53 BTQ [未注册用户]

两个Form之类数据的传递方式不是太好。
光一个PasswordLength的子窗体就写了这么多的代码的话,那么当更多类型的子窗体出现时,你的主窗体中代码将很长,判断也会很多。

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

1.JPG
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace PersonalUdisk
{
    
public partial class frmModifyConfig : Form
    
{
        
private TreeNode _selectedNode;
        
private readonly string _policyType;

        
public frmModifyConfig()
        
{
            InitializeComponent();
        }


        
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        
{
            _selectedNode 
= e.Node;
            lvConfig.Items.Clear();
            Fill_lvConfig(_selectedNode);

        }

        
private void Fill_lvConfig(TreeNode treeNode)
        
{
            
//MessageBox.Show(Convert.ToString(PolicyType.日志策略));
            ListViewItem lvItem1;
            
switch (treeNode.Text)
            
{
                
case "PIN码策略":
                    lvItem1 
= new ListViewItem();
                    lvItem1.SubItems.Clear();
                    lvItem1.SubItems[
0].Text = "PIN码长度最小值";
                    lvItem1.SubItems.Add(
"5 个字符");
                    lvItem1.SubItems.Add(
"frmPasswordLength");
                    lvConfig.Items.Add(lvItem1);

                    lvItem1 
= new ListViewItem();
                    lvItem1.SubItems.Clear();
                    lvItem1.SubItems[
0].Text = "PIN码最长存留期";
                    lvItem1.SubItems.Add(
"42 天");
                    lvItem1.SubItems.Add(
"frmPINOvertime");
                    lvConfig.Items.Add(lvItem1);

                    lvItem1 
= new ListViewItem();
                    lvItem1.SubItems.Clear();
                    lvItem1.SubItems[
0].Text = "PIN码复杂性要求";
                    lvItem1.SubItems.Add(
"字母");
                    lvItem1.SubItems.Add(
"frmPasswordComplexity");
                    lvConfig.Items.Add(lvItem1);
                    
                
                    lvItem1 
= new ListViewItem();
                    lvItem1.SubItems.Clear();
                    lvItem1.SubItems[
0].Text = "PIN码历史记录次数";
                    lvItem1.SubItems.Add(
"5 个记住的密钥");
                    lvItem1.SubItems.Add(
"frmPINHistoryNumber");
                    lvConfig.Items.Add(lvItem1);

                   
break;
                
case "日志策略":
                    lvItem1 
= new ListViewItem();
                    lvItem1.SubItems.Clear();
                    lvItem1.SubItems[
0].Text = "禁用日志";
                    lvItem1.SubItems.Add(
"禁用");
                    lvItem1.SubItems.Add(
"frmForbidLog");
                    lvConfig.Items.Add(lvItem1);
                    
break;
                
case "登录策略":
                    lvItem1 
= new ListViewItem();
                    lvItem1.SubItems.Clear();
                    lvItem1.SubItems[
0].Text = "登录方式";
                    lvItem1.SubItems.Add(
"");
                    lvItem1.SubItems.Add(
"frmLoginManner");
                    lvConfig.Items.Add(lvItem1);
                    
break;
                
case "进程及文件策略":
                    lvItem1 
= new ListViewItem();
                    lvItem1.SubItems.Clear();
                    lvItem1.SubItems[
0].Text = "进程白名单";
                    lvItem1.SubItems.Add(
"");
                    lvItem1.SubItems.Add(
"frmProcessWhiteList");
                    lvConfig.Items.Add(lvItem1);

                    lvItem1 
= new ListViewItem();
                    lvItem1.SubItems.Clear();
                    lvItem1.SubItems[
0].Text = "文件拷贝类型黑名单";
                    lvItem1.SubItems.Add(
"");
                    lvItem1.SubItems.Add(
"frmFileTypeBlackList");
                    lvConfig.Items.Add(lvItem1);

                    lvItem1 
= new ListViewItem();
                    lvItem1.SubItems.Clear();
                    lvItem1.SubItems[
0].Text = "文件打开类型黑名单";
                    lvItem1.SubItems.Add(
"");
                    lvItem1.SubItems.Add(
"frmExecuteFileTypeBlackList");
                    lvConfig.Items.Add(lvItem1);
                    
break;
            }

        }



        
private string _childValue;
        
public string ChildValue
        
{
            
get return this._childValue; }
            
set this._childValue = value; }
        }

        
        
private string _parentValue;
        
public string ParentValue
        
{
            
get return this._parentValue; }
            
set this._parentValue = value; }
        }



        
private void lvConfig_DoubleClick(object sender, EventArgs e)
        
{
            Type type 
= Type.GetType("PersonalUdisk." + lvConfig.SelectedItems[0].SubItems[2].Text);
          
            
this._parentValue = lvConfig.SelectedItems[0].SubItems[1].Text;
            
object obj = Activator.CreateInstance(type);

            Form form 
= (Form)obj;
            form.Owner 
= this;
            form.ShowDialog();
            
if (form.DialogResult == DialogResult.OK)
            
{

                
this.lvConfig.SelectedItems[0].SubItems[1].Text = Convert.ToString(type.GetProperty("ChildValue").GetValue(form, null));
            }

        }


        
private void frmModifyConfig_Load(object sender, EventArgs e)
        
{

        }

    }

    
public enum PolicyType
    
{
        PIN码策略,
        登录策略,
        日志策略,
        进程及文件策略
    }

}
2.JPG
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PersonalUdisk
{
    
public partial class frmPasswordLength : Form
    
{

        
private frmModifyConfig frmParent;
        
        
private string _parentValue;
        
public string ParentValue
        
{
            
get return this._parentValue; }
            
set this._parentValue = value; }
        }

        
private string _childValue;

        
public string ChildValue
        
{
            
get return this._childValue; }
            
set this._childValue = value; }
        }

        
public frmPasswordLength()
        
{
            InitializeComponent();
            btnOK.DialogResult 
= DialogResult.OK;
            btnCancel.DialogResult 
= DialogResult.Cancel;

        }

        
public int PINLength
        
{
            
get return Convert.ToInt32(this.numericPINLength.Value); }
            
set this.numericPINLength.Value = value; ;}
        }


        
private void numericPINLength_ValueChanged(object sender, EventArgs e)
        
{
            ModifylblPINLengthInfo();
        }


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

            
else
            
{
                
this.lblPINLengthInfo.Text = "PIN码必须至少是:";
            }

        }



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


        
private void btnOK_Click(object sender, EventArgs e)
        
{
            
this._childValue = Convert.ToString(this.numericPINLength.Value)+ " 个字符";

        }



    }

}
3.JPG
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PersonalUdisk
{
    
public partial class frmPasswordComplexity : Form
    
{
        
private frmModifyConfig _frmParent;
        
private string _childValue;
        
private string _parentValue;

        
public string ParentValue
        
{
            
get return _parentValue; }
            
set { _parentValue = value; }
        }

        
public string ChildValue
        
        
{
            
get return this._childValue; }
            
set this._childValue = value; }
        }


        
public frmPasswordComplexity()
        
{
            InitializeComponent();
            
this.btnOK.DialogResult = DialogResult.OK;
            
this.btnCancel.DialogResult = DialogResult.Cancel;
        }

        
        
private void frmPasswordComplexity_Load(object sender, EventArgs e)
        
{
            
this._frmParent = (frmModifyConfig)this.Owner;
            
this._parentValue = _frmParent.ParentValue;
            modifyChecked(_parentValue);

        }


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

        }


        
private void btnOK_Click(object sender, EventArgs e)
        
{
            
if (rbtnChar.Checked == true)
            
{
                
this._childValue = "字母";

            }

            
else if (rbtnCharNum.Checked)
            
{
                
this._childValue = "字母+数字";
            }

            
else if ( this.rbtnCharNumSpecial .Checked)
            
{
                
this._childValue = "字母+数字+特殊字符";
            }

        }

    }

}