7 Replies - 5827 Views - Last Post: 05 July 2011 - 01:20 PM Rate Topic: -----

#1 stephen6ipa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-July 11

Update Form1.ListBox from Form2 (button press).

Posted 05 July 2011 - 11:58 AM

I just want to add an item to a listbox from either Form1 or Form2.

I have two forms in C# express; Form1 and Form2.
Form1 contains a listbox and a button.
Form2 just contains a button.

when I press the button in Form1 the following code works:
{
NamesListBox.Items.Add("Hello Form1");
}
And my Listbox gets updated with "Hello Form1". No errors.


However, in Form2 I just have a button with the code:
{
Form1.NamesListBox.Items.Add("Hello Form2");
}

But that doesn't work. I get:

Error 1 An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.NamesListBox'

Error 2 'WindowsFormsApplication1.Form1.NamesListBox' is inaccessible due to its protection level

To me, intuitively, it should work. It works well in Form1. I don't understand why it has to be so diffucult to get the same easily-accomplished task to work from another Form?? Admididly, I don't really understand that much about protection levels but I don't think Form2 is really a threat to Form1. Form2 is a nice, simple, easy Form - it won't hurt anybody!

Am I missing one line of code or a butt-load of code to make it work?

Thanks,

Stephen

Is This A Good Question/Topic? 0
  • +

Replies To: Update Form1.ListBox from Form2 (button press).

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9088
  • View blog
  • Posts: 34,139
  • Joined: 12-June 08

Re: Update Form1.ListBox from Form2 (button press).

Posted 05 July 2011 - 12:05 PM

It doesn't work because of how the object is declared in the designer file. Since .NET works on the object orientated principles objects, functions, and controls all have a scope limit. Public, private, protected, etc.

Either you can dig into your designer and change it by hand or in the form where the controls are created create a public property that gets/sets the listbox.

http://msdn.microsof...y/ms973875.aspx
http://msdn.microsof...y/zsc61976.aspx

For me it's intuitive to have nothing accessible to anyone unless I specify.



Quote

I don't think Form2 is really a threat to Form1. Form2 is a nice, simple, easy Form - it won't hurt anybody!

It's the innocent looking ones that you have to worry about!

This post has been edited by modi123_1: 05 July 2011 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#3 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Update Form1.ListBox from Form2 (button press).

Posted 05 July 2011 - 12:08 PM

Form2 would hurt if it worked that way because everything would be static and that would ruin the world.

So, first of all, you need a reference to Form1 on Form2. Just typing Form1.XXX isn't enough because Form1 is a class, not an instance of the class. This will fix the first error.

The second error can be fixed by setting the modifier of listbox to Public (now it's probably private or protected). I don't remember the property name in property toolbox in visual studio and don't have visual studio at the moment to check it. But search for a property which can be set to "public"

sample code of form 2:
public class Form2  : Form {
  private Form1 frm1reference;

  public Form2(Form1 frm1) {
    frm1reference = frm1;
  }

  public void button_click () {
    frm1reference.mylistbox.items.add(new thingy());
  }
}

// and to call it on form1:
public void button_frm1_click() {
  Form2 frm2 = new Form2(this);
  frm2.Show();
}


Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Update Form1.ListBox from Form2 (button press).

Posted 05 July 2011 - 12:50 PM

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Bulding an application - Part 1
Building an application - Part 2
Quick and easy custom events
Passing values between forms/classes
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4464
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Update Form1.ListBox from Form2 (button press).

Posted 05 July 2011 - 12:51 PM

Quote

Either you can dig into your designer and change it by hand or in the form where the controls are created create a public property that gets/sets the listbox.


In case the OP didn't realize, the first option is a really, really bad one. First, because it breaks encapsulation principles, and second, because any changes you make to the designer will cause the file to be re-written and your changes nuked.
Was This Post Helpful? 1
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Update Form1.ListBox from Form2 (button press).

Posted 05 July 2011 - 12:52 PM

Also, please take the extra 5 seconds to use code tags. It really helps us a lot to see the code properly formatted. It's posted all over including the ghosted text in the posting box.

:code:
Was This Post Helpful? 0
  • +
  • -

#7 stephen6ipa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-July 11

Re: Update Form1.ListBox from Form2 (button press).

Posted 05 July 2011 - 01:06 PM

Thanks Modi and Janne for the info and for having a sense of humor. I changed the private listbox to public in Form1.Designer.cs and the Protection Error went away. I think that was what you were refering to.

Now I am getting:

Error 1 An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.NamesListBox'

My guess here is that I have to declare the NamesListBox (listbox) somehow/where in Form2 and/or to let it know to look in Form1??? OR Let Form1 know that Form2 to is going to be knocking on the door? I don't know how or where to reference it.


I don't get it!




I understand that for you, it's intuitive to have nothing accessible to anyone unless I specify. I am not an open-boarders type of guy either. But when your learning, less constraints on in issue makes more sense to me. Like learning how to walk - If someone told me that I had to have shoes before I could attempt to learn to walk - I would still be crawling.

Anyways thanks again. Maybe there is an easier IDE for beginners other than MS Visual C# that you can recommend. This is requiring too much knowledge just to get, what I think is pretty basic application, to work.

I had a degree in programming before Bill Gates could spell windows and a mouse was just a mouse, and Boston had the #1 selling debut album.

Thanks to you all for your time.

Stephen
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Update Form1.ListBox from Form2 (button press).

Posted 05 July 2011 - 01:20 PM

Stephen: Take Curtis' advice and stop trying to directly access anything in one form from another. Its evil, bad, horrible and hard to maintain in the long run. Its a habit you do not want to get in to and a coding practice you should never see in real world production code. Anyone interviewing you for a job that sees that on a test is going to hold it against you.

Work through the tutorials I linked you. You want for classes (and that includes forms) to be little black boxes that do NOT know anything about the inner workings of other classes. They do their own job and do it very well, but it is not the job of Form2 to directly affect the GUI of Form1. If you are going to tightly bind them like that then there is no point in having two forms. You might as well just have one form with a tab control.

Don't try to micromanage your objects. Let them manage themselves. The analogy I use is making a 911 call. You don't call 911 and say "There is a fire at xxx. Send 2 pumper trucks, 1 supervisor, 37 firemen, 1 ambulance, 2 investigators, 15 oxygen bottles, 7 long hoses, 9 medium hoses and 4 squad cars to cordon off the area." You call up (raise an event) and tell them about a fire; provide 2 parameters of address and object on fire (house, car warehouse) and let the receiving object (the fire department) decide what the appropriate response is.

This post has been edited by tlhIn`toq: 05 July 2011 - 01:25 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1