Tuesday, 27 August 2013

Get User Control Variable

Get User Control Variable

Using Visual Studio 2008
I have a user control. The button calls a folder browser dialog. I am
trying to pass the path to the parent form and it's just not getting
there. I need a little input....
User Control:

public partial class FolderSelectDDL : UserControl
{
public delegate void ButtonClickedEventHandler(object sender,
EventArgs e);
public static event ButtonClickedEventHandler OnUserControlButtonClicked;
private string folderPath;
public string FolderPath
{
get { return folderPath; }
set { folderPath = value; }
}
public FolderSelectDDL()
{
InitializeComponent();
}
private void btnSaveToPath_Click(object sender, EventArgs e)
{
string path;
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
path = folderBrowserDialog1.SelectedPath;
if (OnUserControlButtonClicked != null)
OnUserControlButtonClicked(this, e);
folderPath = path;
}
}
}
And the form:
public partial class ImportCreateExcel : Form
{
FolderSelectDDL uc = new FolderSelectDDL();
public ImportCreateExcel()
{
FolderSelectDDL.OnUserControlButtonClicked += new
FolderSelectDDL.ButtonClickedEventHandler(btnSaveToPath_Click);
InitializeComponent();
}
private void btnSaveToPath_Click(object sender, EventArgs e)
{
MessageBox.Show(uc.FolderPath); //blank
//MessageBox.Show(uc.folderBrowserDialog1.SelectedPath); //blank
}
}
The path is always blank, whether it's from the dialog which is set to
public, or the variable FolderPath.

Any input is always welcome.
Thanks!

No comments:

Post a Comment