Connecting Tech Pros Worldwide Forums | Help | Site Map

C#, win App, Dialog in dialog, Dialogresult error

Newbie
 
Join Date: Jul 2007
Posts: 11
#1: Oct 10 '07
Hi there!

in my app, i open a Dialog, in this dialog i use the FileOpenDialog, when i click on 'Ok' or 'Cancel' in my FileOpenDialog, it automatically closes the parent dialog, with a DialogResult.Cancel???

I have overloaded the ShowDialog to accept parameters in the 'Parent' Dialog


main Code
...Calling Parent Dialog
Expand|Select|Wrap|Line Numbers
  1. if (GenerelSetup.ShowDialog(ref szXStatIni, ref szLogbookFile) == DialogResult.OK)
  2. {....}
  3.  
Parent Dialog
Expand|Select|Wrap|Line Numbers
  1.         public DialogResult ShowDialog(ref string szStatIniFileName, ref string szMachinePath)
  2.         {
  3.             DialogResult ret;
  4.             this.szStatIniFileName = txtStatIni.Text = szStatIniFileName;
  5.             this.szMachinePath = txtMachinePath.Text = szMachinePath;
  6.             ret = this.ShowDialog();
  7.             return ret;
  8.         }
  9.  
  10.         private void btnBrowseStatIni_Click(object sender, EventArgs e)
  11.         {
  12.             openFileDialog.Filter = "X-stat ini files (*.ini)|*.ini|All files (*.*)|*.*";
  13.             openFileDialog.FilterIndex = 1;
  14.             openFileDialog.FileName = "x-stat.ini";
  15.             openFileDialog.InitialDirectory = szStatIniFileName;
  16.             openFileDialog.RestoreDirectory = true;
  17.  
  18.             if (openFileDialog.ShowDialog() == DialogResult.OK)
  19.             {
  20.                 szStatIniFileName = openFileDialog.FileName;
  21.                 txtStatIni.Text = openFileDialog.FileName;
  22.             }
  23.         }
  24.  
The parent dialog opens correct, and the fileopen dialog opens as well.

Anybody able to give me a hint in the right direction??



Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Oct 10 '07

re: C#, win App, Dialog in dialog, Dialogresult error


On your parent dialog window, what buttons are set as the "ok" button and what is set as the "cancel" button?
Maybe it defaulted your button to be the cancel button?
Newbie
 
Join Date: Jul 2007
Posts: 11
#3: Oct 10 '07

re: C#, win App, Dialog in dialog, Dialogresult error


Hi Plater,

No, my 'Ok' button is defined as AcceptButton, and my Cancel button is defined as CancelButton on the parent dialog, but I dont touch any of these buttons, it automatically happens when i click on 'Ok' or 'Cancel' on the OpenFileDialog dialog...
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4: Oct 10 '07

re: C#, win App, Dialog in dialog, Dialogresult error


Wierd. I just did it with no problem at all.
I had my mainwindow open another window with .ShowDialog() and inside that Dialog window I opened up an open file dialog.
No problems with it closing or anything.

Can you set a breakpoint in the button handler and step through the code, maybe that will help get an idea of what's going on?
Newbie
 
Join Date: Jul 2007
Posts: 11
#5: Oct 10 '07

re: C#, win App, Dialog in dialog, Dialogresult error


the parentdialog is activated by the statement ret = this.ShowDialog. in line 6

after pressing the buttonhandler, and accepting with an 'Ok' in the fileopendialog, it executes the correct code inside my buttonhandler and then continues to my 'Return ret' in line 7 and ret has the value of 'Cancel', forcing the parent dialog to close.

Expand|Select|Wrap|Line Numbers
  1.         public DialogResult ShowDialog(ref string szStatIniFileName, ref string szMachinePath)
  2.         {
  3.             DialogResult ret;
  4.             this.szStatIniFileName = txtStatIni.Text = szStatIniFileName;
  5.             this.szMachinePath = txtMachinePath.Text = szMachinePath;
  6.             ret = this.ShowDialog();
  7.             return ret;
  8.         }
  9.  
Definately weird...
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#6: Oct 10 '07

re: C#, win App, Dialog in dialog, Dialogresult error


Maybe it has something to do with calling .ShowDialog() from inside itself, with your ShowDialog() Wrapper.
Would it not be better to do that with the constructor, or make some public properties that can be set with the values?
Newbie
 
Join Date: Jul 2007
Posts: 11
#7: Oct 10 '07

re: C#, win App, Dialog in dialog, Dialogresult error


No, tried to add it to the constructor, but same error.

Then i inspected the .Designer.cs .

It seems somehow Cancel had been added to all my buttons,even the button for showing the FileOpenDialog, althoug looking correct in the properties pane...

Removing these, and adding:
Expand|Select|Wrap|Line Numbers
  1. this.btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
  2. .
  3. .
  4. this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  5.  
It now works perfect.

Thanks for your time Plater, take care.
Newbie
 
Join Date: Sep 2008
Posts: 1
#8: Sep 8 '08

re: C#, win App, Dialog in dialog, Dialogresult error


This usually happens when one copies a button with the Dailog Result set. It is easy to do this when designing a wizard for instance and one uses the ok or cancel button as a template button that is placed somewhere else.
Reply


Similar .NET Framework bytes