使用委托在多个窗体间传值
来源:互联网 发布:端口穿透 编辑:程序博客网 时间: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;
}
}
}
}
...{
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);
}
}
}
}
- 使用委托在多个窗体间传值
- C#委托-在窗体中使用委托和事件
- 委托实现窗体间传值
- C#利用委托和事件在窗体间传值问题
- 使用委托在页面间传值.
- 委托(一个主窗体统计多个从窗体的按钮单击的次数)
- 8.29 2个窗体调用(委托)
- 委托、窗体传值、多播委托
- 委托(窗体传值,多播委托)
- c#利用委托窗体间传值
- Winform窗体间传值(委托)
- 匿名委托,在线程中刷新窗体
- [VB.NET]如何在第二个窗体里使用第一个窗体的控件方法
- 利用委托在子窗体设置父窗体控件
- 简单窗体使用委托、事件传值
- 使用委托实现窗体的移动
- 【C#】使用委托实现窗体传值
- ios中利用委托在二个视图间传值
- 怎么使用SSL
- 科技图书翻译思想指南(上篇)
- 禁止网页被抄袭技巧
- Office VBA用WinAPI实现“另存为”对话框
- J2ME-CLDC/MIDP 资源
- 使用委托在多个窗体间传值
- 对于C#中委托和事件的理解
- Session机制的本质
- 新婚之夜,我手抚老婆两腿之间问:这是什么?
- PL/SQL报无效的窗口句柄的解决办法
- 互联网的力量——从南方周末.系统.巨人.征途事件启迪
- Linux下安装JDK1.6,Eclipse
- ASP创建ACCESS表或添加字段
- ora-00932:数据类型不一致:应为 -,但却获得BLOB