none
Saving A Form's Text in all TextBox's Visual Basic 2010 Express

    Question

  • Hi Good People,

    Can you Please Tell me if I have this code right, When I click a Button I what to save all the textBox's text on my form. The code I have Is Below.

    In My settings Have I got to List each textBox. Like TextBox1, TextBox2, TextBox3. ect..

    IE: My.setting. textBox1 = TextBox1.text (In settings TextBox1: have it as string: User)

    IE: My.setting. textBox2 = TextBox2.text (In settings TextBox2: have it as string: User)

    Or can I have just one TextBox In my settings, that save's all the TextBox's 1 to 8 on my form

    Please point me in the right direction

    Kind Regards

    Gary

    Public Class YourDetailsForm
    
    
    
     Private Sub YourDetailsForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
      TextBox1.Text = My.Settings.Textbox
    
      ReflectionLabel1.Text = My.Settings.ReflectionLable1
    
     End Sub
    
    
    
     Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
    
    
    
      My.Settings.Textbox = TextBox1.Text
    
      My.Settings.Textbox = TextBox2.Text
    
      My.Settings.Textbox = TextBox3.Text
    
      My.Settings.Textbox = TextBox4.Text
    
      My.Settings.Textbox = TextBox5.Text
    
      My.Settings.Textbox = TextBox6.Text
    
      My.Settings.Textbox = TextBox7.Text
    
      My.Settings.Textbox = TextBox8.Text
    
      My.Settings.Save()
    
      SelectionMenuForm.Show()
    
      Me.Hide()
    
    
    
    
    
     End Sub
    
    
    Thursday, March 10, 2011 2:34 PM

Answers

  • if you want to find out if it works in a button click event - try it! That is what the debugger is for :)

    but, yes, you could do it in a button click event.

     2 problems with your code, though. Collections in .NET are zero-based - meaning the indexes start at 0, not 1 (look at the code I posted above)

    when you add the TextBox text to the collection, you need to put all your textboxes - my example was only for 2

    My.Settings.tbText.AddRange(New String() {TextBox1.Text, TextBox2.Text,TextBox3.Text,...TextBox8.Text})
    
    
    

     

    • Marked as answer by Gary Simpson Thursday, March 10, 2011 4:13 PM
    Thursday, March 10, 2011 4:08 PM

All replies

  • "My.Settings.Textbox" is going to be overwritten over and over again. It will finish the routine containing the contents of TextBox8, which will be saved.

     

    You will need to create a separate entry for each TextBox.

    Thursday, March 10, 2011 3:33 PM
  • you can create one Setting of type SpecializedStringCollection and use it to save all. Note that you have to have at least one value in it.

    try this in an app with 2 textboxes. Create a setting named tbText, type SpecializedStringCollection. Click on the value field and in the text field press enter twice. This will populate it with 2 values - a string of 1 space. Type something in the 2 textboxes,then close the form.When you run it again, the text should be in each textbox

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
      My.Settings.tbText.Clear()
      My.Settings.tbText.AddRange(New String() {TextBox1.Text, TextBox2.Text})
    
     End Sub
    
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
      TextBox1.Text = My.Settings.tbText(0)
      TextBox2.Text = My.Settings.tbText(1)
    
     End Sub
    
    Thursday, March 10, 2011 3:36 PM
  • Hi Frosty840

    Thank you for getting back to me, In my settings do I have to List Each TextBox

    Best Regards

    Gary

     


    Gary Simpson
    Thursday, March 10, 2011 3:39 PM
  • Hi Frosty840

    Should it be like this then

     

    Public Class YourDetailsForm

     Private Sub YourDetailsForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

      TextBox1.Text = My.Settings.Textbox

      ReflectionLabel1.Text = My.Settings.ReflectionLable1

    End Sub


     Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click



      My.Settings.Textbox1 = TextBox1.Text

      My.Settings.Textbox2 = TextBox2.Text

      My.Settings.Textbox3 = TextBox3.Text

      My.Settings.Textbox4 = TextBox4.Text

      My.Settings.Textbox5 = TextBox5.Text

      My.Settings.Textbox6 = TextBox6.Text

      My.Settings.Textbox7 = TextBox7.Text

      My.Settings.Textbox8 = TextBox8.Text

      My.Settings.Save()

      SelectionMenuForm.Show()

      Me.Hide()


     End Sub


    Gary Simpson
    Thursday, March 10, 2011 3:47 PM
  • if you want to find out if it works in a button click event - try it! That is what the debugger is for :)

    but, yes, you could do it in a button click event.

     2 problems with your code, though. Collections in .NET are zero-based - meaning the indexes start at 0, not 1 (look at the code I posted above)

    when you add the TextBox text to the collection, you need to put all your textboxes - my example was only for 2

    My.Settings.tbText.AddRange(New String() {TextBox1.Text, TextBox2.Text,TextBox3.Text,...TextBox8.Text})
    
    
    

     

    • Marked as answer by Gary Simpson Thursday, March 10, 2011 4:13 PM
    Thursday, March 10, 2011 4:08 PM
  • Hi jwavila.

    I have just thought of another problem that Might occur.

    what if I have two or more forms with TextBox's, would the code Interfere with form two or form three's TextBox's

    Best Regards

    Gary 


    Gary Simpson
    Thursday, March 10, 2011 4:52 PM
  • Hi gary

    sorry, didn't realize you had posted again.

    no, because you have those methods (the buttonX1 click event and YourDetailsForm load event) within the YourDetailsForm class. So they will only apply to that Form.

    it should not affect the textboxes on another Form.

    Thursday, March 10, 2011 7:10 PM