How to return string from a button click handler?
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??
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;
}
You cannot do that, the click event returns void.
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.
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.
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;
}
}
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.
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.
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.
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.
精彩评论