Results 1 to 19 of 19

Thread: [RESOLVED] Save to text and open text

  1. #1

    Thread Starter
    Member yacky's Avatar
    Join Date
    Nov 2009
    Posts
    38

    Resolved [RESOLVED] Save to text and open text

    Hi,

    I have combobox and 2 buttons (Save and open button).

    When I click Save button I want combobox.text save in .txt file, and when I click Open button I want text from .txt file copy to combobox.

    Thanks for help!!!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Save to text and open text

    vb Code:
    1. 'save items
    2. Dim items(ComboBox1.Items.Count - 1) As String
    3. ComboBox1.Items.CopyTo(items, 0)
    4. IO.File.WriteAllLines("yourtextfile.txt", items)

    vb Code:
    1. 'reload items
    2. ComboBox1.Items.AddRange(IO.File.ReadAllLines("yourtextfile.txt"))

  3. #3

    Thread Starter
    Member yacky's Avatar
    Join Date
    Nov 2009
    Posts
    38

    Re: Save to text and open text

    not working...

    I want to make these buttons using SaveFileDialog and OpenFileDialog...

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Save to text and open text

    vb Code:
    1. dim ofd as new openfiledialog
    2.  
    3. if ofd.showdialog = dialogresult.ok then
    4.     'reload items
    5.     ComboBox1.Items.AddRange(IO.File.ReadAllLines(ofd.filename))
    6. end if

    savefiledialog works the same way

  5. #5

    Thread Starter
    Member yacky's Avatar
    Join Date
    Nov 2009
    Posts
    38

    Re: Save to text and open text

    vb Code:
    1. Dim ofd As New OpenFileDialog
    2.         ofd.InitialDirectory = "C:\"
    3.         ofd.FileName = ""
    4.         ofd.Filter = "Text Files (*.txt)|*.txt"
    5.         ofd.Title = "Open"
    6.         ofd.ShowDialog()
    7.  
    8.         If ofd.showdialog = DialogResult.OK Then            
    9.  
    10.             ComboBox1.Items.AddRange(IO.File.ReadAllLines(ofd.filename))
    11.  
    12.         End If

    not working :S

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Save to text and open text

    Quote Originally Posted by yacky View Post
    not working :S
    not working, doesn't tell me what the problem is. in what way isn't it working?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Save to text and open text

    first remove the first ofd.ShowDialog()

  8. #8

    Thread Starter
    Member yacky's Avatar
    Join Date
    Nov 2009
    Posts
    38

    Re: Save to text and open text

    when I click open nothing is happening...

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Save to text and open text

    can you post the full open button code?

  10. #10

    Thread Starter
    Member yacky's Avatar
    Join Date
    Nov 2009
    Posts
    38

    Re: Save to text and open text

    vb Code:
    1. Dim ofd As New OpenFileDialog
    2. ofd.InitialDirectory = "C:\"
    3. ofd.FileName = ""
    4. ofd.Filter = "Text Files (*.txt)|*.txt"
    5. ofd.Title = "Open"
    6. ofd.ShowDialog()
    7.  
    8.         If ofd.showdialog = DialogResult.OK Then            
    9.  
    10.             ComboBox1.Items.AddRange(IO.File.ReadAllLines(ofd.filename))
    11.  
    12.         End If

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Save to text and open text

    i meant to include:

    vb Code:
    1. private sub button1_click 'etc

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Save to text and open text

    also. as i said earlier, in your code in post #10, remove line 6

  13. #13

    Thread Starter
    Member yacky's Avatar
    Join Date
    Nov 2009
    Posts
    38

    Re: Save to text and open text

    vb Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         Dim ofd As New OpenFileDialog
    3.         ofd.InitialDirectory = "C:\"
    4.         ofd.FileName = ""
    5.         ofd.Filter = "Text Files (*.txt)|*.txt"
    6.         ofd.Title = "Open"
    7.  
    8.         If ofd.showdialog = DialogResult.OK Then
    9.  
    10.             ComboBox1.Items.AddRange(IO.File.ReadAllLines(ofd.filename))
    11.  
    12.         End If
    13.     End Sub
    14. End Class

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Save to text and open text

    does the openfiledialog appear?
    have you tried opening the combobox dropdown?

  15. #15

    Thread Starter
    Member yacky's Avatar
    Join Date
    Nov 2009
    Posts
    38

    Re: Save to text and open text

    Yes, ofd appear. I tried opening combobox dropdown but nothing there...

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Save to text and open text

    ok open the same text file in notepad + tell me what it contains

  17. #17

    Thread Starter
    Member yacky's Avatar
    Join Date
    Nov 2009
    Posts
    38

    Re: Save to text and open text

    You helped me now...it contains nothing...

    Now text is copied in dropdown...can I copy text in combobox but not in dropdown(if you know what I mean) .

    And second problem is Save Button:

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim SD As New SaveFileDialog()        
    3.  
    4.         SD.InitialDirectory = "C:\"
    5.         SD.Filter = "Text Files (*.txt)|*.txt"
    6.         SD.Title = "Save Page To Bookmark"
    7.         SD.FileName = ""
    8.         SD.ShowDialog()
    9.  
    10.         Dim items(ComboBox1.Text.Count - 1) As String
    11.         ComboBox1.Items.CopyTo(items, 0)
    12.         IO.File.WriteAllLines(SD.FileName, items)
    13.  
    14.     End Sub

    It gives me error here:
    vb Code:
    1. IO.File.WriteAllLines(SD.FileName, items)

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Save to text and open text

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim SD As New SaveFileDialog()                
    3.     SD.InitialDirectory = "C:\"        
    4.     SD.Filter = "Text Files (*.txt)|*.txt"        
    5.     SD.Title = "Save Page To Bookmark"        
    6.     SD.FileName = ""        
    7.     if SD.ShowDialog = DialogResult.OK Then      
    8.         io.file.WriteAlltext(SD.FileName, combobox1.text)    
    9.     end if
    10. End Sub

  19. #19

    Thread Starter
    Member yacky's Avatar
    Join Date
    Nov 2009
    Posts
    38

    Re: Save to text and open text

    You really helped me...
    Thank you!!

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