日期:2014-05-19  浏览次数:21166 次

对象互操作,怎么实现?
有A,B两个窗体,现在我要点B里的一个按钮来执行A窗体的一个过程要怎么实现?

------解决方案--------------------
1 public
2 委托
------解决方案--------------------
将A中过程或函数设置为Public
在B中创建一个A的实例

------解决方案--------------------
你的窗体是Form还是其他?

是Form就需要传递消息.
------解决方案--------------------
窗体互相引用增加了 耦合 度. 建议楼主用 Delegate+Event, 如下是实例:
1 定义一个公用的委托
public delegate void DataSourceAddDelegate();
2 中间类:
public class MyDataSource
{
public static ArrayList m_ArrayList = new ArrayList();
public static event DataSourceAddDelegate DataSourceAddEvent;

public static void Add(string str)
{
m_ArrayList.Add(str );
if (DataSourceAddEvent != null)
DataSourceAddEvent();
}
}

3 主窗体 Form1:
public Form1()
{
InitializeComponent();
MyDataSource.DataSourceAddEvent += new DataSourceAddDelegate(RefreshListBox);
}

private void RefreshListBox()
{
this.listBox1.DataSource = null;
this.listBox1.DataSource = MyDataSource.m_ArrayList;
}

private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
}

4 Form2
private void button1_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(this.textBox1.Text))
{
MyDataSource.Add(this.textBox1.Text);
this.textBox1.Text = " ";
this.textBox1.Focus();
}
}