Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 forms novkontakt and novkontakt1 who should get same value from tipNaLice form,
when is one form kontakt work perfect, but when add secound have error

System.NullReferenceException: 'Object reference not set to an instance of an object.'


What I have tried:

this is my code from tipNaLice
novKontakt orForm;
novKontakt1 or1Form;

public tipNaLice(novKontakt inForm)
{
    orForm = inForm;
    InitializeComponent();
}

public tipNaLice(novKontakt1 in1Form)
{
    or1Form = in1Form;
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
     foreach (object redovi in listBox1.SelectedItems)
     {
        orForm.textLice = redovi.ToString();
        orForm.vmetniTipNaLice();
        or1Form.textLice1 = redovi.ToString();
        or1Form.vmetniTipNaLice1();

        this.Close();
     }

}
Posted
Updated 9-Apr-19 7:27am

Don't do it that way. That means that your forms need to know about each other and how they work - that's bad and makes your code hard to maintain properly.

Exactly how depends on the "relationship" between the two forms.
Have a look at these, one of them will fit your circumstances.
The form that creates an instance of another:
C#
MyForm mf = new MyForm();
mf.Show();
Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
 
Share this answer
 
Comments
Member 11920272 9-Apr-19 13:12pm    
i know this
 tipNaLice frm = new tipNaLice(this);
      frm.Show();


when is one work
private void button1_Click(object sender, EventArgs e)
{
     foreach (object redovi in listBox1.SelectedItems)
     {
        orForm.textLice = redovi.ToString();
        orForm.vmetniTipNaLice();
        
        this.Close();
     }
}


i look your code and don't understant good,
i like more simple solution, i fine one
OriginalGriff 9-Apr-19 14:01pm    
The problem is that it's a bad solution: it means that the common controls have to be aware of each other, and their internal workings. It means that you can't now change either without considering how that might affect the outside world; it means that you can;t make changes to the form containing them without knowing intimate details of how they work. And it means that you can't reuse the controls in other places.

All of those are simple to do "properly" - just look at the code in the links I gave you, it's all pretty simple stuff.
Making controls public is a very bad idea which will come back to bite you later, when it has been used so much that fixing it becomes extremely difficult and tedious.
When novKontakt1 form is open do something (in my case send value from listbox)
so this is my solution
public static Form OpenForm(Type FormType)
        {
            foreach (Form OpenForm in Application.OpenForms)
            {
                if (OpenForm.GetType() == FormType)
                    return OpenForm;
            }

            return null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            novKontakt1 nk1 = null;
            
            foreach (object redovi in listBox1.SelectedItems)
            {
                if ((nk1 = (novKontakt1)OpenForm(typeof(novKontakt1))) == null)
                {
                    orForm.textLice = redovi.ToString();
                    orForm.vmetniTipNaLice();
                }
                else
                {
                    or1Form.textLice1 = redovi.ToString();
                    or1Form.vmetniTipNaLice1();
                }
                this.Close();
            }
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900