在實際應用中,很多時候需要在多個窗體間進行傳值,而在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)
在實際應用中,很多時候需要在多個窗體間進行傳值,而在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)

...{

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;

}




初始化字體#region 初始化字體

public void InitFont()


...{


FontFamily[] family = FontFamily.FamilIEs;


txtFont.Text = family[1].Name.ToString();


foreach (FontFamily f in family)


...{

lstFont.Items.Add(f.GetName(1).ToString());

}

}

#endregion



初始化字體樣式#region 初始化字體樣式

public void InitFontStyle()


...{

Type fontStyle = typeof(FontStyle);


FieldInfo[] fontStyles = fontStyle.GetFIElds();


txtFontStyle.Text = fontStyles[1].Name.ToString();


for (int i = 1; i < fontStyles.Length; i++)


...{

lstFontStyle.Items.Add(fontStyles[i].Name.ToString());

}


}

#endregion



初始化字體大小#region 初始化字體大小

public void InitFontSize()


...{

for (int i = 9; i < 40; i++)


...{

lstFontSize.Items.Add(i.ToString());

}


lstFontSize.SelectedItem = "9";


txtFontSize.Text = "9";



}

#endregion


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();

}



得到用戶選中的字體樣式#region 得到用戶選中的字體樣式

private FontStyle GetFontStyle(string fontStyle)


...{

if (txtFontStyle.Text == "Bold")


...{

return FontStyle.Bold;

}

else if (txtFontStyle.Text == "Italic")


...{

return FontStyle.Italic;

}

else if (txtFontStyle.Text == "Regular")


...{

&nbs; return FontStyle.Regular;

}


return 0;

}

#endregion


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);

}

}

}

}