使用现有第一种形式的弹出窗体中的函数 [英] Using function from popup form in existing first form
问题描述
基本的是,我有1个表单,用于填写各种数据我保存在2个列表中。第二种形式是通过菜单选项调用的,该选项显示所有当前数据的概览。它是这样打开的:
private void OpenOverviewForm()
属性和方法
{
概述formOverview = new Overview( );
for(int i = 0; i< callListNL.Count; i ++)
{
ListViewItem item = new ListViewItem(callListNL [i] .Opco);
item.SubItems.Add(callListNL [i] .UserID);
item.SubItems.Add(callListNL [i] .Email);
item.SubItems.Add(callListNL [i] .Title);
formOverview.listView1Overview.Items.Add(item);
}
for(int i = 0; i< callListPL.Count; i ++)
{
ListViewItem item = new ListViewItem(callListPL [i] .Opco );
item.SubItems.Add(callListPL [i] .UserID);
item.SubItems.Add(callListPL [i] .Email);
item.SubItems.Add(callListPL [i] .Title);
formOverview.listView1Overview.Items.Add(item);
}
formOverview.StartPosition = FormStartPosition.CenterScreen;
formOverview.Show();
$ b $ p
$ b在这个新表单中,有一个列表视图,其中包含所有保存的日志列表。我想要的是最终用户能够双击其中一个条目,然后关闭第二个表单并显示他们在第一个表单上选择的数据。为了做到这一点,我需要能够使用列表视图的doubleclick事件来提供他们已经双击的项目的索引到第一种形式中存在的一个函数。然而,我无法找到一种方法,从我的Form2中识别Form1中的函数。
我知道以下几种可能:
public partial class概述:表单
实例的公共
{
public概述()
{
InitializeComponent();
private void listView1Overview_DoubleClick(object sender,EventArgs e)
{
Form1 newform = new Form1();
newform.OverviewFormDisplay(listView1Overview.SelectedIndices [0]);
但是由于这段代码实际上创建了一个我的原始形式的全新实例,我不认为这是可用的。或者我错了?
解决方案创建主要表单实例变量
并添加概述
引用Form1
作为参数public partial class概述:Form
{
private Form1 _mainForm;
$ b $ public(Form1 mainform):this()
{
this._mainForm = mainForm;
}
private void listView1Overview_DoubleClick(object sender,EventArgs e)
{
this._mainForm.OverviewFormDisplay(listView1Overview.SelectedIndices [0]);
然后您可以使用所有
中的
概述
表单Form1
$ b在创建
Overview >表单的实例时,将主表单的引用传递给构造函数
// .....这行必须在Form1
中概述formOverview = new Overview(this);
// ....
I'm having trouble figuring out how to properly do this.
The basics is that i have 1 form which is used to fill out all kinds of data which i save in 2 lists. The 2nd form is one that is called via a menu option which displays an overview of all the current data. it is opened like so:
private void OpenOverviewForm() { Overview formOverview = new Overview(); for (int i = 0; i < callListNL.Count; i++) { ListViewItem item = new ListViewItem(callListNL[i].Opco); item.SubItems.Add(callListNL[i].UserID); item.SubItems.Add(callListNL[i].Email); item.SubItems.Add(callListNL[i].Title); formOverview.listView1Overview.Items.Add(item); } for (int i = 0; i < callListPL.Count; i++) { ListViewItem item = new ListViewItem(callListPL[i].Opco); item.SubItems.Add(callListPL[i].UserID); item.SubItems.Add(callListPL[i].Email); item.SubItems.Add(callListPL[i].Title); formOverview.listView1Overview.Items.Add(item); } formOverview.StartPosition = FormStartPosition.CenterScreen; formOverview.Show(); }
In this new form, there is a listview with a list of all saved logs. And what i want is for the endusers to be able to doubleclick one of the entries, which will then close the 2nd form and display the data that they've selected on the first form. In order to do that, i need to be able to use the doubleclick event of the listview to provide the index of the item that they've doubleclicked to one of my functions that exist in the first form. However i can't find a way to get my function from Form1 recognised in my Form2.
I know it's possible with the following:
public partial class Overview : Form { public Overview() { InitializeComponent(); } private void listView1Overview_DoubleClick(object sender, EventArgs e) { Form1 newform = new Form1(); newform.OverviewFormDisplay(listView1Overview.SelectedIndices[0]); } }
But as this code actually creates a completely new instance of my original form, i don't think that this is useable. Or am i wrong?
解决方案Create a variable of your main form instance
and add constructor ofOverview
with reference ofForm1
as parameterpublic partial class Overview : Form { private Form1 _mainForm; public Overview(Form1 mainform) : this() { this._mainForm = mainForm; } private void listView1Overview_DoubleClick(object sender, EventArgs e) { this._mainForm.OverviewFormDisplay(listView1Overview.SelectedIndices[0]); } }
Then you can use all
public
properties and methods ofForm1
instance inside yourOverview
formWhen creating instance of
Overview
form, pass reference of main form to constructor//.....this line must be in the Form1 Overview formOverview = new Overview(this); //....
这篇关于使用现有第一种形式的弹出窗体中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!