1

I want to add some lines into ListBox, and I have few classes. I try to make my ListBox public in properties, and add some lines in my class, Form1 form = new Form1(); and then form.listBox3.Items.Add("ex");.

My program doesn't show any problems. What should I do next?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zee
  • 415
  • 1
  • 7
  • 13
  • Did you [refresh](http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.refreshitems.aspx) the items? Also see [How do I make a ListBox refresh its item text?](http://stackoverflow.com/questions/61421/how-do-i-make-a-listbox-refresh-its-item-text) – oleksii Dec 27 '11 at 23:35
  • 2
    It is only simple when you understand OOP and have never used vb.net before. Are you comfortable about creating a *new* instance of Form1 and add items to its listbox? Then also add `form.Show();` to see them. Next stop is your favorite C# language programming book and review the chapter about constructors that can take arguments. – Hans Passant Dec 27 '11 at 23:39
  • 1
    @downvoters, dont be harsh on newbies. I see that this kinda questions which stems from lack of idea on OOP are the most common for starters.. I myself was a culprit too those days:) – nawfal Dec 27 '11 at 23:49
  • Can you give us a bit more information. Where are the items coming from that you are trying to add? Why is one form trying to add items to a listbox of another form? – Jason Down Dec 28 '11 at 00:43

3 Answers3

1

Create a public method on the form which accepts the item to place into the listbox which then does the dirty work of actually adding the item to the listbox.

Don't expose internals to other classes; it's not good object oriented form.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
1

Just pass the form (which contains listbox3) to the form from where you are adding to listbox3.

    Form1 f = new Form1(this);
    f.Show();

And from Form1

Constructor:

    MyListBoxForm _f;
    public Form1(MyListBoxForm f)
    {
        InitializeComponent();
        _f = f;
    }

And your function:

   void Add()
   {
      _f.listBox3.Items.Add("ex");
   }

Edit: Do set listbox3 internal or public. An alternative could be to create public method in your listbox class.. Depends on your need actually. If your requirement is just to add a few things to your listbox and that always will be, go for method approach..

nawfal
  • 70,104
  • 56
  • 326
  • 368
  • Not a good way of doing it: why should he make his program dependant on the implementation of his `Form`? – Adam Dec 27 '11 at 23:50
  • 1
    @nawfal I would replace the naming you have there for readability. `_f = _frm1Handle` and `f = Form1Handle`. And for this code to work he needs to change the Modifiers to the listBox3 to public. – Fernando Silva Dec 27 '11 at 23:54
  • 1
    @codesparkle I have done this in one of my apps, though im at an early stage learning process. Could you elaborate on why its a bad idea? I was going to suggest the same thing, but your comment caught my attention. – Fernando Silva Dec 27 '11 at 23:56
  • I would say in those situations where a few more of GUI tweaking has to be done on MyListBoxForm. This is more a general solution which is scalable, and OOP like. I would not want to create public methods on form for operations like this, considering there happens to be not just one listbox that needed to be populated from other forms. He can always have multiple constructors to tackle your problem. – nawfal Dec 27 '11 at 23:57
  • 3
    no, it's neither scalable nor "OOP-like". Your code violates the "Ask, don't Tell" principle because `Form1` now knows that the other Form contains a `ListBox`. It depends on the existance of `listBox3` and will not work with any other `Form`. @Fernando this approach is problematic because it makes different parts of the system depend on the *implementation* of `MyListBoxForm`, making it hard to change that implementation in the future. – Adam Dec 28 '11 at 00:13
  • @codesparkle I get that. The point here is, it actually depends on his actual requirement. I would say the above approach has its place but yes, I shouldnt have foreseen that from the simple question posted. Method approach could be easily assumed as one which would solve OP's requirement. – nawfal Dec 28 '11 at 00:23
  • @codesparkle I understand what you'r saying, but how would you get access to another form's content without going trough this process? Im asking because i really dont know. I had that issue some time ago and this was the only solution i found (im still new to programming). If you think its worthy of a question i'll open it, just let me know^^ – Fernando Silva Dec 28 '11 at 00:35
  • @FernandoSilva just use public a method to do the process in your list box form and call it from the desired form. Hope its clear.. – nawfal Dec 28 '11 at 00:39
  • @nawfal So in form1 class you would have a method `puclic void UpdateListBox() {this.listBox3.Items.Add("ex");}` But how can you execute this method on form2? The only way i see this happening is if form2 returns a value or values that will then be used as paramaters in the `UpdateListBox(...)` method. Just like what happens with a Dialog... I say this because i dont think i can do something like `form1.UpdateListBox();` inside form2 class. I think i'll try to put together a proper question about this so we can get this cleared up for me. – Fernando Silva Dec 28 '11 at 00:48
  • 1
    @Fernando the OP doesn't mention *from which* class he's wanting to add the elements. One approach is to create an **event** in the class that knows the information which needs to be shown. The form can then be register one of its methods as an **event handler**. Once the information becomes available, the class notifies the Form by calling that handler with an argument containing the displayable information. The beauty of this approach is that the class is **decoupled** (not dependent on) the implementation of `Form`. The MVC and MVVM design patterns are other robust ways of doing it. – Adam Dec 28 '11 at 00:58
  • 1
    @fernando maybe for a conceptual question like this one http://programmers.stackexchange.com would be better suited? – Adam Dec 28 '11 at 00:59
  • @FernandoSilva Yes even in that scenario you got to pass the listbox form to form1. Its a hack. – nawfal Dec 28 '11 at 01:12
  • @codesparkle Take a peek and run your mind at me, please!! http://programmers.stackexchange.com/q/127251/43722 – Fernando Silva Dec 28 '11 at 02:50
  • @Fernando Silva I'm naive enough to answer that question, but I wonder why would that question be on programmers. It has to be on SO – nawfal Dec 28 '11 at 10:26
0

In the form designer, change the Modifiers property under Design to Public.

EDIT: i didnt say to change the code, but in the Visual Studio Designer. (PropertyGrid)

Lennart
  • 345
  • 4
  • 17
  • You should NEVER change the designer code behind. This is bad practice - especially if someone is coming behind you to debug / maintain your code. – tsells Dec 28 '11 at 03:36
  • He can change it on the properties pane of his design view, he doesn't need to get into the partial class, although it does get changed. Are you suggesting changing the property in the "code" part of the partial class, so that others can see what was changed? – Fernando Silva Dec 28 '11 at 11:12
  • Thanks for the clarification. I changed the vote. However - I disagree with changing them to public. These controls are part of this form and should not be accessed by another class / form directly. – tsells Dec 28 '11 at 14:37