LCKB
c# listbox problem - Printable Version

+- LCKB (https://lckb.dev/forum)
+-- Forum: ** OLD LCKB DATABASE ** (https://lckb.dev/forum/forumdisplay.php?fid=109)
+--- Forum: Programmers Gateway (https://lckb.dev/forum/forumdisplay.php?fid=196)
+---- Forum: Coders Talk (https://lckb.dev/forum/forumdisplay.php?fid=192)
+---- Thread: c# listbox problem (/showthread.php?tid=3830)



- Nikolee - 07-12-2014


Hey dudes,

i got a problem with ma Listbox.

Im trying to acess the listbox from a nonstatic class, but i always get the error that something is null (object reference).

The Text cant be null cuz i set the string manually but seems like the Listbox is null i tried many diffrent ways but cant handle it.

 

Here some snippets;

/*--just for test--*/
                MainForm frm = new MainForm();
                frm.listBox1.Items.Add("xy");
                /* throws error */
 
                /*-- example --*/
                frm.listBox1 = new System.Windows.Forms.ListBox();
                frm.listBox1.Items.Add("TEST");
                /* Dont throws an error but text still empty */
I also tried to call a method from the mainform to fill it still didnt worked.




- Nikolee - 07-14-2014


Solved solution for someone who may need it:

First mistake:

Create a constructor in your Class:

      private MainForm nMainForm;
 
  public myClassName(MainForm mainForm)
        {
            nMainForm = mainForm;
        }

/*Call it like this*/

public void urFunc()
{
nMainForm.listBox1.Items.Add("test");
}
 

But theres another thing u need to set up a constructor in the mainform for your Class:

 

 

 public urClassName myClass;
 
 //now initialize it in the mainform
 public MainForm()
 {
       myClass = new urClassName(this);
 }