|
-
Jul 29th, 2005, 01:31 AM
#1
[RESOLVED] Passing values (structs) between forms...
This is a bit complicated to follow. But please try.
I have one class with a form in it. And as a member object it has an object of an other form. Lets call the first one A and the one contained in A for B.
When A starts, it makes an instance of the B form, and showes it. Then B will let them choose some options, and when they press the OK button, I want A to be able to receive these settings. No matter how it is done. I have tried a couple of things, but not sure what way to go about it.
I tried passing a "this" pointer to the object Bs constructor. And I am able to send values back with this pointer. But the problem is that when Form B is disposed, it will also dispose what is in the other end of the pointer. Hence it disposes Form A too.
In C++ I guess I would have used a function pointer, and in C# that is more or less a Delegate (even if it is not exactly the same thing).
So any good sugestions here?
- ØØ -
-
Jul 29th, 2005, 02:53 AM
#2
Fanatic Member
Re: Passing values (structs) between forms...
declare ur form A as public static and also the component modifier to public. ' ex public static A form1 = null;
If u make an instance of B initialize the the variable.
form1 = this;
in Form B . u can call such like dis.
A.form1.textbox1.text = "hello";
A.form1.textbox2.text = "test";
-
Jul 29th, 2005, 03:29 AM
#3
Re: Passing values (structs) between forms...
Hmmm..yeah...I can probably do that. But it will require all my member variables and member functions to be static too though....doesn't it...hmmm....I guess the solution is OK, not great, but OK...would have been prettier with a function pointer or something like that. So if anyone has a better solution, I want to hear it, and untill then I will use this method.
Sample:
Code:
using System;
// Program start class
public static class A{
private static String m_hello;
public static void Main(){
B test = new B();
test.callBack();
gameLoop();
}
private static void gameLoop(){
Console.WriteLine("m_hello: " + m_hello);
}
public static void setString(String p_hello){
m_hello = p_hello;
}
}
public class B : IDisposable{
public B(){
}
public void callBack(){
A.setString("hello");
this.Dispose();
}
public void Dispose(){
GC.SuppressFinalize(this);
}
}
Thanks popskie
- ØØ -
-
Jul 29th, 2005, 03:51 AM
#4
Re: Passing values (structs) between forms...
If you are using B like a dialog box then you can override B's ShowDialog and alter it so it returns an instance of your struct instead of the normal DialogResult.
I have only ever done this in VB and its works nicely.
I don't live here any more.
-
Jul 29th, 2005, 04:07 AM
#5
Re: Passing values (structs) between forms...
Hmmm...yeah, that is basicaly what I do. I am just using it to set parameters for the other form. Hmm....so do I still inherit from Form, or from some Dialog class?
- ØØ -
-
Jul 29th, 2005, 09:01 AM
#6
Fanatic Member
Re: Passing values (structs) between forms...
Inherit a form, it has the Showdialog() function which you can override like wossname said. You can also give it properties to retreive the values.
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jul 29th, 2005, 09:33 AM
#7
Re: Passing values (structs) between forms...
Ahha...ok...don't have MSDN here, so can't look it up, will do some googling and check MSDN when I get home, and tell you if I can make it work...I am a n00b after all..
- ØØ -
-
Jul 29th, 2005, 10:19 AM
#8
Re: Passing values (structs) between forms...
Hmmm...I might not even have to over ride it if I can just do this:
Code:
public void ShowMyDialogBox()
{
Form2 testDialog = new Form2();
// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of testDialog's TextBox.
this.txtResult.Text = testDialog.TextBox1.Text;
}
else
{
this.txtResult.Text = "Cancelled";
}
testDialog.Dispose();
}
- ØØ -
-
Jul 29th, 2005, 06:10 PM
#9
Re: [RESOLVED] Passing values (structs) between forms...
Yeah, that worked really well. I wrote a few read only properties for the dialog, so the settings can be read when it is closing before it is disposed... Really nice looking code..
- ØØ -
-
Jul 30th, 2005, 12:30 PM
#10
Re: [RESOLVED] Passing values (structs) between forms...
i feel ill. 
Naughty noteme.
Last edited by wossname; Jul 30th, 2005 at 12:35 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|