• 0

[C#]Accessing Object in another form


Question

Hi

I have a Main form (form1.cs) and when I click a label on that form a method should be called.

this works and currently displays a MessageBox. However, if there is too much data a message box is no use as it is not scrollable.

So I thought I would create another form when i click the label the same method gets called but instead o

Form child = new Form2();

child.visable = true;

this shows the form as expected

I now want to add a listbox (to use to display the output as this is scrollable)

However I don?t know how to reference that listbox from the main form

I get an error stating

  Quote
The name 'listbox1' does not exist in the current context

How can I make this work?

Thanks

Link to comment
https://www.neowin.net/forum/topic/561097-caccessing-object-in-another-form/
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Could you not declare Form2 to take a string in its constructor, which internally populates the listbox:

public class Form2 : Form
{
		public Form2(string Message)
		{
			   listbox1.Items.Add(Message);
		}
}

Then you call the above as follows:

string displayText = "Text to display";
Form2 child = new Form2(displayText);
child.Show();

You should see your message in the list box.

--- OR ---

You could have a public property on Form2 which returns the listbox:

public class Form2 : Form
{
		 public Form2()
		 {
				mListBox1 = new ListBox();
		 }

		 public ListBox ListBox
		{
				 get { return mListBox1; }
		}

		 private ListBox mListBox1;
}

Then on Form1 (your main form), you can access it as follows:

Form2 child = new Form2();
child.ListBox.Items.Add("Stuffdd");
child.ListBox.Items.Add("Moref To Add");
child.Show();

Hope this helps :)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • You can do that right now, so what stops you? Nobody forbids you from installing Windows 10 on your PC.
    • Users: "Bring back the adjustable Taskbar!" Microsoft: "Here's an adjustable Volume Indicator!"
    • The flyouts appear in the top left in Windows 8 as well.
    • Supposed Samsung Galaxy Z Fold7 gets unpacked before July in a video by Sagar Naresh Bhavsar We are just a few days away from the official launch of Samsung's premium foldables: the Galaxy Z Fold7 and Galaxy Z Flip7. The second Unpacked event is expected to take place in New York, marking Samsung's return to the city after two years. Samsung has already shared two teasers with the Galaxy Z Fold7 in focus, suggesting that this year's Galaxy Z Fold7 will be taller and wider compared to last year's Galaxy Z Fold6. The teaser also highlights the thinness of the upcoming Z Fold7, claiming that it will be the thinnest and lightest foldable from Samsung ever. Samsung's claims appear to be true, as, way ahead of its official unveiling, a Korean YouTuber, TheSINZA, has shared a video comparing the supposed Galaxy Z Fold7 with the standard Galaxy Z Fold6 and the Galaxy Z Fold6 Special Edition, which launched in China and Korea. The dummy unit of the Galaxy Z Fold7, when compared with last year's foldables, reveals just how thin and lightweight the upcoming foldable could be. The video suggests that the Z Fold7 will weigh 215 grams, much lighter than the Galaxy Z Fold6 Special Edition, which weighed 236 grams. image by TheSINZA via YouTube When the Galaxy Z Fold7 dummy unit is put side-by-side against the Z Fold6 Special Edition, it appears to be noticeably thinner, measuring 9mm compared to the 10.6 mm thickness of the Galaxy Z Fold6 SE. From the front, the Galaxy Z Fold7 looks similar to the Galaxy Z Fold6 Special Edition, but different than the Galaxy Z Fold6. image by TheSINZA via YouTube Though the volume rockers and power buttons appear thin in the Galaxy Z Fold7 dummy unit, they are located in the same place as previous foldables. The dummy unit is also compared with the Galaxy S25 Ultra, and in terms of thickness, it is almost identical. This would mean that the foldable won't feel different compared to Samsung's premium slab phone. Samsung could unveil its foldables on July 9. There are also rumors that an affordable Galaxy Z Flip7 FE could also debut during the event.
    • Thought so, then why would they replace the S+ series with this
  • Recent Achievements

    • First Post
      emptyother earned a badge
      First Post
    • Week One Done
      Crunchy6 earned a badge
      Week One Done
    • One Month Later
      KynanSEIT earned a badge
      One Month Later
    • One Month Later
      gowtham07 earned a badge
      One Month Later
    • Collaborator
      lethalman went up a rank
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      660
    2. 2
      ATLien_0
      270
    3. 3
      Michael Scrip
      218
    4. 4
      Steven P.
      161
    5. 5
      +FloatingFatMan
      157
  • Tell a friend

    Love Neowin? Tell a friend!