-
October 26th, 2012, 03:16 PM
#1
Problem displaying the savefiledialog while saving a file.
Hi,
I am trying to save to file to a location by browsing the SaveFileDialog. The saveFileDialog doesnot display at all. It is in the background. Any help is very much appreciated..
-
October 28th, 2012, 04:15 PM
#2
Re: Problem displaying the savefiledialog while saving a file.
Not without any CODE. We could GUESS what you did, but it would be RIGHT. Post what you did to see what was WRONG with it (hopefully)
-
October 29th, 2012, 10:09 AM
#3
Re: Problem displaying the savefiledialog while saving a file.
Below is the code: I have an OK button, on click I am calling the exportToFile method through a thread (STA). The SaveFileDialog doesnot display in the front. Everything else works just fine. PLease let me know if am wrong anywhere.
Code:
Protected Sub exportToFile(ByVal obj As Object)
Dim response As HttpResponse = CType(obj, HttpResponse)
'CREATING A FILE - Open or Create the file
Dim spc As SearchParentCompany = New SearchParentCompany
Dim dt As DataTable
Dim selectedItems As ArrayList = New ArrayList
Dim sb As New StringBuilder()
Dim delim As String = RadioButtonListDelimiter.SelectedValue
For Each item As GridItem In RadGrid3.MasterTableView.Items
If TypeOf item Is GridDataItem Then
Dim dataItem As GridDataItem = CType(item, GridDataItem)
Dim id As String = dataItem.OwnerTableView.DataKeyValues(dataItem.ItemIndex)("Store Code").ToString()
selectedItems.Add(id)
End If
Next
'iterate the arraylist to get the corresponding store information
For m = 0 To selectedItems.Count - 1
dt = (FileManager.GetParentCompanyStoreSearchExportFile(selectedItems(m).ToString))
If m = 0 Then
For i As Integer = 0 To dt.Columns.Count - 1
sb.Append(dt.Columns(i).ColumnName + RadioButtonListDelimiter.SelectedValue)
Next
End If
sb.Append(Environment.NewLine)
For j As Integer = 0 To dt.Rows.Count - 1
For k As Integer = 0 To dt.Columns.Count - 1
sb.Append(dt.Rows(j)(k).ToString() + RadioButtonListDelimiter.SelectedValue)
Next
Next
Next
sb.Append(Environment.NewLine)
Dim objSaveFileDialog As Windows.Forms.SaveFileDialog = New Windows.Forms.SaveFileDialog
With objSaveFileDialog
.DefaultExt = "txt"
.FileName = "PCSInfoFile.txt"
.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
.FilterIndex = 1
.OverwritePrompt = True
.Title = "Export File to"
End With
Dim FilePath As String
If objSaveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
response.Clear()
response.Write(sb.ToString())
FilePath = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, objSaveFileDialog.FileName)
My.Computer.FileSystem.WriteAllText(FilePath, sb.ToString, True)
End If
End Sub
Protected Sub BtnOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnOk.Click
RadGrid3.Visible = True
ExportDelimiterDiv.Visible = False
ExportDelimiterDiv.Style.Add("display", "none")
Dim newThread As Thread = New Thread(New ParameterizedThreadStart(AddressOf exportToFile))
newThread.IsBackground.Equals(False)
newThread.SetApartmentState(ApartmentState.STA)
newThread.Start(Response)
End Sub
Last edited by GremlinSA; October 29th, 2012 at 12:22 PM.
Reason: added code tags
-
October 29th, 2012, 11:46 AM
#4
Re: Problem displaying the savefiledialog while saving a file.
Try without the thread to see if you are on the right thread to begin with.
-
October 29th, 2012, 12:11 PM
#5
Re: Problem displaying the savefiledialog while saving a file.
Thanks for the response.
I did try without the thread and got the following error, also the result was the same, the saveFileDialog at displayed at the background:
"Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it. This exception is only raised if a debugger is attached to the process."
Thanks,
Chitra
-
October 29th, 2012, 07:47 PM
#6
Re: Problem displaying the savefiledialog while saving a file.
Do you get that exception on the line that opens the dialog ? As far as I know, that dialog has nothing to do with OLE.
-
October 29th, 2012, 08:16 PM
#7
Re: Problem displaying the savefiledialog while saving a file.
I get the exception on this line:
If objSaveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Thanks
-
October 29th, 2012, 08:27 PM
#8
Re: Problem displaying the savefiledialog while saving a file.
It didn't run without the thread. Hence the STA Error...
-
October 29th, 2012, 08:38 PM
#9
Re: Problem displaying the savefiledialog while saving a file.
Did you do anything to the main thread or the threading model of the application itself ?
-
October 30th, 2012, 09:32 AM
#10
Re: Problem displaying the savefiledialog while saving a file.
-
October 30th, 2012, 11:11 AM
#11
Re: Problem displaying the savefiledialog while saving a file.
Resolved! I created a dummy form and made it transparent, and topmost. The below code might help some one with same problem in future!
Thanks all for the help!!
With dummyform
.TopMost = True
.BringToFront()
.Opacity = 0%
.Visible = False
.WindowState = Windows.Forms.FormWindowState.Maximized
End With
make the form visible just before :
dummyform.visible = true
if .showdialog = dialogresult.ok then
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
|