Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 16th, 2007, 10:10 AM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting all listbox items

Hi

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
{
get
{
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?

Lucy
  #2 (permalink)  
Old December 1st, 2009, 05:25 AM
Friend of Wrox
Points: 742, Level: 10
Points: 742, Level: 10 Points: 742, Level: 10 Points: 742, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2009
Location: Tehran, Iran
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:

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

Code:
for(int i = 0; i < ListBox1.Items.Count; i++)
   myList.Add(ListBox1.Items[i].ToString());
and in second form you can access this variable

Code:
for(int i = 0; i < Form1.myList.Length; i++)
   ListBox2.Items.Add(Form1.myList[i]);
  #3 (permalink)  
Old December 1st, 2009, 05:36 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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?
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:32 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.