[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?
- ØØ -
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";
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
- ØØ -
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.
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?
- ØØ -
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.
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..:)
- ØØ -
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();
}
- ØØ -
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..:)
- ØØ -
Re: [RESOLVED] Passing values (structs) between forms...
i feel ill. :D
Naughty noteme.