I have added an open file dialog box to my client application so that the used can select a specific file they want to send to the web service.
However the file gets sent the moment the file has been selected, whereas I would like to have a secondary prompt e.g. "Send - 'file name' Button Yes. Button No." to pop up after they have selected the file.
This would be incase the user selected the wrong file they would have a chance to see which one they selected.
So far I have the following code -
private void button1_Click(object sender, EventArgs e)
{
//Read txt File
openFi开发者_如何学GoleDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader myReader = new StreamReader(openFileDialog1.FileName);
myReader.Close();
string csv = File.ReadAllText(openFileDialog1.FileName);
I need the prompt to come up after they have selected the file but not sure how to do this so any input would be greatly appreciated.
you need to add the second check manually after the first dialog:
private void button1_Click(object sender, EventArgs e)
{
//Read txt File
openFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (MessageBox.Show("Message", "Title",MessageBoxButtons.YesNo)==DialogResult.Yes)
{
StreamReader myReader = new StreamReader(openFileDialog1.FileName);
myReader.Close();
string csv = File.ReadAllText(openFileDialog1.FileName);
etc etc
Information on MessageBox.Show. You can get information on the possible results/options from here.
You could ensure that the user sees the file to be uploaded by making the message something like:
"Are you sure you want to upload " + openFileDialog1.FileName;
MessageBox.Show(...) is the method you're looking for.
You can use a message box:
if (MessageBox.Show(string.Format("Upload {0}, are you sure?", openFileDialog1.FileName), "Please Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// ...
}
A sample of the code modified.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
DialogResult dr = MessageBox.Show(message, caption, MessageBoxButtons.YesNo);
if(dr == DialogResult.Yes )
StreamReader myReader = new StreamReader(openFileDialog1.FileName);
// more code
else
// do something else
精彩评论