• Navigation
Results 1 to 4 of 4
  1. #1
    Salvage Bans
    Join Date
    Apr 2006
    Posts
    763
    BG Level
    5
    FFXI Server
    Lakshmi

    C# Programming - Accessing value from a control on a separate form

    Okay, so the program has three forms.

    One is the customer information form (name, address, city, etc.),

    second is the order form and whatever it is they are hypothetically buying,

    and third is the order summary form.

    On the third form, I want to pull data from textboxes and such from form 1 and 2 and plug 'em into form 3.

    I did some research, but the only way I am seeing is to use code like:
    Form1 someForm = new Form1();
    and then to set the control's modifier to public

    The problem with that, though, is I am assuming it's creating a new instance of the form? And if so, then all of the fields are blank.

    Could have sworn visual basic does this way easier... any help is appreciated!

  2. #2
    Banned.

    Join Date
    Oct 2006
    Posts
    10,115
    BG Level
    9

    Create a separate class or classes to store/read the Form data from.

  3. #3
    Fake Numbers
    Join Date
    Jun 2005
    Posts
    86
    BG Level
    2
    FFXI Server
    Bahamut
    WoW Realm
    Mal'Ganis

    What the above poster is probably the better way of doing it, but if you want a quick and dirty way of doing it you can instantiate the other form and change the "Modifier" property of the controls you want to access from "Private" to "Public".

    Simple example 1: (Change while Form2 is still open)

    first form: (contains a textbox with a public modifier, textBox1)

    Code:
    Form2 frm2 = new Form2(this);
    frm2.ShowDialog(); //You can use Show() if you want both forms to be interactive at once
    second form:
    Code:
    //include statements
    
    private Form1 _f1;
    
    //Constructor takes one instance of Form1
    public Form2(Form1 f1)
    {
     _f1 = f1;
    }
    
    private void Form2_Load(object sender, EventArgs e)
    {
    _f1.textBox1.Text = "Changed from Form2";
    }
    Simple example 2: (Textbox values change after Form2 is closed. Public modifier controls not necessary since code from first Form is doing the changing)

    First Form:

    Code:
    Form2 frm2 = new Form2();
    frm2.ShowDialog();
    textBox1.Text = frm2.Sample; //This doesn't trigger until frm2 is closed when using ShowDialog(). If you use Show() it would be set to the initial value, in this case "ABCD"
    Second Form:

    Code:
    public string Sample = "ABCD";
    //You can modify the public string however
    
    //Other code
    
    this.Close();
    Some part of this could be slightly off but it should work.

  4. #4
    Salvage Bans
    Join Date
    Apr 2006
    Posts
    763
    BG Level
    5
    FFXI Server
    Lakshmi

    Thanks to the both of you. Managed to get it working!

Similar Threads

  1. Replies: 8
    Last Post: 2008-12-21, 02:11
  2. XBCD & 360 Controller on PC
    By Tradition in forum Tech
    Replies: 6
    Last Post: 2008-09-08, 11:44
  3. Bluetooth and PS3 controller on PC
    By Vertabreaker in forum Tech
    Replies: 2
    Last Post: 2008-06-04, 12:27
  4. Replies: 1
    Last Post: 2007-11-15, 18:46
  5. PS3 controller on the PC
    By khain in forum Tech
    Replies: 1
    Last Post: 2007-04-29, 14:38
  6. xbox360 controller on vista
    By Boyiee in forum Tech
    Replies: 0
    Last Post: 2007-02-13, 14:07