如何将控制权返回到启动表单 [英] how to go control back to StartUp Form

查看:67
本文介绍了如何将控制权返回到启动表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有两种形式(Form1和Form2).
Form1包含DataGridView和添加"按钮.
运行表单时,第一个Form1将打开并保持原样,然后在我单击Add按钮时打开Form2.

我的问题是关闭Form2后,Form1应该保持原样并自行更新.

请提供示例或示例代码.

Hi all,

I''ve two forms (Form1 and Form2).
Form1 contains DataGridView and Add button.
While running the form, 1st Form1 will be open, retaining as it is, then Form2 will be opening as I clicked Add button.

my question is After closing Form2,Form1 should get stay as it is and updated itself.

Please reply with an example or sample code.

推荐答案

我认为您正在使用Form.Show?
这会将其视为新形式.

尝试改用Form.ShowDialog().

通过访问先前的Form属性,这将从原始Form中获取数据.

希望对您有所帮助.
I presume you are using Form.Show?
This will treat it as a new form.

Try using Form.ShowDialog() instead.

This will get the Data from the original Form by accessing the previous Form properties.

Hope that helps.


您可以将form1的对象发送到form2,并添加一个带有form2的参数对象的构造函数,如下所示:

在form1中,当您调用form2时:

you can send your object of form1 to form2 and add a constructor with a parameter object of form2 like this :

In form1 when you are calling form2 :

Form2 frm2 = new Form2(this);



在form2中,您可以创建一个像这样的构造函数:



In form2 you can create a constructor like this :

public static Form1 Frm1 = new Form1();

public Form2(Form1 form)
{
     Frm1 = form
}



现在,将form1的修改器设置为public后,您就可以访问它的任何控件.



now you can access any control of form1 after setting its modifier to public.


Ravi,
首先,您必须在form2中创建一个重载的构造函数,并在form1中创建一个全局对象,您已经存在的默认构造函数为:

Hi Ravi ,
first you must create an overloaded constructor in form2 and a global object of form1 , your already existing default constructor is :

public Form2()
{
     InitializeComponent();
}



全局对象和重载的构造函数代码将是:



The global object and the overloaded constructor code will be :

public static Form1 Frm1 = new Form1();

public Form2(Form1 form)
{
     InitializeComponent();
     Frm1 = form;
}



现在,您可以在form1的修饰符中将datagridview设置为public.

然后在点击添加按钮事件中,您将插入以下代码以打开form2:



Now you can set your datagridview in form1 ''s modifier to public .

Then in the click add button event you will insert this code for opening form2 :

Form2 Frm2 =new Form2(this);
Frm2.show();



现在在form2代码中,您可以使用:
更新form1中的datagridview



now in the form2 code you can update your datagridview in form1 using :

Frm1.dataGridView1.Rows[...].Cells[...].Value = "<your desired="" data="">";
</your>



我希望这次很清楚,
祝你好运
:)



I Hope this time it is clear ,
Good luck
:)


这篇关于如何将控制权返回到启动表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆