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..
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
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."
Bookmarks