Passing strings from one Form to another in Visual Basic Express 2008?

Discussion in 'Programming' started by FPForum, Feb 6, 2010.

  1. #1
    I am using a TextBox control on one form to accept a directory name and trying to use it on a second form to find the number of files in that directory

    (filecnt = My.Computer.FileSystem.GetFiles(Form1.txtLocallyArchivedJpg). I receive the following error whenever I try and use the directory entered in the TextBox “Value of type 'System.Windows.Forms.TextBox' cannot be converted to 'String'”

    How can I get rid of this error?
     
    FPForum, Feb 6, 2010 IP
  2. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There are several methods. The easiest is to write a public property on Form1 to pass the string of the test box as in following example.

    
    public string MyTestBox {
         get {return textbox1.Text;}
         set {textbox1.Text = value;}
    }
    
    Code (markup):
     
    NeoCambell, Feb 6, 2010 IP
  3. FPForum

    FPForum Notable Member

    Messages:
    4,172
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    225
    Digital Goods:
    2
    #3
    Thanks Neo. I tried to leave some rep for you but says I need to leave rep for others first before I can give you more, lol. Anyways, I tried what you had said but I am getting an error saying the keyword string is not a valid identifier.

    Basically I can get the two forms to talk to one another...but I can't seam to pass the text in textbox1 (Form1) into a textbox I have on Form2.

    Thanks again for your help!
     
    FPForum, Feb 6, 2010 IP
  4. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Did you load the form with text box as below from the main form? (Note that codes are in C#, I think it will be easy to convert them to VB.Net)

    
            Form2 form2;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                form2 = new Form2();
                form2.Show();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show(form2.MyTestBox);
            }
    
    Code (markup):
     
    NeoCambell, Feb 7, 2010 IP
  5. FPForum

    FPForum Notable Member

    Messages:
    4,172
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    225
    Digital Goods:
    2
    #5
    Thanks Neo! Got it all working now :)
     
    FPForum, Feb 8, 2010 IP