tags:

views:

115

answers:

10

I need to return a string from a button method .How can i do that?

private string folderPathButton_Click(object sender, EventArgs e)
{
    FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
    folderBrowser.ShowDialog();
    string folderPath = folderBrowser.SelectedPath;                 
    return folderPath;           
}

On that method on button click i get the folder path.Now i need to return it.But this code is not working? Can anybody help me to solve the problem??How can i return the folder path and call it from another method??

+2  A: 

You can't change the signature of an event.

You could create another different method that returns a string and does the work and call it from the button handler and wherever else you need it.

private void folderPathButton_Click(object sender, EventArgs e)
{
    browseAndStuff();
}

private string browseAndStuff()
{
    FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
    folderBrowser.ShowDialog();
    string folderPath = folderBrowser.SelectedPath;                 
    return folderPath;           
}
Brian R. Bondy
Dear Brian R. Bondy, thanks for answer.But i face a new problem.I just need the folder path.Because i use the path in many method to save or edit file after doing some work .so when i call the method then the FolderBrowserDialog is also open in several methods and its not possible to work.So is their any easy way to get the folder path only?? pls reply ...thanks--riad
riad
@riad: You can only open the folderBrowser is a member variable is not already filled. So move the `string folderPath` to the top of your class and before you call `ShowDialog` first check if the variable is already filled.
Brian R. Bondy
sorry for asking many times...could u pls give me an example?It will be better for me..thanks again--riad
riad
Let say i want only the folder path on abc method..what should i do??private void abc(){ string fpath=??///need only the folder path not to open the file dialoger.what should i wrote }pls guide
riad
+2  A: 

You cannot do that, the click event returns void.

Raj
+2  A: 

Call your other method from the button event. Button event fires when you click - there is nothing proceding it, so you can't call a button event from a method as it makes no sense.

SLC
A: 

The method fires when you click the button, at which point you store the folder path in the variable folderpath - then you return the string to wherever you came from, whick is nowhere (the click of a button won't stora any variables).

So what you'll need to do is create a class variable and assign the value to it in the button method.

illuzive
A: 

Either what Brian suggested or set a property that is accessible.

azel
A: 

The click event handler is supposed to have a return type of void, so you can't return anything from it. The event handler is called when the button is clicked, so it makes no sense to call it yourself.

If you want to keep the folder path for later processing simply save it in a field/property.

Jacob
A: 

Sorry for not answering the question that you asked, but as others have already said - I don't believe it is possible.

If I understand you correctly, what you actually want to do is change the folder path on select, so I think you want something like this:

private string folderPathButton_Click(object sender, EventArgs e)
{
    FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
    folderBrowser.ShowDialog();
    txtFolder.Text = folderBrowser.SelectedPath;                 
}

Assuming txtFolder is the name of your textbox control.

pm_2
A: 

The click event handler fires when the user clicks on that button. So you start taking an action there. You can all another method that performs all you are trying to do within that method.

Damiano
+1  A: 

As others have explained, you'd have to do something like this, and add guarding so you don't accidentally overwrite the value if the user wants to cancel from the dialog.

private string myFolderPath;

private string folderPathButton_Click(object sender, EventArgs e)
{
    FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
    if(DialogResult.OK == folderBrowser.ShowDialog())
    {
        myFolderPath = folderBrowser.SelectedPath;    
    }
}
ZombieSheep
thanks bro for ans.but i need just return the folder path.because i use it to another some methods to edit and save some file.so i just need the folder path string.can u help??--riad
riad
You can't. You have to use a variable within your form class somewhere, and reference that variable in your other methods (see above for assigning the value to the local variable). A button click event can not return anything other than void.
ZombieSheep
A: 

You can't really change the signature of the button click event, so you need to handle the string value you want to return when the button is clicked by some other logic. You might store in a class variable in the class or display the string in a label, textbox or some other graphical component on the form in question. If you want to save or open a file using the selected path, you could pass the path to another function that handles the save/load work and then either show the user the file or its content (by opening it or displaying its content on the form, depending on requirements) or notify the user that the file was saved successfully on the specified path.

daft