Hello all. I am teaching myself C# and I have been fairly dedicated to solving my own problems. However, for some reason I am just stumped.

I have a main form that manages alot of data. To insert data into the form you push a button to bring up a wizard (another form). In that form you enter your data then press submit.

What I want to happen is the data is listed in a list box on the main form. The wizard will stay open after submit is clicked. So I need something that only pulls the information into the main form when the submit button is clicked.

I thought this should be easy. However, referencing the list box in the primary form wants an object reference for the primary form. If I create a new reference, such as: Form1 myform = new Form1(); It doesn't work. Presumably because i am creating a new instance of form1 instead of using the existing form.

What is the simplist way for me to get the existing object reference for the primary form?

Thank you for your help.

Recommended Answers

All 13 Replies

You can either make an event on your main form which you tie into the button on your second form (messier)

or

You can create a method which you send the data from your second form, to your first form. So the first form handles all its updates.

Thank you for the speedy reply.

You can either make an event on your main form which you tie into the button on your second form (messier)

I'd like to explore this, but I don't really know how to go about adding events, much less events that would pull from a different form from where I am setting up the event from.


or

You can create a method which you send the data from your second form, to your first form. So the first form handles all its updates.

I tried this already. The problem that remains, and which I am trying to get around, is the object reference. Simply calling Form1.listbox.add doesn't work because it wants an object reference of Form1. Making a new object reference (as in literally new Form1())clears the error yet the data does not get to Form1 from Form2. If I could use the method approach, how do I do this. Using a method would work better for my purposes, I think.

Thank you for the help again.

have you considered setting a property on form2 to be the value of form1? so you have a reference?

OK, so now Ive eaten my dinner - and I got 30 mins before I go out:

Picture this.

You have a routine on your first form, which you assign to an event on your second form - such as a routine like:

public UpdateText(String msg)
{
  this.textBox1.Text=msg;
}

If your event is programed to receive such a thing, you could easily on creation of your second form do

form2.MyEvent += new MyEvent(UpdateText);

then within form2, on clicking of button you can test

if (MyEvent!=null) { MyEvent("testme"); }

and you just triggered your event..

another way to do it is to have

void DoMyevent(String msg)
{
if (MyEvent!=null) { MyEvent("testme"); }
}

and then when you potentially need to call your event do

doMyEvent("sometext");

and your code is easier to read


Just read up on delegates and events.

Well, I've done my best, as I can work it out, but I still can't get this right. It may be that I am not setting up the event properly or putting the snippets in the right place. One thing, the code:

public UpdateText(String msg)
{
  this.textBox1.Text=msg;
}

was producing an error requesting a return value. This wasn't hanging up anything. My bigger problem I think was: form2.MyEvent += new MyEvent(UpdateText); wherein it gave me an error for the "new MyEvent".

How did you define the MyEvent type?

the UpdateText method looks reasonable, it would seem more likely it didnt get called.

I probably didn't define it properly. However, the more I think about the direction we are taking for this particular wizard it doesn't seem to handle the overal problem I am having, which is communicating between the various forms. The primary form will be calling a bunch of wizards for different tasks. Those wizards will be required to pull data from the main form as well as putting information into the form. The current path seems to be a fix for the imediate problem of getting form2's data onto form1. However, direct communication between the two forms is what is really required.

In short, I need to be able to reference controls in form1 to perform a wider range of options. So directly manipulating form1's controls from other forms is what I am looking for. I can't seem to find any way to do this. Is this even possible?

I have started a new project just to start testing ideas. In that project I have two forms. The first form has a textbox and a button. The button opens form2. Form2 has a text box and a button. I want to enter text into form2, click the button, and the text appears in the textbox on form1.

Normally, if I wanted to add text to form1 it would be as simple as textbox1.Text = "MyText"; . I want to be able to do this almost as directly. So lets say it were possible to be on form2 and have a line like Form1.textbox1.Text = form2textbox.Text; when I click the button on Form2.

Does that make sense?

it will, and it would be the best and proper way to deal with your issue.. As whatever form calls the wizard it could set itself to be a recipient of a heres your data event and work from there, thus removing all your issues of which instance/form etc asked for the wizard.. thats the whole beauty of it.

Here's another example.

When I try what I am saying in the above post it looks like this:

button click on form2

private void button1_Click(object sender, EventArgs e)
        {
            string s = textBox.Text;
            Form1.textBox1.Text = s;
        }

Now there is an error at line 4 that says that an object reference is required for a non-static field.

Okay, now I can clear my error by doing this:

private void button1_Click(object sender, EventArgs e)
        {
            Form1 parent = new Form1();
            string s = textBox.Text;
            parent.textBox1.Text = s;
        }

Now I have created a new instance of Form1. So the error is clear. But of course I get no text in parent.textBox1 because we created a new reference, instead of referencing the existing Form1. I would think that there was something besides "new" on line 3 that I could use to identify Form1, but I can't find what that is. If I could answer that question the problem would be solved for all wizards in the application.

it will, and it would be the best and proper way to deal with your issue.. As whatever form calls the wizard it could set itself to be a recipient of a heres your data event and work from there, thus removing all your issues of which instance/form etc asked for the wizard.. thats the whole beauty of it.

He-he. Your reply beat my followup.

Then I am afraid I may need a more step by step on this solution including calling the event, if you don't mind. It may be that my brain is just missing something very simple, but I can't seem to follow you.

SOLVED!!!!!

Okay, I did some googling and finally found what I was looking for.

Here's the solution (found here)

on Form1's button click we do the following:

private void button1_Click(object sender, EventArgs e)
        {
            Form2 newform = new Form2();
            newform.Frm = this;
            newform.ShowDialog();

        }

On line 4 we are setting the object reference. It is showing an error because we need to set it up in form 2.

In form 2, we have:

namespace TestingCrossForms
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public Form1 Frm;


        private void button1_Click(object sender, EventArgs e)
        {
            string s = textBox.Text;
            Frm.textBox1.Text = s;
        }
    }
}

in line 9 we initiate the object Frm which serves the purpose of providing the breadcrumb back to Form1.

It was so simple. I had thought of this, even tried it, but mucked it up somewhere.

Still, LizR you have been incredible in your patience and help. I know I'm not really using your suggestions, but just the fact that you took your time to help is very kind of you and worth the mention. Thank you so much. At the very least you kept me from giving up on it, and you did inspire me to keep looking at new ideas.

There are issues potentially with the way you chose to do it, but, it does work (and was the ugly method I said earlier) the clean and proper method is better, and allows you to have multiple recipients that may need to know of changes, and is an OO answer to an OO problem

I've encountered a similar challenge, myself, and have seen several "fixes" on the web that were far beyond my understanding.

Here's what I did:
1. declare globally: your second form and the controls it uses, such as a string, text box, etc.
2. Put a button on your first form that creates the second form and text box along with a button to "SAVE" and a button to "QUIT." You must do all the associated housework of sizing them, placing them, etc. Make the second form the Parent of the textbox.
3. In the QUIT button click method, place your code to close the second form.
4. In the SAVE button click method, copy the information from the textbox into the global string. Now you can use the string elsewhere on form1. Lastly, call the QUIT button click method to close the second form.

That's pretty simple but it works.

E Coloney

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.