使用委托在多个窗体间传值

来源:互联网 发布:端口穿透 编辑:程序博客网 时间:2024/05/16 08:03

      在实际应用中,很多时候需要在多个窗体间进行传值,而在winform 中所有成员都是私有的,在子窗体中无法访问.通过委托和事件就可以进行一个函数的回调.具体代码如下:

主窗体:FrmNotePad

namespace Notepad
{
    
public partial class FrmNotePad : Form
    
{
        
private int count = 0;
        
public FrmNotePad()
        
{
            InitializeComponent();
        }


        
private void FrmNotePad_Load(object sender, EventArgs e)
        
{
            
           
        }


        
private void FrmNotePad_Resize(object sender, EventArgs e)
        
{
            txtContent.Width 
= this.Width - 10;
            txtContent.Height 
= this.Height;
        }


        
private void menuWarp_Click(object sender, EventArgs e)
        
{
            menuWarp.Checked 
= true;
            txtContent.WordWrap 
= true;
            count
++;
            
if (count % 2==0)
            
{
                menuWarp.Checked 
= false;
                txtContent.WordWrap 
= false;

            }

             
        }


        
private void menuFont_Click(object sender, EventArgs e)
        
{
            NotepadFont  myFont  
= new NotepadFont();

            myFont.Font 
+= new DelegateFont(SetTxtContentFont);   //订阅事件

            FrmFont frmFont 
= new FrmFont(myFont);
            frmFont.ShowDialog();

            SetTxtContentFont(myFont.FontFamily,myFont.FontSize,myFont.NFontStyle);
        }


        
private void SetTxtContentFont(string fontFmaily,float fontSize,FontStyle  fontStyle)
        
{
            txtContent.Font 
= new Font(fontFmaily, fontSize, fontStyle);
        }


        
private void FrmNotePad_FormClosing(object sender, FormClosingEventArgs e)
        
{
            e.Cancel 
= true;
           DialogResult   myResult  
= MessageBox.Show("是否保存文档""系统消息", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

           
if (myResult == DialogResult.Yes)
           
{
               saveDialog.Filter 
= "文本文件(*.txt)|*.txt|所有文件|*.*";
               saveDialog.FileName 
= "*.txt";
             DialogResult  myResult2 
=   saveDialog.ShowDialog();

             
if (myResult2 == DialogResult.OK)
             
{

                 e.Cancel 
= false;
             }

             
else
             
{
                 e.Cancel 
= true;
             }

           }

           
else if (myResult == DialogResult.No)
           
{
               e.Cancel 
= false;
           }

           
else if(myResult == DialogResult.Cancel)
           
{
               e.Cancel 
= true;
           }

         
        }

       
    }

}

 

子窗体:FrmFont

 

namespace Notepad
{
    
public partial class FrmFont : Form
    
{
        
private NotepadFont _myFont;
        
public FrmFont(NotepadFont myFont)
        
{
            InitializeComponent();
            _myFont 
= myFont;
        }


        
private void FrmFont_Load(object sender, EventArgs e)
        
{
            InitFont();
            InitFontStyle();
            InitFontSize();

            
this.AcceptButton = btnCancel;
        }



        
初始化字体 

        
初始化字体样式 

        
初始化字体大小 

        
private void lstFont_SelectedIndexChanged(object sender, EventArgs e)
        
{
           

        }


        
private void lstFontStyle_SelectedIndexChanged(object sender, EventArgs e)
        
{
            txtFontStyle.Text 
= lstFontStyle.SelectedItem.ToString();
            _myFont.SetFont(txtFont.Text, 
float.Parse(txtFontSize.Text), GetFontStyle(txtFontStyle.Text));

        }


        
private void lstFontSize_SelectedIndexChanged(object sender, EventArgs e)
        
{
            txtFontSize.Text 
= lstFontSize.SelectedItem.ToString();
            _myFont.SetFont(txtFont.Text, 
float.Parse(txtFontSize.Text), GetFontStyle(txtFontStyle.Text));
        }


        
private void btnCancel_Click(object sender, EventArgs e)
        
{
            _myFont.FontFamily 
= lstFont.Items[0].ToString();

            _myFont.FontSize 
= 9f;

            _myFont.NFontStyle 
= FontStyle.Regular;
            
this.Hide();
        }


        
private void FrmFont_HelpRequested(object sender, HelpEventArgs hlpevent)
        
{
         
        }


        
private void btnOK_Click(object sender, EventArgs e)
        
{
            _myFont.FontFamily 
= txtFont.Text;

            _myFont.NFontStyle 
= GetFontStyle(txtFontStyle.Text);

            _myFont.FontSize 
= float.Parse(txtFontSize.Text);

         
//   this.Hide();
        }


        
得到用户选中的字体样式 

        
private void lstFont_Click(object sender, EventArgs e)
        
{
            txtFont.Text 
= lstFont.SelectedItem.ToString();
            _myFont.SetFont(txtFont.Text, 
float.Parse(txtFontSize.Text),GetFontStyle(txtFontStyle.Text));
        }


        
private void FrmFont_FormClosing(object sender, FormClosingEventArgs e)
        
{
           
        }


    }

}

用于传值的委托类:

 

namespace Notepad
{

    
public delegate void DelegateFont(string fontFamily, float fontSize, FontStyle fontStyle);
     
public  class NotepadFont
    
{
         
public event DelegateFont Font;

        
private string fontFamily;

        
public string FontFamily
        
{
            
get return fontFamily; }
            
set { fontFamily = value; }
        }


        
private float fontSize;

        
public float FontSize
        
{
            
get return fontSize; }
            
set { fontSize = value; }
        }


         
private FontStyle nfontStyle;

        
public FontStyle NFontStyle
        
{
            
get return nfontStyle; }
            
set { nfontStyle = value; }
        }



         
public void SetFont(string fontFamily, float fontSize, FontStyle fontStyle)
         
{
             
if (Font != null)
             
{
                 Font(fontFamily, fontSize, fontStyle);
             }

         }

    }

}

 

 

原创粉丝点击