在实际应用中,很多时候需要在多个窗体间进行传值,而在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;
}
}
}
}
... {
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;
}
初始化字体#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")
...{
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)
...{
}
}
}
... {
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")
...{
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);
}
}
}
}
... {
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);
}
}
}
}