Hi, I have two forms in my application. I created an instance of one in the other e.g. FileBrowse browse = new FileBrowse();
Then, I call browse.show() and the form appears. In the class FileBrowse, I have a few buttons etc. One such button opens a file dialog to the user. When do I invoke the broswe.btnBrowse(rtb1) in my other form?

Help is appreciated.

You need to go to the FileBrowse.Designer.cs file, search where the btnBrowse declaration is, and change it to public, anyways this would only grant you acces to the very button properties, as Size, text etc... if you want to execute button instructions, you need to place those instructions into one public method, which you can call from another form. this method can be static as well, but if so needs to be called from a static method to.

Thanks Ricardo. I understand what you are saying, but I'm still unsure.
Let me explain.
There is MainForm.cs and FileBrowse.cs
I create an instance of FileBrowse in MainForm
I call it's show() method and it appears
Now, what I want is when the user clicks on the 'browse' button, a file dialog opens.
I have the code for this, but I don't know when to execute it.

Help is appreciated.

ok, first you have to make a method like this:

private void BrowseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter =
            "Media Files|*.wav;*.mp3;*.mp2;*.wma|All Files|*.*"; //or your prefered file types
            if (DialogResult.OK == openFileDialog.ShowDialog())
            {
              //Do stuff
            }
     }

and where you declared BrowseButton add this line:

this.BrowseButton.Click += new System.EventHandler(this.BrowseButton_Click);

that way, you are tellin BrowseButton which method to invoke when clicked, notice that this method have to be void, and also have object sender, EventArgs e as parameters, otherwise it wont work

That worked perfectly!
Just one more question....
Once the dialog opens and the user selects the file, the file that opens is to open in a text area in MainForm.cs
Do I need to reference MainForm in some way to do this?

Thanks

In FileBrowse.cs you need to place one "OK" button, this button might have this property:

this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;

then lets say that you stored the text of the file into a String variable, make it public and call your FileBrowser form from MainForm as you did for the OpenFile Dialog. like this:

FileBrowser browse = new FileBrowser();

if(browse.ShowDialog() == DialogResult.OK){
      textbox1.Text = browse.TheFileYouJustRead;
}

this instead doing FileBrowser.Show();

Thanks for your reply. I don't follow this. Could you perhaps try and explain it more, if possible, step by step.

Help is appreciated.

This article has been dead for over six months. Start a new discussion instead.