开发者

C# Multi-Form Variables?

I have a main form that has most of the functionality in i开发者_运维知识库t. I was just wondering how would I pass on a variable from say a pop up form, to that main form.

Like for instance:

I have a main form that needs some connection info. So when you click the button "Enter Connection Info", it opens up a new form that the user can type in the IP Address he wants to use for his connection.

On this new form, I have a textbox and a button and once you enter the information it should close and pass on the string that contains the ip back to the original form.

Any suggestions? Do you think there is a better method of accomplishing this than using a windows form, and just going ahead and using a windows form or something? I'm quite perplexed on this issue at the moment.


Expose the textbox text as a public read only property. Show the connection form as a dialog and when it closes, get the connection from the property and then dispose the form:

in open form handler (button click, menu, whatever)

 string connectionString = null;

 using (ConnectionForm form = new ConnectionForm())
 {
     DialogResult result = form.ShowDialog();
     if (result == DialogResult.Ok)
         connectionString = form.ConnectionString
 }

In you connection form:

 public class ConnectionForm: Form
 {
      ....
      public string ConnectionString { get { return textBox1.Text; } }
 }


You can create a public property in your main form and pass main form instance in pop-up constructor. In this way you can change the main form property.

You can also create an event in your pop-up form and hook it in your main form.


I like to use a pattern sort of like this (bear with me, c# is not my first language):

    public class ValueForm: Form 
    {      
        public static string GetFromUser(string originalValue) 
        {
          using (ConnectionForm form = new ConnectionForm())
          {
             form.TheValue = originalValue;
             var result = form.ShowDialog();
             if (result == DialogResult.Ok)
                return form.TheValue;
             else 
                return originalValue;
          }
        } 

       public string TheValue { 
          get { return textBox1.Text; } 
          set { textBox1.Text = value; }
       }

       /* also some code behind your OK & cancel buttons to set 
          DialogResult appropriately, 
          and do any validation that you need to do
       */

    }

and then you would use this like:

string newValue = ValueForm.GetFromUser(oldValue);


Reference Bind the controls on the dialog Form to properties of the Parent Form.

public dlgDbConnProps ( Form Owner )
{
    // TODO: Complete member initialization
    InitializeComponent ( );

    owner = Owner;
}

private void cbo_ProviderLst_SelectedIndexChanged ( object sender, EventArgs e )
{
    owner.Provider = cboLst.Text;
}

Here is another method that I have implemented:

... pass a Func to the child form constructor:

public dlgRequestLogin ( Func<string, string, bool> LoginMethod )
{
    InitializeComponent ( );
    p_loginMethod = LoginMethod;
}

... then handle on button click (or other appropriate event):

private void cmd_SendLoginCredentials_Click ( object sender, EventArgs e )
{
    bool res = p_loginMethod.Invoke ( txt_UserID.Text, txt_UserPassword.Text );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜