子窗体通过事件修改父窗体内容
下面我们例子是这样的,父窗体是一个列表窗体,如图所示:
选择一个考生,点击【设置成绩】按钮,弹出如下子窗口:
这里我们要实现【上一个】、【下一个】选择父窗体中的记录。
原本我是准备在子窗体中重新读取数据源,然后实现向上或向下
滚动一条记录,但当父窗体体的记录按不同的列排序时,子窗体
的记录不能正确滚动。想来想去,还是在子窗体中创建事件比较
合理,现在我想与大家分享一下,也给自己做个备忘录,方便日
后查询。
我们先在子窗体中定义两个事件,如下:

#region 定义事件
/// <summary>
/// 上一个
/// </summary>
public event ExtendEventHandler PreviewClick;
/// <summary>
/// 下一个
/// </summary>
public event ExtendEventHandler NextClick;
#endregion
/// <summary>
/// 上一个
/// </summary>
public event ExtendEventHandler PreviewClick;
/// <summary>
/// 下一个
/// </summary>
public event ExtendEventHandler NextClick;
#endregion
ExtendEventHandler是一个自定义的委托,

/// <summary>
/// 处理事件的扩展方法
/// </summary>
/// <param name="sender">事件源</param>
/// <param name="e">包含事件数据的扩展 System.EventArgs</param>
public delegate void ExtendEventHandler(object sender,ExtendEventArgs e);
/// 处理事件的扩展方法
/// </summary>
/// <param name="sender">事件源</param>
/// <param name="e">包含事件数据的扩展 System.EventArgs</param>
public delegate void ExtendEventHandler(object sender,ExtendEventArgs e);
然后我们通过点击【上一个】、【下一个】按钮去触发这两个事件,代码如下:

//上一个
private void btnPreview_Click(object sender, EventArgs e)
{
if (PreviewClick != null)
{
this.Cursor = Cursors.WaitCursor;
//
ExtendEventArgs ExArgs = new ExtendEventArgs();
ExArgs.Current = txtKSBH.Text.Trim();
PreviewClick(sender, ExArgs);
//
this.Cursor = Cursors.Default;
}
}
//下一个
private void btnNext_Click(object sender, EventArgs e)
{
if (NextClick != null)
{
this.Cursor = Cursors.WaitCursor;
//
ExtendEventArgs ExArgs = new ExtendEventArgs();
ExArgs.Current = txtKSBH.Text.Trim();
NextClick(sender, ExArgs);
//
this.Cursor = Cursors.Default;
}
}
private void btnPreview_Click(object sender, EventArgs e)
{
if (PreviewClick != null)
{
this.Cursor = Cursors.WaitCursor;
//
ExtendEventArgs ExArgs = new ExtendEventArgs();
ExArgs.Current = txtKSBH.Text.Trim();
PreviewClick(sender, ExArgs);
//
this.Cursor = Cursors.Default;
}
}
//下一个
private void btnNext_Click(object sender, EventArgs e)
{
if (NextClick != null)
{
this.Cursor = Cursors.WaitCursor;
//
ExtendEventArgs ExArgs = new ExtendEventArgs();
ExArgs.Current = txtKSBH.Text.Trim();
NextClick(sender, ExArgs);
//
this.Cursor = Cursors.Default;
}
}
这里我自己定义了一个事件参数ExtendEventArgs,其代码很简单,如下所示:

/// <summary>
/// 包含事件数据的扩展类
/// </summary>
public class ExtendEventArgs : EventArgs
{
object _Current;
/// <summary>
/// 当前值
/// </summary>
public object Current
{
get { return _Current; }
set { _Current = value; }
}
}
/// 包含事件数据的扩展类
/// </summary>
public class ExtendEventArgs : EventArgs
{
object _Current;
/// <summary>
/// 当前值
/// </summary>
public object Current
{
get { return _Current; }
set { _Current = value; }
}
}
到此,子窗体中的事件就已经定义好了,下面,我们只需要到父窗体中去实现就可以了。
比如我们在父窗体中列表控件的双击事件中去弹出子窗体,实现代码如下:

private void lstView_DoubleClick(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
//
foreach (ListViewItem item in lstView.SelectedItems)
{
CurrentItem = item;
keyinFM = new frmKeyinJFSCJ(true);
keyinFM.ShowData(item.SubItems[1].Text
, item.SubItems[2].Text
, item.SubItems[3].Text
, item.SubItems[4].Text
, item.SubItems[5].Text
, item.SubItems[6].Text
, item.SubItems[7].Text
, item.SubItems[8].Text);
keyinFM.PreviewClick += new YanZhao.Framework.Event.ExtendEventHandler(keyinFM_PreviewClick);
keyinFM.NextClick += new YanZhao.Framework.Event.ExtendEventHandler(keyinFM_NextClick);
keyinFM.ShowDialog();
break;
}
//
this.Cursor = Cursors.Default;
}
{
this.Cursor = Cursors.WaitCursor;
//
foreach (ListViewItem item in lstView.SelectedItems)
{
CurrentItem = item;
keyinFM = new frmKeyinJFSCJ(true);
keyinFM.ShowData(item.SubItems[1].Text
, item.SubItems[2].Text
, item.SubItems[3].Text
, item.SubItems[4].Text
, item.SubItems[5].Text
, item.SubItems[6].Text
, item.SubItems[7].Text
, item.SubItems[8].Text);
keyinFM.PreviewClick += new YanZhao.Framework.Event.ExtendEventHandler(keyinFM_PreviewClick);
keyinFM.NextClick += new YanZhao.Framework.Event.ExtendEventHandler(keyinFM_NextClick);
keyinFM.ShowDialog();
break;
}
//
this.Cursor = Cursors.Default;
}
实现PreviewClick事件的方法是keyinFM_PreviewClick,NextClick事件的方法是keyinFM_NextClick,具体代码如下:

void keyinFM_PreviewClick(object sender, ExtendEventArgs e)
{
bool HaveFind = false;
for (int i = lstView.Items.Count - 1; i >= 0; i--)
{
if (HaveFind)
{
if (keyinFM != null)
{
CurrentItem = lstView.Items[i];
keyinFM.ShowData(CurrentItem.SubItems[1].Text
, CurrentItem.SubItems[2].Text
, CurrentItem.SubItems[3].Text
, CurrentItem.SubItems[4].Text
, CurrentItem.SubItems[5].Text
, CurrentItem.SubItems[6].Text
, CurrentItem.SubItems[7].Text
, CurrentItem.SubItems[8].Text);
}
break;
}
else
{
if (lstView.Items[i].SubItems[1].Text == e.Current.ToString())
{
HaveFind = true;
continue;
}
}
}
}
void keyinFM_NextClick(object sender, ExtendEventArgs e)
{
bool HaveFind = false;
for (int i = 0; i < lstView.Items.Count; i++)
{
if (HaveFind)
{
if (keyinFM != null)
{
CurrentItem = lstView.Items[i];
keyinFM.ShowData(CurrentItem.SubItems[1].Text
, CurrentItem.SubItems[2].Text
, CurrentItem.SubItems[3].Text
, CurrentItem.SubItems[4].Text
, CurrentItem.SubItems[5].Text
, CurrentItem.SubItems[6].Text
, CurrentItem.SubItems[7].Text
, CurrentItem.SubItems[8].Text);
}
break;
}
else
{
if (lstView.Items[i].SubItems[1].Text == e.Current.ToString())
{
HaveFind = true;
continue;
}
}
}
}
{
bool HaveFind = false;
for (int i = lstView.Items.Count - 1; i >= 0; i--)
{
if (HaveFind)
{
if (keyinFM != null)
{
CurrentItem = lstView.Items[i];
keyinFM.ShowData(CurrentItem.SubItems[1].Text
, CurrentItem.SubItems[2].Text
, CurrentItem.SubItems[3].Text
, CurrentItem.SubItems[4].Text
, CurrentItem.SubItems[5].Text
, CurrentItem.SubItems[6].Text
, CurrentItem.SubItems[7].Text
, CurrentItem.SubItems[8].Text);
}
break;
}
else
{
if (lstView.Items[i].SubItems[1].Text == e.Current.ToString())
{
HaveFind = true;
continue;
}
}
}
}
void keyinFM_NextClick(object sender, ExtendEventArgs e)
{
bool HaveFind = false;
for (int i = 0; i < lstView.Items.Count; i++)
{
if (HaveFind)
{
if (keyinFM != null)
{
CurrentItem = lstView.Items[i];
keyinFM.ShowData(CurrentItem.SubItems[1].Text
, CurrentItem.SubItems[2].Text
, CurrentItem.SubItems[3].Text
, CurrentItem.SubItems[4].Text
, CurrentItem.SubItems[5].Text
, CurrentItem.SubItems[6].Text
, CurrentItem.SubItems[7].Text
, CurrentItem.SubItems[8].Text);
}
break;
}
else
{
if (lstView.Items[i].SubItems[1].Text == e.Current.ToString())
{
HaveFind = true;
continue;
}
}
}
}
到这里【上一个】、【下一个】按钮的功能都已完成。