Results 1 to 10 of 10

Thread: [RESOLVED] Passing values (structs) between forms...

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Resolved [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?
    - ØØ -

  2. #2
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    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";

  3. #3

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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
    - ØØ -

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  5. #5

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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?


    - ØØ -

  6. #6
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    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

  7. #7

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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..


    - ØØ -

  8. #8

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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();
    }

    - ØØ -

  9. #9

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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..


    - ØØ -

  10. #10
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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
  •  



Click Here to Expand Forum to Full Width