我曾今 在窗体2 使用 窗体1 f = new 窗体1(); f.刷新函数 但是不刷新为什么。 而刷新函数在 窗体1执行就可以 --------------------- 窗体1 f = new 窗体1(); 你又实例化一个窗体1
LZ把窗体2定义成dialog可能会更好处理一点吧
不用委托了。用ShowDialog()方法。然后返回。代码如下: MainFrom.cs /* * Created by SharpDevelop. * User: admin * Date: 2010-1-12 * Time: 14:07 * * To change this template use Tools | Options | Coding | Edit Standard Headers. */ using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms;namespace ShowDialogForm { /// <summary> /// Description of MainForm. /// </summary> public partial class MainForm : Form { public MainForm() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent();
// // TODO: Add constructor code after the InitializeComponent() call. // }
void Button1Click(object sender, EventArgs e) { Form1 form1=new Form1(); if(form1.ShowDialog()==DialogResult.OK) { MessageBox.Show(form1.Form1Str); } } } } Form1.cs主要代码/* * Created by SharpDevelop. * User: admin * Date: 2010-1-12 * Time: 14:09 * * To change this template use Tools | Options | Coding | Edit Standard Headers. */ using System; using System.Drawing; using System.Windows.Forms;namespace ShowDialogForm { /// <summary> /// Description of Form1. /// </summary> public partial class Form1 : Form { private string form1Str; public Form1() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent();
// // TODO: Add constructor code after the InitializeComponent() call. // } /// <summary> /// 获取Form1的值 /// </summary> public string Form1Str { get { return form1Str; } } void Button1Click(object sender, EventArgs e) { form1Str="OK"; this.DialogResult=DialogResult.OK; }
窗体1 f = new 窗体1();
f.刷新函数 但是不刷新为什么。 而刷新函数在 窗体1执行就可以
---------------------
窗体1 f = new 窗体1(); 你又实例化一个窗体1
MainFrom.cs /*
* Created by SharpDevelop.
* User: admin
* Date: 2010-1-12
* Time: 14:07
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;namespace ShowDialogForm
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void Button1Click(object sender, EventArgs e)
{
Form1 form1=new Form1();
if(form1.ShowDialog()==DialogResult.OK)
{
MessageBox.Show(form1.Form1Str);
}
}
}
}
Form1.cs主要代码/*
* Created by SharpDevelop.
* User: admin
* Date: 2010-1-12
* Time: 14:09
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Drawing;
using System.Windows.Forms;namespace ShowDialogForm
{
/// <summary>
/// Description of Form1.
/// </summary>
public partial class Form1 : Form
{
private string form1Str;
public Form1()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
/// <summary>
/// 获取Form1的值
/// </summary>
public string Form1Str
{
get
{
return form1Str;
}
}
void Button1Click(object sender, EventArgs e)
{
form1Str="OK";
this.DialogResult=DialogResult.OK;
}
void Button2Click(object sender, EventArgs e)
{
this.DialogResult=DialogResult.Cancel;
}
}
}
if(frm = null)
{
frm.RefreshDataSource();
}
在窗体1中:
Form2 newForm = new Form2();
newForm.ShowDialog();
this.RefreshForm1()//刷新列表的方法
是两个平级窗体 不是parent