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...
 
Are you posting or getting the form?

Regards,


Lewis Harvey
lewis@lharvey.com
 
Not quite sure what you mean, but this is my code:

On Form1:
form2 frm2 = new form2();
string sRemoteTest = oStartString.sStartRemoteTest;
string s= frm2.sform2;

On Form2:
public string sStartRemoteTest;
public string sform2;
private void btnStart_Click(object sender,System.EventArgs
e)
{
sform2 = mystringbuilder.ToString();
}

When the btnStart_Click event is evoked, sform2 stores the string built using the stringbuilder class. I then go back to form1 to click another button, where i send strings from an array. The string built in form2 should take the last element of that array. However, it always shows up as null.
 
do you need to put string s = frm2.sform2.value or .text? so its know what to look for.

string s = frm2.sform2.Text; <-Think this is the right one.
string s = frm2.sform2.Value;

Let me know if it works

Regards,


Lewis Harvey
lewis@lharvey.com
 
There is no option to set string s = frm2.sform2.Text;

I am calling the form2.Show() method in another class. Does that make a difference as to whether the values get passed through to form1 or not?

The other thing is that all the variables that i have already set in form2, show correctly in the form2 object when debugging through form1...
 
Are you sure the form2 frm2 = new form2();

actually links to the form. when you initialise the form do you not have to tell it which form it is looking at, not sure if that initialisation technique works.

Regards,


Lewis Harvey
lewis@lharvey.com
 
why don't you just call the constructor of Form2 with Form1 as param and when the string building is completed on form2, it automatically stores the result in a string of form1 like this:

on Form1:
Code:
// declare this variable
public string returnedString;

// same code but with different constructors
Form2 frm2 = new Form2(this, oStartString.sStartRemoteTest);
// and you should open form2 as dialog:
form2.ShowDialog();
// when form2 closes you already have the result stored as returnedString:
string s = this.returnedString;

on Form2:
Code:
private Form parentForm;
private string sRemoteTest;
public void Form2(Form parentForm, string sRemoteTest)
{
    this.parentForm = parentForm;
    this.sRemoteTest = sRemoteTest;
}
.....
private void btnStart_Click(object sender,System.EventArgs
e)
{
     parentForm.returnedString = mystringbuilder.ToString();
}
 
I made a mistake in my code as i was trying to explain it in easier terms. So, oStartString is an instance of form2 and sStartRemoteTest was the string that stored the value of the string.
The code should look like this...apologies for the mistake!
I'm just using what i have actually got written down.

On Form1:
DisplayScreen oStartString = new DisplayScreen();
string sRemoteTest = oStartString.sStartRemoteTest;


On Form2:
public string sStartRemoteTest;
private void btnStart_Click(object sender,System.EventArgs
e)
{
sStartRemoteTest = mystringbuilder.ToString();
}

The problem is that i can't call oStartString.sRemoteTest in creating oStartString. So, what should replace the second parameter when declaring an instance of the form.

Also, form2 (DisplayScreen) is opened in another class, will this affect using the ShowDialog method?

Thanks
 
On Form1:
Code:
// declare this;
public string resultString;

// use this instead of what you wrote
DisplayScreen oStartString = new DisplayScreen(this);
oStartString.ShowDialog();
string sRemoteTest = this.resultString;
// oStartString.sStartRemoteTest;

On Form2:
Code:
// the form that opened form2 (must be of type Form1 to access the resultString variable)
private Form1 parentForm;

// the constructor of 'Form2'
public void DisplayScreen(Form1 parentForm)
{
     this.parentForm = parentForm;
}

// the button method - you might want to close this form after the resultString is set
private void btnStart_Click(object sender,System.EventArgs
e)
{
     parentForm.resultString = mystringbuilder.ToString();
}

and no, it doesn't affect the ShowDialog method
 
This comes out with an error:
DisplayScreen: member names cannot be the same as their enclosing type.

If public void DisplayScreen(Form1 parentForm) is a constructor, should it have a return type?

I removed the void and this resulted in more errors, saying that the this keyword used in
DisplayScreen oStartString = new DisplayScreen(this);
was not available in the current context.

Also, oStartString.ShowDialog() is not available after I have declared the instance of form2 in form1. Is there a certain place this needs to be put?
 
i made a mistake by writing void there... it's correct without.

the error could be because either the component you call Form1 is not actually called Form1 as a class (in this case replace Form1 with the actual class name) and the ShowDialog missing means that DisplayScreen is not derived from System.Windows.Forms.Form
 
I have a control array of buttons on form1 which i have created in a separate class. I have a common event handler for all the buttons that opens up form2, but with a different Tag value, depending on which button was pressed ie button 4 pressed, form2 opens up with text as 4 and tag value of 4.

So, when i create an instance of form2 in form1, does that mean that i am using another instance and not the instance of the form opened by the button? If this is the case, how do i access the form opened in the other class (ButtonArray). Whenever i create an instance of ButtonArray, i get an error message saying that it takes no overloaded arguments, even though i do not pass any parameters.

Also, what i have is that you can open multiple instances of form2, via the click event in the ButtonArray class. The reason for this is that i need to build different strings depending on the number of the screen and then pass this string value to form1 to be used as the last element of an array.

I'm sorry if this is all a bit confusing, but i am new to C# programming.

Thank you!
 
this is alot clearer now...

you should create the button array only once in form1.
the buttons contained all open a different form2 but when you pass 'this' as parameter you actually pass the class in which they're defined (ButtonArray) and not form1. To use form1 as parameter use 'Button1.FindForm()' instead of 'this'.

on form1 you should try to define resultString as a 'property' instead of a 'string' like this:
Code:
public string resultString
{
     set
     {
             // add the string to the array and do whatever logic you need
            myResultStringsArray.Add(value)
     }
}

the code in form 2 should remain the same.

...basically what it should do is: you click a button, an instance of form2 appears and you click the button that makes the resultString sets it via the property in form1 (which actually adds the value to an array)

let me know if it works
 
I have changed the code as per your instructions, however i get the following error:

Object reference not set to an instance of an object.

Also, I do not have the Add method for my array. Does it have to be an ArrayList to be able to add elements?
 
1. where do you get that error?
2. yes... make it an arraylist so you don't have to worry about it's size.
 
The error comes at the following line:
parentForm.resultString = StartRemoteTest.ToString();

I have declared public SerialJIG parentForm;

and there's the constructor:
public DisplayScreen(SerialJIG parentForm)
{
this.parentForm = parentForm;
}
if this is in the code or not, i still get the same error at the same line.
 
is resultString declared public in Form1?
if so, it means that parentForm is null... try to set a breakpoint at the line with the error and see whether this is the case or not...
 
I made resultString a property of Form1 instead of a string.

My property looks like this:
public string resultString
{
get
{
return sRemoteTest;

}
set
{
//JIGString.Add(value);
sRemoteTest = oStartString.StartString;
JIGString[30] = sRemoteTest;
RemoteTest = value;

}
}

Do i still need to have this line in my code? As i get an error saying i cannot use this.

string sRemoteTest = this.resultString;

The parentForm has undefined value in debug mode, but the error says its a system.nullreferenceexception error.
How do i change the value of parentForm so that its not null?
 
:D

the error is someplace else: (replace your property with)
Code:
public string resultString
{
    // you might not need the get part
    get
    {
        return JIGString[JIGString.Count-1].ToString();
    }
    set
    {
        JIGString.Add(value);
    }
}

and no, you don't need that line anymore.
 
I still get the same error. I put a try-catch statement around the line with the problem and it still comes up with the same message, indicating that that line has an error.

Could i set the same property in form2 and get it in form1? If so, how would i do that?

The other thing is that i need to elements of the array to be in a string format, as i need to send a string. So how would i convert the objects of the arraylist to the strings the are held in the elements. Would it then be easier to just use a normal array?


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top