Zath
Zath

Reputation: 557

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!

Upvotes: 1

Views: 2234

Answers (2)

King King
King King

Reputation: 63327

You have already had a uc in your form class, but you defined a new uc (local variable) in the btnSaveToPath_Click handler which of course has the initial FolderPath property as empty.

private void btnSaveToPath_Click(object sender, EventArgs e)
{
    MessageBox.Show(uc.FolderPath); //NOT blank
    MessageBox.Show(uc.folderBrowserDialog1.SelectedPath); //NOT blank
}

UPDATE

You don't have any code to fire the custom event in your UserControl:

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;
        //Fire event here
        if(OnUserControlButtonClicked != null) OnUserControlButtonClicked(this, EventArgs.Empty);
     }        
  }
}

Upvotes: 1

Shaharyar
Shaharyar

Reputation: 12449

The form code should be:

public partial class ImportCreateExcel : Form
{
    FolderSelectDDL uc = new FolderSelectDDL();
    public ImportCreateExcel()
    {
        //FolderSelectDDL.OnUserControlButtonClicked += new FolderSelectDDL.ButtonClickedEventHandler(btnSaveToPath_Click);
        uc.OnUserControlButtonClicked += new FolderSelectDDL.ButtonClickedEventHandler(btnSaveToPath_Click);
        InitializeComponent();
    }

    private void btnSaveToPath_Click(object sender, EventArgs e)
    {
        //FolderSelectDDL uc = new FolderSelectDDL(); //a new instance
        MessageBox.Show(uc.FolderPath);
    }
}

You are creating a new instance that's why it is blank. I have not tried it. If any problem here. Ask me.

Upvotes: 0

Related Questions