CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    7

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

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Oct 2012
    Posts
    7

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Oct 2012
    Posts
    7

    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

  6. #6
    Join Date
    Apr 2012
    Posts
    43

    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.

  7. #7
    Join Date
    Oct 2012
    Posts
    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

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Problem displaying the savefiledialog while saving a file.

    It didn't run without the thread. Hence the STA Error...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  9. #9
    Join Date
    Apr 2012
    Posts
    43

    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 ?

  10. #10
    Join Date
    Oct 2012
    Posts
    7

    Re: Problem displaying the savefiledialog while saving a file.

    No nothing..

  11. #11
    Join Date
    Oct 2012
    Posts
    7

    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
  •  





Click Here to Expand Forum to Full Width

Featured