Results 1 to 11 of 11

Thread: TextBox problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    TextBox problem

    I made projects with two forms, and when I click button in Form1 it's OK, text in textbox1 in Form2 is OK, but when I close Form2 and open Form2 second time there isn't text.

    Here it is project:

    http://www.vbforums.com/attachment.p...1&d=1269722422

    Thanks.
    Attached Files Attached Files

  2. #2
    Addicted Member DramaQueen's Avatar
    Join Date
    Mar 2010
    Posts
    187

    Re: TextBox problem

    Well I didn't download your project, but I'm guessing you're actually closing your Form2 instead of hiding it, which causes the instance of Form2 to disappear and therefore a new one to be created when you open it again. If you're using...
    vb Code:
    1. Form2.Close()
    Change it to...
    vb Code:
    1. Form2.Hide()
    And to show it again...
    vb Code:
    1. Form2.Show()

  3. #3
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: TextBox problem

    Clarify your question a bit more.

    You have a TextBox on Form2 and you want to retain the text that is in it even after the form is closed? Do you want that text to load with the program every time it is run or clear the TextBox when the program is closed?

    To have it maintain the text only while the program is running (not after it is closed and re-opened), just declare a global String variable in the main class (Form1) that holds the value of the Textbox and on Form2's Load event, just set the TextBox's Text property to that variable.

    To maintain the text between multiple program executions (run it, close it, run it again), you could write that text to a text file, a database, or save it in your My.Settings.

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

    Re: TextBox problem

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Form2.Show()
    5.     End Sub
    6.  
    7. end Class

    vb Code:
    1. Public Class Form2
    2.  
    3.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         TextBox1.AppendText("Some text...")
    5.     End Sub
    6.  
    7. End Class

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

    Re: TextBox problem

    or you can do it this way:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         dim frm as new form2("Some text...")    
    5.         frm.Show()
    6.     End Sub
    7.  
    8. end Class

    vb Code:
    1. Public Class Form2
    2.  
    3.     Public Sub New(ByVal txt As String)
    4.         InitializeComponent()
    5.         TextBox1.Text = txt
    6.     End Sub
    7.  
    8. End Class

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Re: TextBox problem

    I tried to write: Form2.Hide

    but, the same thing happened, I don't know how to describe my problem better, that's why I posted complete project.

  7. #7
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: TextBox problem

    When he closes Form2, it'll clear the TextBox each time though. If the text in the TextBox is dynamic (changes), hard coding it with the AppendText won't be ideal.

    He could do something like:

    TextBox1.AppendText(myText) 'myText being a global string variable

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

    Re: TextBox problem

    ok. to make your text persistent in form2, do this:

    1/ select your textbox in your design time form
    2/ expand (ApplicationSettings) in your properties window
    3/ select (PropertyBindings) + click the ellipsis to the right
    4/ select the text property in the dialog + click the dropdown arrow to the right
    5/ click New in the dropdowm menu + type in a name for the property in the New Application Setting window
    6/ click OK on the New Application Setting window
    7/ click OK on the Application Settings window

    then use this code:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         dim frm as new form2("Some text...")    
    5.         frm.Show()
    6.     End Sub
    7.  
    8. end Class

    vb Code:
    1. Public Class Form2
    2.  
    3.     Public Sub New(ByVal txt As String)
    4.         InitializeComponent()
    5.         TextBox1.appendText(txt)
    6.     End Sub
    7.  
    8. End Class

  9. #9
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: TextBox problem

    why would he need that code if it's bound to an ApplicationSetting?
    When form2 loads, it will be bound already and will get populated with the text.

    He shouldn't need any code at that point if he's doing it that way.

    I'd use a User Setting though instead of ApplicationSetting as he may want to change the text

    Form's load event he would do:

    Form2TextBox1.Text = My.Settings.myText

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Re: TextBox problem

    Thank you for yours answers, but I didn't solve problem.
    Maybe I would help you more If I tell you where I would use this.
    I need log file. I have, let's say 10 forms in my project, and when I do something in any form I want to put text in some other form (where is it log file), so I want to control text in one form from all other forms in projects.
    I hope that's clear now.

  11. #11
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: TextBox problem

    i've already said, the easiest way is to declare a Class-level variable in your main form. this gives your whole project (every form) access to the variable and its value.

    if you don't understand that, research "class-level variables vb.net" and you will find your answer.

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