locked
OpenFileDialog1

    Question

  • Is there a way to autosort the streamreader of the OpenFileDialog1. If listbox sorted = false and the txt file that is imported is not in alphabetical order it autosort.
    I am using it in this manner if users already exist they are loaded in a textbox in this manner name1, name4, name2. Or would it be easier to set the txtbox to autosort?

    2nd once in awhile when OpenFileDialog1 is cancelled it leaves a temp file in filelocation.

    3rd is there a way to catch the cancel of OpenFileDialog1 event? to a label so if cancel is click label would refelect this?

     

      Private Sub ImportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImportToolStripMenuItem.Click  
            If ToolStripStatusLabel1.Text = "Scan in Progress." Then  
                MsgBox("Currently Scanning! Stop Scan to Add Users!", MsgBoxStyle.Question, Title:="Validator: Select User!")  
                Exit Sub  
            End If  
            ListBox2.Items.Clear()  
            ListBox3.Items.Clear()  
            ListBox6.Items.Clear()  
            ToolStripStatusLabel1.Text = "System Refreshing..." 
            ToolStripStatusLabel2.Text = "" 
            ToolStripStatusLabel3.Text = "" 
            ToolStripStatusLabel4.Text = "00:00:00" 
            Me.OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt" 
            Me.OpenFileDialog1.Title = "MyApp" 
            Me.OpenFileDialog1.FileName = "Scanlist" 
            Me.OpenFileDialog1.InitialDirectory = "C:\Documents and Settings\All Users\Desktop\Scanlist.txt" 
            Me.OpenFileDialog1.ShowDialog()  
            Dim fs As FileStream = New FileStream(Me.OpenFileDialog1.FileName, FileMode.OpenOrCreate)  
            Dim sw As StreamReader = New StreamReader(fs)  
            Dim s As String  
            Dim thisDuplicates As String = "" 
            Dim thisCount As String = "" 
            While sw.Peek > -1  
                s = sw.ReadLine.ToLower  
                s = Trim(s)  
                If s.Length > 0 Then  
                    If ListBox1.Items.Contains(s) = False Then  
                        ListBox1.Items.Add(s)  
                        Label3.Text = ListBox1.Items.Count  
                        ListBox3.Items.Add(s)  
                    Else  
                        ListBox2.Items.Add(s)  
                        's sort line goes here  
                        thisDuplicates += s & ", "  
                        thisCount = ListBox2.Items.Count  
                    End If  
                End If  
                lblStatCount.Text = CStr(ListBox1.Items.Count)  
            End While  
            sw.Close()  
            sw = Nothing 
            ToolStripStatusLabel1.Text = "~System Loaded~" 
            ToolStripStatusLabel2.Text = " User(s) Imported Successfully." 
            ToolStripStatusLabel3.Text = ListBox3.Items.Count  
            Label1.Focus()  
            If ListBox2.Items.Count = 0 Then  
                Exit Sub  
            Else  
                f.AddDups(thisDuplicates)  
                f.AddCount(thisCount)  
                f.ShowDialog()  
            End If  
            If ListBox1.Items.Count = 0 Then  
                Exit Sub  
            Else  
                ListBox1.SelectedIndex = (0)  
            End If  
        End Sub 
    Saturday, February 28, 2009 8:45 AM

Answers

  •  if users already exist they are loaded in a textbox

    ListBox or TextBox? Your code shows a ListBox. Sorting a TextBox requires Shell Sort code.

    edit: see here for more info:

    http://linux.wku.edu/~lamonml/algor/sort/shell.html

    1. easiest to set  ListBox2.Sorted = True

    2. Windows frequently leaves Temp files when programs close. Just happens. Suppose you could search that folder for any "*.tmp" files and delete.

    3.

    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then 
                Label1.Text = "Cancelled" 
            End If 

    • Edited by jwavila Saturday, February 28, 2009 3:58 PM edit
    • Marked as answer by Blackened_system32 Sunday, March 01, 2009 2:14 PM
    Saturday, February 28, 2009 3:54 PM
  • Let's try to straighten some of this out:

    1. change: Dim s As String to Dim s As String = ""
    2. right below the Dim s As String line, put ListBox2.Sorted = True
    3. in the Else clause where you're adding the items to ListBox2, only have the ListBox2.Items.Add(s)
    4. right below the Label1.Focus line, put the following (and remove them from where they are up above):
        Dim thisDuplicates As String = ""
        Dim thisCount As Integer = ListBox2.Items.Count
    5. in the Else clause where you are testing for ListBox2.Items.Count put right below the Else clause

            For i As Integer = 0 To ListBox2.Items.Count - 1        
                thisDuplicates = thisDuplicates & CStr(ListBox2.Items(i)) & ","        
            Next   
            thisDulpicates = thisDuplicates.Remove(thisDuplicates.Length-1,1)  
     
    this will set the thisDuplicates text to User1,User2,User3,  Notice there will be an extra , at the end, so we will strip that off.
    Also, set Option Strict and Option Explicit to On.




    Sunday, March 01, 2009 6:40 AM

All replies

  •  if users already exist they are loaded in a textbox

    ListBox or TextBox? Your code shows a ListBox. Sorting a TextBox requires Shell Sort code.

    edit: see here for more info:

    http://linux.wku.edu/~lamonml/algor/sort/shell.html

    1. easiest to set  ListBox2.Sorted = True

    2. Windows frequently leaves Temp files when programs close. Just happens. Suppose you could search that folder for any "*.tmp" files and delete.

    3.

    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then 
                Label1.Text = "Cancelled" 
            End If 

    • Edited by jwavila Saturday, February 28, 2009 3:58 PM edit
    • Marked as answer by Blackened_system32 Sunday, March 01, 2009 2:14 PM
    Saturday, February 28, 2009 3:54 PM
  • Took a lil bit of toying with it to get it from firing two openfiledialog boxs, so far in beta testing an modified to this an so far working fine: Had to remove  Me.OpenFileDialog1.ShowDialog() to get to work , odd but since that code line has been removed so far not getting temp file.

    If ToolStripStatusLabel1.Text = "Scan in Progress." Then  
                MsgBox("Currently Scanning! Stop Scan to Add Users!", MsgBoxStyle.Question, Title:="myapp: Select User!")  
                Exit Sub  
            End If  
            ListBox2.Items.Clear()  
            ListBox3.Items.Clear()  
            ListBox6.Items.Clear()  
            ToolStripStatusLabel1.Text = "System Refreshing..." 
            ToolStripStatusLabel2.Text = "" 
            ToolStripStatusLabel3.Text = "" 
            ToolStripStatusLabel4.Text = "00:00:00" 
            Me.OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt" 
            Me.OpenFileDialog1.Title = "Myapp" 
            Me.OpenFileDialog1.FileName = "Scanlist" 
            Me.OpenFileDialog1.InitialDirectory = "C:\Documents and Settings\All Users\Desktop\Scanlist.txt" 
            If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then  
                ToolStripStatusLabel1.Text = "~Import Cancelled~" 
                Exit Sub  
            Else  
     
                Dim fs As FileStream = New FileStream(Me.OpenFileDialog1.FileName, FileMode.OpenOrCreate)  
                Dim sw As StreamReader = New StreamReader(fs)  
                Dim s As String  
                Dim thisDuplicates As String = "" 
                Dim thisCount As String = "" 
                While sw.Peek > -1  
                    s = sw.ReadLine.ToLower  
                    s = Trim(s)  
                    If s.Length > 0 Then  
                        If ListBox1.Items.Contains(s) = False Then  
                            ListBox1.Items.Add(s)  
                            Label3.Text = ListBox1.Items.Count  
                            ListBox3.Items.Add(s)  
                        Else  
                            ListBox2.Items.Add(s)  
                            's sort line goes here  
                            thisDuplicates += s & ", "  
                            thisCount = ListBox2.Items.Count  
                        End If  
                    End If  
                    lblStatCount.Text = CStr(ListBox1.Items.Count)  
                End While  
                sw.Close()  
                sw = Nothing 
                ToolStripStatusLabel1.Text = "~System Loaded~" 
                ToolStripStatusLabel2.Text = " User(s) Imported Successfully." 
                ToolStripStatusLabel3.Text = ListBox3.Items.Count  
                Label1.Focus()  
                If ListBox2.Items.Count = 0 Then  
                    Exit Sub  
                Else  
                    f.AddDups(thisDuplicates)  
                    f.AddCount(thisCount)  
                    f.ShowDialog()  
                End If  
                If ListBox1.Items.Count = 0 Then  
                    Exit Sub  
                Else  
                    ListBox1.SelectedIndex = (0)  
                End If  
    End If  
     

    The dup sort goes into a second form that uses a textbox like this user1, user2, user3, an so on. I have looked at your link seems a lil bit confusing but trying to modify it to work in my app.

    Saturday, February 28, 2009 11:49 PM
  • If I read this right the duplicates are going into ListBox2 and to the string thisDuplicates at the same time, correct? Why not just add to the ListBox and use that to sort first ( with the ListBox2.Sorted = True). Then read each item from the ListBox and add to the string? Maybe in the Else clause in the If ListBox2.Items.Count... That way you only work on thisDuplicates if there is anything in ListBox2.

            Dim thisDuplicates As String = ""    
            For i As Integer = 0 To ListBox2.Items.Count - 1     
                thisDuplicates = thisDuplicates & CStr(ListBox2.Items(i)) & ","     
            Next     
     

    Then you can do whatever you want to that string.
    Sunday, March 01, 2009 12:56 AM
  • Yea prolly would be easier to do that, the reason they go into a listbox is to get the number count of dups if i used something like thiscount = thisdupliactes.count no matter how many users were dupped it say 7 for the count. Well off to implement your code post back later if worked, if works I mark your replies as answer
    Sunday, March 01, 2009 1:06 AM
  •   Me.OpenFileDialog1.InitialDirectory = "C:\Documents and Settings\All Users\Desktop\Scanlist.txt" 

    You shouldn't have the file name in that only the directory name . Set the filename separately Me.OpenFileDialog1.filename = "Scanlist.txt"

    Coding for fun
    Sunday, March 01, 2009 1:09 AM
  • jwavila I used your code an removed old it made form lockup an not respond. 

    "You shouldn't have the file name in that only the directory name . Set the filename separately Me.OpenFileDialog1.filename = "Scanlist.txt""


    By setting the intial directory it opens the openfiledialog to preset not directory not the last directory opened. By default it uses last file directory if ya had c:program file/my file everytime ya opened openfiledialog that would be the path if desktop was used that would be the default as well. By modifying by way I have done not matter how many times it is open it opens specific directory not last one used.  Or are your refering that this Me.OpenFileDialog1.InitialDirectory = "C:\Documents and Settings\All Users\Desktop\Scanlist.txt"  should be Me.OpenFileDialog1.InitialDirectory = "C:\Documents and Settings\All Users\Desktop\"
    Sunday, March 01, 2009 1:21 AM
  • Yes that is what I meant just the directory .
    Coding for fun
    Sunday, March 01, 2009 1:29 AM
  • I think maybe you can omit the filename and I think maybe it should be the whole path anyway not sure I usually don't try to preset the filename but setting the InitialDirectory just use the directory without the filename .
    Coding for fun
    Sunday, March 01, 2009 1:32 AM
  • When you said lock up and not respond, did you get any type of error? On which line did it lock up?
    A couple of things:
    1. you have thisCount as String = "", but later thisCount = ListBox2.Items.Count which is an integer. Is it supposed to be a String or Integer?
    2. move the thisCount out of the Else clause (really don't want it get the count until ALL items are added). Set it somewhere around the Label1.Focus.
    3. Only have this in  the Else clause for the sw:
      ListBox2.Items.Add(s)
      ListBox2.Sorted = True

    Sunday, March 01, 2009 1:57 AM
  •  1. you have thisCount as String = "", but later thisCount = ListBox2.Items.Count which is an integer. Is it supposed to be a String or Integer?

    Not sure it works, but works as string so.. maybe i fix that!

    I didnt get a error jus did nothing for while so click on form an say n me.text that not responding no kickout errors so had to end it by builder. I have to go work but I will try this later 2morow
    Sunday, March 01, 2009 2:20 AM
  • Let's try to straighten some of this out:

    1. change: Dim s As String to Dim s As String = ""
    2. right below the Dim s As String line, put ListBox2.Sorted = True
    3. in the Else clause where you're adding the items to ListBox2, only have the ListBox2.Items.Add(s)
    4. right below the Label1.Focus line, put the following (and remove them from where they are up above):
        Dim thisDuplicates As String = ""
        Dim thisCount As Integer = ListBox2.Items.Count
    5. in the Else clause where you are testing for ListBox2.Items.Count put right below the Else clause

            For i As Integer = 0 To ListBox2.Items.Count - 1        
                thisDuplicates = thisDuplicates & CStr(ListBox2.Items(i)) & ","        
            Next   
            thisDulpicates = thisDuplicates.Remove(thisDuplicates.Length-1,1)  
     
    this will set the thisDuplicates text to User1,User2,User3,  Notice there will be an extra , at the end, so we will strip that off.
    Also, set Option Strict and Option Explicit to On.




    Sunday, March 01, 2009 6:40 AM
  • Tested your edited code an works in beta tests, ty very much. Only thing I was unable to set Option Strict & Explicit to on as soon as I did an click debug showed 62 errors. I can post them if you want but w/o adding that explicit/strict on works fine so far. Also, I had modified/ omitted your code line ListBox2.Sorted = True since that can be set in designer properties of the Listbox.

    Exactly what does this code line do?    thisDulpicates = thisDuplicates.Remove(thisDuplicates.Length-1,1)  Is that to remove the extra , at end  ?? If so that doesn't work I in the duplicates form I added this code line  TextBox1.Text = TextBox1.Text.TrimEnd(",")
    Sunday, March 01, 2009 2:13 PM
  •   A suggestion: work with both Option Strict and Option Explicit ON. While it may not be a problem in this app, at some point it might cause a program to crash. As you see, none of those 62 errors caused it to not compile, so they are easy to fix. I would guess most of them are similar to this:

    Label3.Text = ListBox1.Items.Count  

    Labels only accept strings, but a ListBox.Items.Count is an integer. Option Strict prohibits this type of implicit conversion. You would need something like:

    Label3.Text = CStr(ListBox1.Items.Count)

    I saw some of those in your code, that was why I had you turn it ON. When you turn it on, each of those lines will have a blue squiggle under it and a red box on the right side. Move your cursor anywhere over where the blue squiggle is, and a box with an exclamation mark will pop-up. Move the cursor over the box and click on the down arrow. This will give you options on how to fix the error. In the above case it will say "Replace ListBox1.Items.Count with CStr(ListBox1.Items.Count)". Just click on the correction and it will fix it for you.

    Glad this is working now
    Sunday, March 01, 2009 3:54 PM