5 Replies - 360 Views - Last Post: 26 February 2012 - 02:21 PM Rate Topic: -----

#1 roguex20  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 11-August 11

Where Do I Pass The Value To The Next Form? Signed - Frustated Guy

Posted 25 February 2012 - 07:13 PM

I got some clues from someone in a previous thread and from a few websites, but I am doing something wrong in my code that is not allowing the value of the string variable to pass to the second form. I tried different ways and spent quite some time, but no luck.

I will clean up the code after I get some more functionality working. Currently stuck at getting the value of destFile2 over to form2.

Here is my code for form1 and below that for form2.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Project21
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(this); //pass reference to this form in constructor
        }
        public string TextBoxText //read only property to access textbox text
        {
            get { return textBox1.Text; }
        }
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        string targetPath = @"C:\temp";
        string targetPath2 = @"C:\FileIn";
        string renamePath = @"C:\FileOut";
        
        public void button1_Click(object sender, EventArgs e)
        {
            fbd.Description = "Select folder";
            fbd.RootFolder = Environment.SpecialFolder.Desktop;
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                MessageBox.Show(fbd.SelectedPath);
                
            string[] files = System.IO.Directory.GetFiles(fbd.SelectedPath);
            progressBar1.Minimum = 0;
            progressBar1.Maximum = files.Length;
            progressBar1.Step = 1;
            
            // Copy the files and overwrite destination files if they already exist.
            foreach (string file in files)
            {
                // Use static Path methods to extract only the file name from the path.
                
                string fileName = System.IO.Path.GetFileName(file);
                string destFile = System.IO.Path.Combine(targetPath, fileName);
                
                System.IO.File.Copy(file, destFile, true);
                progressBar1.PerformStep();
            }
         }

        private void button2_Click(object sender, EventArgs e)
        {
            if(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                MessageBox.Show(ofd.FileName);

            string[] files = System.IO.Directory.GetFiles(targetPath);
            //move file to FileIn folder on desktop

            progressBar2.Minimum = 0;
            progressBar2.Maximum = files.Length;
            progressBar2.Step = 1;
             
            string destFile2 = System.IO.Path.GetFileName(ofd.FileName);
            
            File.Copy(ofd.FileName, targetPath2 + "\\" + destFile2);
            progressBar2.PerformStep();
            InitializeComponent();
            this.Hide();
            
            Form2 frm2 = new Form2(this);
            frm2.ShowDialog();
            frm2.MdiParent = this;} 
        
        }
        
 }



form2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project21
{
    public partial class Form2 : Form
    {
        Form1 frm1;
        private string p;
        public Form2(Form1 callingForm)
        {
            InitializeComponent();
            frm1 = callingForm; //store local reference to Form1
        }

        public string TextBoxText //read only property to access textbox text
        {
            get { return textBox1.Text; }//testing to see if the I can pass value from from1 to here - not working
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Test;more todo here");//would like to be able to
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Where Do I Pass The Value To The Next Form? Signed - Frustated Guy

#2 tlhIn`toq  Icon User is online

  • Closing in on 5,000
  • member icon

Reputation: 4926
  • View blog
  • Posts: 10,462
  • Joined: 02-June 10

Re: Where Do I Pass The Value To The Next Form? Signed - Frustated Guy

Posted 25 February 2012 - 07:31 PM

This is asked so often it is my FAQ #3

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2


FAQ (Frequently Asked Questions - Updated Feb 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Closing in on 5,000
  • member icon

Reputation: 4926
  • View blog
  • Posts: 10,462
  • Joined: 02-June 10

Re: Where Do I Pass The Value To The Next Form? Signed - Frustated Guy

Posted 25 February 2012 - 07:41 PM

        private void Form1_Load(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(this); //pass reference to this form in constructor
        }




        public Form2(Form1 callingForm)
        {
            InitializeComponent();
            frm1 = callingForm; //store local reference to Form1
        }




This is wrong on so many levels.

What you want to be doing is raising an event in one form and subscribing to it from the other.

I beg of you to stop trying to do this and look at those tutorials I linked to you.
Was This Post Helpful? 1
  • +
  • -

#4 roguex20  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 11-August 11

Re: Where Do I Pass The Value To The Next Form? Signed - Frustated Guy

Posted 25 February 2012 - 08:27 PM

View PosttlhIn`toq, on 25 February 2012 - 07:41 PM, said:

        private void Form1_Load(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(this); //pass reference to this form in constructor
        }




        public Form2(Form1 callingForm)
        {
            InitializeComponent();
            frm1 = callingForm; //store local reference to Form1
        }




This is wrong on so many levels.

What you want to be doing is raising an event in one form and subscribing to it from the other.

I beg of you to stop trying to do this and look at those tutorials I linked to you.


thanks.....i am still new and yes I recall reading up on subscribing....

I also checked out the faq 3 finally and see how I was in the wrong direction.
Was This Post Helpful? 0
  • +
  • -

#5 roguex20  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 11-August 11

Re: Where Do I Pass The Value To The Next Form? Signed - Frustated Guy

Posted 26 February 2012 - 01:39 PM

View PosttlhIn`toq, on 25 February 2012 - 07:41 PM, said:

        private void Form1_Load(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(this); //pass reference to this form in constructor
        }




        public Form2(Form1 callingForm)
        {
            InitializeComponent();
            frm1 = callingForm; //store local reference to Form1
        }




This is wrong on so many levels.

What you want to be doing is raising an event in one form and subscribing to it from the other.

I beg of you to stop trying to do this and look at those tutorials I linked to you.


I got it to work. I really appreciate you forcing me to do it the right way. That help get my head wrapped around something I had the pieces of the puzzle for put not all put together. Its all so simple now to me, but it took me going through the process to finally get it to click and sink in.

Developing just got more interesting for me because of this. Now I have to figure out a way to get autoitscript to work with this. AutoIt is a pretty good scripting language. I will I took up a .net language years ago - this would have helped me in so many other small apps I've used autoit for.
Was This Post Helpful? 1
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Closing in on 5,000
  • member icon

Reputation: 4926
  • View blog
  • Posts: 10,462
  • Joined: 02-June 10

Re: Where Do I Pass The Value To The Next Form? Signed - Frustated Guy

Posted 26 February 2012 - 02:21 PM

Glad to have helped. Its what we're here for.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1