How to get all instance of FolderBrowserDialog in a form?

D

Du Dang

I want to get all the instances of FolderBrowserDialog in a form
programmaticly

I tried to loop through the Components in the form but I still couldn't pick
it out

foreach (Component com in this.components.Components){
if (com is FolderBrowserDialog){
FolderBrowserDialog fb = com as FolderBrowserDialog;
rchtxtOutput.Text += fb.SelectedPath + "\r\n";
}
else {
rchtxtOutput.Text += com.ToString() + "\r\n";
}
}

any ideas???

Thanks,

Du
 
A

Alfred Taylor

What do you mean you still couldn't pick it out? Meaning your loop didn't
find you all instances? You could try

foreach(Control c in this.Controls)
{
FolderBrowserDialog fbd = c as FolderBrowserDialog;
if(fbd != null)
{
// do stuff with my dialog
}
else
{
// do stuff without a dialog
}
}

and just a side note, using the "as" keyword and checking for null is a more
elegant solution than using "is". if i remember correctly, "is" actually
uses "as" in the underlying IL. so by using "is" followed by"as" you're
essentially making a redundant call.
 
A

Alfred Taylor

whoops, just realized FolderBrowserDialog doesn't inherit from Control . . .
perhaps someone else could step in? ;)
 
D

Du Dang

Thanks for trying!!!


Alfred Taylor said:
whoops, just realized FolderBrowserDialog doesn't inherit from Control . . ..
perhaps someone else could step in? ;)
 
N

Nicholas Paldino [.NET/C# MVP]

Du Dang,

You aren't going to be able to do this. Generally speaking, you aren't
going to be able to get any instances of an object unless you are holding a
reference to them yourself. Basically, store the FolderBrowserDialog in a
hashtable or array and then you will have all the references stored.

What are you trying to do?
 
D

Du Dang

Hi Nicholas,

Thank you for your reply. What I'm trying to do is create a component that
allows developer to drag an drop to their forms and save the selected path
of all the the instance of FolderBrowserDialog and retore the setting next
time the app is opened.

Is there a way to do this through reflection? I don't know much about
reflection yet, I'm still catch up on my reading. However it looks
promising.

Anyway thanks again for your help,

Du Dang

Nicholas Paldino said:
Du Dang,

You aren't going to be able to do this. Generally speaking, you aren't
going to be able to get any instances of an object unless you are holding a
reference to them yourself. Basically, store the FolderBrowserDialog in a
hashtable or array and then you will have all the references stored.

What are you trying to do?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Du Dang said:
I want to get all the instances of FolderBrowserDialog in a form
programmaticly

I tried to loop through the Components in the form but I still couldn't pick
it out

foreach (Component com in this.components.Components){
if (com is FolderBrowserDialog){
FolderBrowserDialog fb = com as FolderBrowserDialog;
rchtxtOutput.Text += fb.SelectedPath + "\r\n";
}
else {
rchtxtOutput.Text += com.ToString() + "\r\n";
}
}

any ideas???

Thanks,

Du
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top