Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

instance of form does not pass values to another form 1

Status
Not open for further replies.

nishasp

Programmer
May 13, 2004
33
0
0
GB
I have created on instance of form2 in form1 and need to pass a string value across. However my object does not hold any values of the variables from form2 and shows these as being 0. I have declared the instance of form2 within the Members region of my code and assigned a string to the object that i wish to access. This returns a null value for the string. I am sure that its something silly that i have missed out...please help!!

Thanks in advance...
 
- is the JIGString array declared globally in Form1?

- is the JIGString array initialised somewhere in the constructor of form1, or where it is globally declared? (e.g. ArrayList JIGString = new ArrayList())

to access the elements as strings you do JIGString[0].ToString() or (string)JIGString[0]
 
I have declared the array globally and also tried initialising it in the constructor as JIGString = new ArrayList();

Neither one solves the error regarding the object reference. Does that mean there is something wrong with the way the properly is written, or the way i have declared something?

Just to be sure, i have declared a constructor in form2 as:
public DisplayScreen(SerialJIG parentForm)
{
this.parentForm = parentForm;
}

 
do you create Form2 like this:
Code:
Form2 frm2 = new Form2(((Button)sender).FindForm());
frm2.ShowDialog();
? (assuming this is done in an on click procedure of the buttons you have) ?
 
I have a general click handler for the buttons in form1, but this is written in the class where i have created the ButtonArray.

JIGButtonArray = new ButtonArray(btnTransmit.FindForm());
This line is declared in the Constructor of form1 where btnTransmit is a button on the form. A button on form2 is meant to set the property of resultString and then the button on form1 is meant to put the resultString value in the array.

if this is not correct, where should i put the lines of code that you just wrote?
 
I have a general click handler for the buttons in form1, but this is written in the class where i have created the ButtonArray.

in this general click event handler you should use the lines I just wrote. I don't understand why you call the constructor of the button array with a parameter... you probably don't need it. if you still can't figure this out, mail me the code (all of it) at alex@vrsoftware.ro
 
It says in a microsoft article that the following error:
An unhandled exception of type 'System.NullReferenceException' occurred in <project name>.exe

Additional information: Object reference not set to an instance of an object.

is sometimes caused because the Set method of a control expects a Reference type instead of a Value type. How would i do this, if its the right thing to do?!


Also, this line
Form2 frm2 = new Form2(((Button)sender).FindForm());

will not work as the constructor for form2 takes SerialJIG parentForm as its parameter.


 
you just need to cast it like:
Code:
Form2 frm2 = new Form2(((SerialJIG)((Button)sender).FindForm()));
 
I think i got it figured, but now i have another problem
:-(

This is what i did:
parentForm = new SerialJIG(); //newline
parentForm.resultString = StartRemoteTest.ToString();

I must not have declared it as a new instance of the form.

The problem now however is that i can't still look at form1, even though it is there on the taskbar. I need to be able to look at form1 as, depending on what string comes back, the buttons change colour. Any ideas?
 
this is definitelly not the approach (creating a new instance I mean)... you have to see where the problem is when passing that argument to Form2 contructor! that's the whole key to it.
 
If you don't mind, I'm going to send you a copy of my code to have a look at...i am totally confused now!
 
I think I found your problem...

uncomment the line that passes as parameter to you DisplayScreen the SerialJIG form and modify the constructor of the display screen to this:
Code:
		public DisplayScreen(SerialJIG parentForm) : this()
		{
			this.parentForm = parentForm;
		}
 
Do you mean i should uncomment this line:
parentForm = new SerialJIG(); ??

This passes the value to form1, but it sets the count to 0 and this string becomes the first element in the arraylist. I don't think that it is reading the other elements that i have added to the list. I need these to be continually polling.

Also, my line
SendStr(JIGString[iJIGMsgString].ToString());
causes an error. I thought that using ToString would get the string value of the array element??
 
i'll give you the code for the button click...
Code:
		public void ClickHandler(Object sender, System.EventArgs e)
		{
			DisplayScreen oScreens = new DisplayScreen(((SerialJIG)((System.Windows.Forms.Button)sender).FindForm()));
			DisplayScreen oDisplayScreen = new DisplayScreen();
			sTag = ((System.Windows.Forms.Button) sender).Tag.ToString();
			//Assigns the jig number to a textbox on the form
			sJIGNumberLabel = "JIG" + " " + ((System.Windows.Forms.Button) sender).Tag.ToString();
			oScreens.JIGLabel = sJIGNumberLabel;

			//Assigns the jig state to a textbox on the form
			sJIGState = "IDLE";
			oScreens.JIGState = sJIGState;

			//Assigns the jig number to the name of the form
			oScreens.Text = "JIG" + " " + ((System.Windows.Forms.Button) sender).Tag.ToString();
			oScreens.Tag = sTag;
			
			//Calls the DisplayScreen to show
			oScreens.Show();
                  }
 
Thank you for all your help. I would have been stuck on this for days otherwise!
:-D

 
no problem... i'm glad we got to the bottom of this
 
Nice work DaZZleD! - You deserve a couple of stars for that!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
One more question regarding this topic...

Because i have multiple instances of the same form, if i pass a value from instance1 of form2 to form1, and then do not close that screen, then the same value gets passed across when i try and send a different value from instance2 of form2. Is there anything i can do so that even if more than one form of form2 is open it will send the value of the form that i want??

I hope this makes sense...
 
are you using the array to store the values? if so, I think you might be looking at the wrong index, because there should be no reason for this to happen. otherwise make sure you don't get the same values from both forms...
 
I store the add the value to an arraylist, but this value gets removed. Does this not clear the element?

If i close one form and then send the value from another form, there is no problem. However if i send a value from the first form and do not close it, then when i send a value from the second form, even though the value is held in the variable, it just assigns the value of the first form to the property in form1 and adds it to the last element of the array again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top