Search code examples
c#openfiledialog

Deciding how to pass selected file from Open File Dialog to another form


I am having a problem with thinking of a way that would let me pass a selected file to another form.

I am trying to select a file in Form 1 by using Open File Dialog and then pass the same file to Form 2 in which I will try to add another entry into a file.

The problem is that I am nnot sure what I should be passing. Should I pass only openFileDialog.FileName or I need to pass more than just this?

I was thinking of this all day today but I don't know how should I do this? Which parts of Open File Dialog should I pass over?

Or.. I might just thinking too hard of this problem. Please clear my mind.


Solution

  •         public partial class Form1 : Form
            {
                Form2 frm2;
                public Form1()
                {
                    InitializeComponent();
                    frm2 = new Form2();
                }
                private void btnOpenFile_Click(object sender, EventArgs e)
                {
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        frm2.FileName = openFileDialog1.FileName;
                        frm2.Show();
                    }
                }
            }
    
        public partial class Form2 : Form
        {
            string _fileName = "";
             public string FileName
            {
                get
                {
                    return this._fileName;
                }
                set
                {
                    this._fileName = value;
                }
            }
        }