|
-
May 10th, 2007, 07:00 AM
#1
Thread Starter
Addicted Member
[RESOLVED] openfiledialog question
' Display an OpenFileDialog so the user can select a Cursor.
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "VMware Configuration files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.Title = "Selecteer een bestand"
' Show the Dialog.
' If the user clicked OK in the dialog and
' a .XML file was selected, open it.
If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
I gote a problem whit this code
you can open a txt file but when
I click an the button cancel he gives error
Ik know that with this it to do has
If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
I can just open it but i wanne cancel to how do i do that
-
May 10th, 2007, 07:18 AM
#2
Re: openfiledialog question
There shouldn't be an error really. At least not from what you have shown. I guess there's code that tries to execute outside of the Open file dialog but it's hard to say without seeing it. You could try this:
vb Code:
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
'You have code here
ElseIf OFD.ShowDialog = Windows.Forms.DialogResult.Cancel Then
Exit Sub 'Don't execute any code that follows this
End If
-
May 10th, 2007, 07:52 AM
#3
Thread Starter
Addicted Member
Re: openfiledialog question
-
May 10th, 2007, 11:59 AM
#4
Re: openfiledialog question
That will show the dialog twice though.
Should be
Code:
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
'Ok clicked
Else
'Canceled
End If
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 1st, 2009, 05:25 AM
#5
Member
Re: [RESOLVED] openfiledialog question
Guys help, I can' t understund
I used the above code, I used three different codes, none works.
Here is another example
Select Case MessageBox.Show(MessageBoxButtons.OKCancel)
Case MsgBoxResult.Yes
'do yes stuff here
MessageBox.Show("OK")
Case MsgBoxResult.Cancel
'do cancel stuff here
MessageBox.Show("Cancel")
End Select
I press "Open" or "Cancel", and the dialogbox shows itself again and again, for 10-15 times.
It executes Messagebox, and the same again. After this it stops.
Some times (with Select-Case), it gives me MessageBox "1".
It doesn't make sence.
I traced with breakpoint, but no luck. It goes round and round.
-
Sep 1st, 2009, 11:38 AM
#6
Re: [RESOLVED] openfiledialog question
Four things:
1) You have Option Strict Off if that code compiles, so I would suggest turning it on.
2) The first parameter of the Show method is a string containing the text you want to show, the second parameter is the title of the message box, the third parameter is where the "MessageBoxButtons.OKCancel" should go.
3) The show method returns a DialogResult, not a MsgBoxResult
4) You didn't mention where this code even is.
Try this to show your message box:
vb.net Code:
Select Case MessageBox.Show("text", _ "caption", _ MessageBoxButtons.OKCancel) Case Windows.Forms.DialogResult.OK 'Run code for acceptance Case Windows.Forms.DialogResult.Cancel 'Run code for cancel End Select
The reason you are getting a message box of "1" is because (as mentioned earlier) you have Option Strict Off so there is an implicit conversion to string going on in your MessageBox.Show. The Show method is expecting a string where you put MessageBoxButtons.OKCancel so it automatically converts that to it's string representation which is "1".
-
Sep 2nd, 2009, 06:43 AM
#7
Member
Re: [RESOLVED] openfiledialog question
Now it comes up only twice, and then it gives me an error.
Code:
Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click, cmdLoad.Click
Dim ofd As New OpenFileDialog
Dim FileName As String
Dim sr As StreamReader
ofd.InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
ofd.Filter = "Excel Files|*.xls|Excel 2007 Files|*.xlsx"
ofd.DefaultExt = "*.xls"
ofd.ShowDialog()
Select Case MessageBox.Show("text", _
"MMMMMMmmmmmm", _
MessageBoxButtons.OKCancel)
Case Windows.Forms.DialogResult.OK
'Run code for acceptance
Try
MyConn.Open()
MyComm = New OleDbCommand("select * from [TZOKER$]", MyConn)
dtAdapter = New OleDbDataAdapter(MyComm)
dtAdapter.Fill(dtSet, "TZOKER")
For Each dtRow As DataRow In dtSet.Tables(0).Rows
'sum = dtRow("N1") + dtRow("N2") + dtRow("N3") + dtRow("N4") + dtRow("N5")
'code to do something with the 'sum' value
Next
dgViewSample.DataSource = dtSet.Tables(0)
MyConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
MyConn.Close()
Finally
MyConn.Close()
MyConn = Nothing
MyComm = Nothing
dtAdapter = Nothing
dtSet = Nothing
End Try
Exit Select
Case Windows.Forms.DialogResult.Cancel
'Run code for cancel
Exit Select
End Select
End Sub
-
Sep 11th, 2009, 03:29 PM
#8
Re: [RESOLVED] openfiledialog question
Take the close out of the catch block as it also runs in the finally block.
What comes up twice? The dialog form?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 12th, 2009, 01:59 PM
#9
Member
Re: [RESOLVED] openfiledialog question
 Originally Posted by RobDog888
What comes up twice? The dialog form?[/color]
Yes.
-
Sep 12th, 2009, 02:07 PM
#10
Re: [RESOLVED] openfiledialog question
Hey,
Rather than explicitly call the Close method on the connection, you might think about wrapping the Connection and Command objects in Using statements, that way they will automatically closed before they go out of scope.
On another note, are you meaning to call the OpenFileDialog, and the straight away call a MessageBox, that seems quite unintuitive to me. Are you not actually wanting to call the OpenFileDialog then, based on whether the user hits Ok or Cancel perform some action?
What you are doing just now is, display the OpenFileDialog, then, regardless of what this click on there, you are then opening a MessageBox, and then doing an action based on what they press.
Is that actually what you want?
Gary
-
Sep 14th, 2009, 12:18 PM
#11
Member
Re: [RESOLVED] openfiledialog question
Hello,
I want to open the openfiledialog, choose an excel file, and open it.
I have done several tries with different codes, but when I choose my file,
(sometimes the data loads to datagridview, sometimes not) the openfiledialog opens again.
With the code I posted, the openfiledialog does it only two times. With other codes I just stopped counting.
I don't want the messege box, with "OK" or "Cancel".
I just want to understand what is happenning.
If I figure out I will do it other way
Edit. For some reason I cannot leave " Private Sub cmdLoad_Click", if it don't executes the hole code twice.
I placed an "Exit Sub", but....
Code:
Finally
MyConn.Close()
MyConn = Nothing
MyComm = Nothing
dtAdapter = Nothing
dtSet = Nothing
End Try
Exit Select
Case Windows.Forms.DialogResult.Cancel
'Run code for cancel
Exit Select
Exit Sub
End Select
Exit Sub
End Sub
Last edited by manin; Sep 14th, 2009 at 12:39 PM.
-
Sep 15th, 2009, 01:14 AM
#12
Re: [RESOLVED] openfiledialog question
Hey,
Let's see if we can break this down slightly for you.
This line:
Causes an OpenFileDialog to appear. When this window appears, the end user has two options.
1) Select a file to open and then hit OK, which will return a DialogResult of OK.
2) Hits the Cancel button, which will return a DialogResult of Cancel.
Now this line:
Code:
Select Case MessageBox.Show("text", _
"MMMMMMmmmmmm", _
MessageBoxButtons.OKCancel)
Will cause a MessageBox to appear, where the user again, has the option to hit OK and Cancel, which would return a value of DialogResult.OK or DialogResult.Cancel respectively.
You are currently switching your logic based on the result of the MessageBox.Show line, which in my opinion, based on what you have described, is not what you want to do.
Instead, you want to switch on the result of the ShowDialog line. Something like:
Code:
Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click, cmdLoad.Click
Dim ofd As New OpenFileDialog
Dim FileName As String
Dim sr As StreamReader
ofd.InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
ofd.Filter = "Excel Files|*.xls|Excel 2007 Files|*.xlsx"
ofd.DefaultExt = "*.xls"
Select Case ofd.ShowDialog()
Case Windows.Forms.DialogResult.OK
'Run code for acceptance
Try
MyConn.Open()
MyComm = New OleDbCommand("select * from [TZOKER$]", MyConn)
dtAdapter = New OleDbDataAdapter(MyComm)
dtAdapter.Fill(dtSet, "TZOKER")
For Each dtRow As DataRow In dtSet.Tables(0).Rows
'sum = dtRow("N1") + dtRow("N2") + dtRow("N3") + dtRow("N4") + dtRow("N5")
'code to do something with the 'sum' value
Next
dgViewSample.DataSource = dtSet.Tables(0)
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MyConn.Close()
MyConn = Nothing
MyComm = Nothing
dtAdapter = Nothing
dtSet = Nothing
End Try
Exit Select
Case Windows.Forms.DialogResult.Cancel
'Run code for cancel
Exit Select
End Select
End Sub
However, what is confusing me, is that you don't seem to use the excel file anywhere in the rest of the code that you have pasted. So where is your code to look at the excel spreadsheet? Have you hardcoded the link to the spreadsheet in your connection string?
Gary
-
Sep 15th, 2009, 11:01 AM
#13
Member
Re: [RESOLVED] openfiledialog question
TZOKER is the sheet of my excel file.
The data of the file, are loaded corectly to the datagridview.
Unfortunatly after the data are previewed to the datagridview, the openfiledialog open appears again.
This is what I can't understand. The messagebox, is another try to avoid the multiple appering of datagridview.
-
Sep 15th, 2009, 11:14 AM
#14
Re: [RESOLVED] openfiledialog question
Hey,
Search your code for ShowDialog.
I can't see any reason, based on the code you have shown why it would be showing twice.
Gary
-
Sep 15th, 2009, 11:22 AM
#15
Member
Re: [RESOLVED] openfiledialog question
Neither do I.
I just deleted and write it again. I was at the begining, so it was easy.
Now it's all ok.
Thanks
-
Sep 15th, 2009, 11:29 AM
#16
Re: [RESOLVED] openfiledialog question
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|