Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 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 October 16th, 2007, 10:10 AM
Authorized User
Join Date: Oct 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting all listbox items


I have two forms. Both forms have a listbox. When form2 is opened by clicking a button I want the same items that are shown in the listbox on form1 to show in the listbox in form2. I know that to add a selected item from the listbox on form1 the following is used in the class of form1:

public string Items
return listBox2.SelectedItem.ToString();

In the form2 load event the following code is used:

public Form1 mainForm;

textBox1.Text = mainForm.Items;

But How do I get ALL items from the listbox to appear in the listbox in form2?

Old December 1st, 2009, 05:25 AM
Friend of Wrox
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Default answer

because in C# you can't access to other Forms' Controls, but we know that static Members are accessible throw other classes. so you should define a static variable in first form to store items in it like this:

public static List<string> myList = new List<string>();
and somewhere else you should store data of listbox into this variable:

for(int i = 0; i < ListBox1.Items.Count; i++)
and in second form you can access this variable

for(int i = 0; i < Form1.myList.Length; i++)
Old December 1st, 2009, 05:36 AM
samjudson's Avatar
Friend of Wrox
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts

Your replying to 2 year old posts again...

Oh, and by default you can't access another forms controls, but you can change that easily by editing the Modifiers property and setting it to public.

Your solution of using a static variable would not work if there were more than one copy of the first form as well.
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?

Similar Threads
Thread Thread Starter Forum Replies Last Post
unselecting items in a listbox Durkee VB.NET 2002/2003 Basics 9 October 8th, 2007 03:01 PM
Moving Items up and down in a listbox Brendan Bartley Access 15 September 18th, 2007 06:42 AM
UNSelect Items in ListBox mmcdonal Access VBA 7 June 21st, 2007 08:09 AM
Unhighlight Listbox Items selevanm Access VBA 1 March 7th, 2007 01:39 PM
Saving Listbox Items antonducabre VB.NET 2002/2003 Basics 1 July 16th, 2003 12:46 PM

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