使用现有第一种形式的弹出窗体中的函数 [英] Using function from popup form in existing first form

查看:133
本文介绍了使用现有第一种形式的弹出窗体中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何正确地做到这一点。

基本的是,我有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 of Overview with reference of Form1 as parameter

public 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 of Form1 instance inside your Overview form

When 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屋!

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