473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Set items of listbox from another form or class

384 256MB
I'm looking to add items on a listbox on form A from class B which is called from form A. I just don't know how to say add them to the listbox on the active form A from the class B!?
It's in C#, can anyone help?
Sep 8 '08 #1
3 1919
joedeene
583 512MB
I'm looking to add items on a listbox on form A from class B which is called from form A. I just don't know how to say add them to the listbox on the active form A from the class B!?
It's in C#, can anyone help?
how about referencing the other class/form to a sub that does it, (for vb)

*this is a sample in VB, perhaps you could recode it into C#, im not real experienced in that particular area.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.  
  4.     Public Shared Sub additemtolistbox(ByVal itemtoadd As String)
  5.         Form1.ListBox1.Items.Add(itemtoadd)
  6.     End Sub
  7.  
  8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  9.         myotherclass.subfromotherclasstoadditems()
  10.     End Sub
  11. End Class
  12.  
  13. Public Class myotherclass
  14.  
  15.     Public Shared Sub subfromotherclasstoadditems()
  16.         Form1.additemtolistbox("test")
  17.     End Sub
  18. End Class

joedeene
Sep 9 '08 #2
ziycon
384 256MB
Got it working like this, have an if statement working off a int that counted how many records where in the table and shows all the elements of the returned array that have values. Works perfect.

Expand|Select|Wrap|Line Numbers
  1. public string[] getList()
  2.        {
  3.            DBConnect();
  4.  
  5.            string[] list = new string[100];
  6.            int i = 0;
  7.  
  8.            QueryString = "SELECT title FROM listdat ORDER BY title ASC";
  9.            command.CommandText = QueryString;
  10.  
  11.            Reader = command.ExecuteReader();
  12.            while (Reader.Read())
  13.            {
  14.                list[i] = Reader.GetValue(0).ToString();
  15.                i++;
  16.            }
  17.  
  18.            DBDisconnect();
  19.  
  20.            return list;
  21.        }
Sep 9 '08 #3
balabaster
797 Expert 512MB
Hmm, it really depends on the structure of your class... is your listbox going to display each instance of Class B? Or is Class B Implementing a List? Or is Class B an instance of a class that has a list containing the items you wish to populate your listbox with?
Sep 9 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: John R. | last post by:
How do I create an item for a listbox? I want to do something like this EXCEPT for a listbox: ListViewItem item = new ListViewItem(); item.Text = column.Name; int height = item.Bounds.Height;...
9
by: Alpha | last post by:
Hi, How can I set all the items in a listbox to be selected? I can't find a property or mehtod to do it so I thought I'll try using setselected method but I need to find out how many items are in...
5
by: Kay | last post by:
Hello, I have two list boxes on my form, one initially displays with items and the other displays blank, by clicking a button, it is possible to move items from one box to another and vice a...
3
by: Giovanni Bassi | last post by:
Hello All, I have a class implementing IList and a global object of this class type. I have two different form objects. One has a ListBox and the other has ComboBox. Both of their datasources...
2
by: Joe Fallon | last post by:
I have 2 listboxes on a Web Form. As I move an item from 1 to the other it shows up at the end of the list. How can I sort the list that just got the new item added to it so it is in alphabetical...
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
2
by: koosh34 | last post by:
VS2005 VB.net 2.0 I have a form called options with a ListBox1 on it, to fill the list box from several events on the form I put the code to update the ListBox1 items into a sub in a separate code...
4
by: bogdan | last post by:
Hi, I have two listboxes on a page and need to move items between them - using buttons (e.g. "<<" ">>"). Can this be done on a client side in asp.net? I'd like to avoid hitting the server on...
5
by: BlackBox | last post by:
I have a Listbox1 in the main form and I can add listbox1 items in the parent form. But when I close the parent form Listbox1 in the main form does not get refresh. I need to re-open the app. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.