Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 7th, 2006, 03:20 AM
Authorized User
Join Date: Jun 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default calling control in one form from another form

hi all,

i have two forms in a C#.net windows application

i have a list view control in form1.

i want to fill this list view from form2 and display form1 from form2.

any idea of doing this?
thanks in advance
Old November 7th, 2006, 06:20 PM
Friend of Wrox
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts

It is almost always a bad idea to manipulate a control on one form from another form.

However, if you want to do this directly, you merely need to change the access modifier in
the declaration for the listview in form2 from private to public.

Then you can reference the listview from the first form where you instantiate and display the form. Still, I want to make it very clear that this is commonly regarded as a very bad programming practice.

A better solution is to let the second form populate its own listview. There are numerous ways to do this, but one common way is to provide a method on the second form that is used to pass the list that you want displayed (by list here, I mean the array, dataset, or whatever you are going to use to populate the listview). For a dialog form, you would control all the behavior of populating the listview, showing the form, and then finally returning any required values to the first form as needed. There are other ways as well.

To go a step further, however, it would be worthwhile looking into a model/view/controller or other more decoupled architecture to investigate some better solutions.

Woody Z http://www.learntoprogramnow.com
Old November 8th, 2006, 07:35 AM
Authorized User
Join Date: Jun 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts

thanks a lot woodyz.i have found a solution for that problem.thanks again
Old November 8th, 2006, 10:05 AM
Authorized User
Join Date: Feb 2006
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to MAKO

Hi Netfresher, I've had the same problem with a windows form project. Could you be so kind to help me find the solution as you did? Thanks.

MAKO - "El super simio"

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the current calling form in a class? littlekitty ASP.NET 2.0 Basics 0 October 17th, 2006 08:28 PM
calling functions form another window geogomez Javascript How-To 2 November 29th, 2005 07:20 AM
Calling a procedure embeded in another form lryckman Access VBA 6 April 29th, 2005 08:48 AM
Addressing a control on a form from another form Lamprey Access 1 March 3rd, 2005 12:05 AM
calling a form melvik C# 4 July 23rd, 2003 05:24 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.