在窗体之间将值传递给ListView控件-简单的问题,无需代码 [英] passing values between forms to ListView control - simple question no code needed

查看:64
本文介绍了在窗体之间将值传递给ListView控件-简单的问题,无需代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨ppl

我在尝试理解某些内容时遇到了一个问题.我不需要代码,因为我已经找到解决问题的方法,所以我只是想了解为什么这不起作用.

我将尝试以最简单的方式进行解释.
我有2种表格:
-表格1是我的主要应用菜单表格.
-表格2是我的记录器窗口,其中包含一个大型ListView控件

现在,仅当用户按下特定的工具条按钮时才创建表格2.但是在用户执行此操作之前(实际上这并不重要,因为即使在早期创建表单时它也不起作用),我试图在Form2中向ListView提供一些数据.好吧,以免出现一些代码:

表格1:

Hi ppl

Ive got bit of a question as im trying to understand something. I dont need a code as i already found a walkaround for my problem, so im just trying to uderstand why this does not work.

Im gonna try to explain it in simplest way I can.
I have 2 forms:
- Form 1 is my main app menu form.
- Form 2 is my logger window that holds a big ListView control

Now Form 2 is only created when user presses specific tool strip button. But before user does that (actually that does not matter because it does not work even when form is created earlier) Im trying to feed that ListView in Form2 with some data. Ok so lest go with some code:

Form1:

Form1 form = new Form1();
form.AddToListView(e.Name.ToString(), e.FullPath.ToString(), e.ChangeType.ToString());



表格2:



Form2:

internal void AddToListView(string fileName, string fileSourceLocation, string fileDestination)
{
    ListViewItem item = new ListViewItem();
    item.Text = fileName;
    item.SubItems.Add(fileSourceLocation);
    item.SubItems.Add(fileDestination);
    myListView.Items.Add(item);
}



我调试了整个程序,并将所有数据传递给"myListView.Items.Add(item);".我只是感到惊讶,因为它将所有值传递到了最后一个阶段,但是ListView中什么也没有出现.就像我之前提到的,我做了一些遍历,我写了一个类来为我处理数据交换.但是我试图在这里学习一些东西,所以我想知道为什么我的第一次尝试没有奏效.

比大家提前抽出宝贵的时间.抱歉,如果我把事情复杂化了.
Cheers



I debugged whole thing and it passes all data to "myListView.Items.Add(item);". Im just surprised because it passes all the values to the last stage but nothing appears in my ListView. Like I mentioned earlier i did a little bit of walkaround, I wrote class that handles data exhchanging for me. But Im trying to learn something in here so i would like to know why my first attempt didnt work.

Than you all for Your time in advance. Sorry if I overcomplicated things.
Cheers

推荐答案

这是有关表单协作的流行问题.最健壮的解决方案是在Form类中实现适当的接口,并传递接口引用而不是对Form的整个实例"的引用.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please see all other suggestions. If your application is pretty simple, the solution could be as simple as passing a reference to one form instance to another form and providing some internal (no need for public!) members on that form to be used in another form. In case of more complex projects and several cases of such trivial collaborations, this simple approach could mess up code and invite some bugs, due to lack of more strict encapsulation.

Good luck,
—SA


这篇关于在窗体之间将值传递给ListView控件-简单的问题,无需代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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