locked
Can someone help me fix my code? RRS feed

  • Question

  • I’m new to this whole visual basics thing and I am trying to make a text in a textbox show when I click a button and then make the same text reappear when I press the same button. Please help me fix this. Thank you!

    The code I put that doesn’t work is: 
    Private Sub Button9 Click (sender As System.Object, e As System.EventArgs) Handles Button9.Click 

    If TextBox1.Text = Visible=True Then
       TextBox1.Text = Visible = False 
    Else
        TextBox1. Text = Visible = True
    End if 

    End sub
    End class

    Saturday, September 12, 2020 1:57 AM

All replies

  • I’m new to this whole visual basics thing and I am trying to make a text in a textbox show when I click a button and then make the same text reappear when I press the same button. 

    One way:

    Static Dim str As String = TextBox1.Text
    If TextBox1.Text = str Then
        TextBox1.Text = ""
    Else
        TextBox1.Text = str
    End If
    

    - Wayne

    Saturday, September 12, 2020 5:02 AM


  • The code I put that doesn’t work is: 
    Private Sub Button9 Click (sender As System.Object, e As System.EventArgs) Handles Button9.Click 

    If TextBox1.Text = Visible=True Then
       TextBox1.Text = Visible = False 
    Else
        TextBox1. Text = Visible = True
    End if 

    End sub

    You will get an error from your code as TextBox1.Text is a String and
    Visible is a Boolean Property. So the expression:

    If TextBox1.Text = Visible = True

    is invalid as it attempts to compare (or assign) a Boolean to a String.

    You would get the same error with the reduced statement:

    If TextBox1.Text = Visible

    With Option Strict off, your code will compile but will get an error at
    run time. With Option Strict on, your code will get both Intellisense and
    compile errors.

    The Visible property of controls is used to make the control itself visible
    or not. It's not used to make the *contents* of a control visible/invisible.
    For example, the following code will make TextBox1 appear and disappear from
    the Form.

    If TextBox1.Visible = True Then
        TextBox1.Visible = False
    Else
        TextBox1.Visible = True
    End If
    

    - Wayne

    Saturday, September 12, 2020 6:22 AM
  • Public Class Form1
        ' You want to make a higher-level memory field to contain the 
        ' hidden text if necessary
        Private HiddenText As String
    
        Private tbox As TextBox
        Private btn As Button
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.HiddenText = String.Empty
    
            Me.btn = New Button
            Me.btn.Dock = DockStyle.Top
            Me.btn.Text = "Hide textbox text"
            AddHandler Me.btn.Click, AddressOf Me.btn_click
            Me.Controls.Add(btn)
    
            Me.tbox = New TextBox
            Me.tbox.Dock = DockStyle.Top
            Me.Controls.Add(tbox)
    
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
            Me.tbox.Dispose()
            Me.btn.Dispose()
        End Sub
    
        Private Sub btn_click(ByVal sender As Object, ByVal e As EventArgs)
            If (Me.HiddenText <> String.Empty) Then
                Me.tbox.Text = Me.HiddenText
                Me.HiddenText = String.Empty
            Else
                Me.HiddenText = Me.tbox.Text
                Me.tbox.Text = String.Empty
            End If
        End Sub
    End Class


    Before you can learn anything new you have to learn that there's stuff you don't know.


    Saturday, September 12, 2020 2:33 PM
  • Hi

    I would say that you need to clarify things. 

    You say 'make a text in a textbox show when I click a button' and then go on to say 'make the same text reappear when I press the same button' is very ambiguous. 

    I read it to mean click a button to make text disappear then clcik same button again and make the same text re-appear again. 

    Beware,  the term disappear is not the same as 'clear the textbox'.- if the textbox is cleared (or emptied), then there is no text in the textbox, but, if the text has been made to disappear (ie make the text color the same as the background color), then the text is still in the textbox. 

    *

    Using

    Dim str As String = TextBox1.Text

    then str would contain nothing if the TextBox has been cleared, or, the same text if just made to 'disappear'.

    *

    A third idea is that you just want the button to clear the textbox if it contains a fixed string ,OR, set the text to the fixed string if it is not already set to that fixed string.

    *

    Many variations, so down to you now ...................... is it any of those, or, maybe another thing altogether?


    Regards Les, Livingston, Scotland

    Saturday, September 12, 2020 3:05 PM
  • Beware,  the term disappear is not the same as 'clear the textbox'.- if the textbox is cleared (or emptied), then there is no text in the textbox, but, if the text has been made to disappear (ie make the text color the same as the background color), then the text is still in the textbox.

    @Les:  This option did occur to me, but I figured such a function would only be useful with a password box and in that case you'd be toggling the plaintext vs password-char display property, not "hiding" the text. 

    The overall description implies to me that the OP is building a test or quiz type application, where you would click a button to show a hint or the answer in a textbox that is defaulted to empty or blank, aka hidden.

    If I'm right then any of these methods are fine - clearing the textbox would be better since just making the text invisible (same forecolor as backcolor) would make it so users could copy the text out to another program to see it without clicking the button - while clicking the button could be used to record that the hint or answer was given.


    Before you can learn anything new you have to learn that there's stuff you don't know.

    Saturday, September 12, 2020 3:21 PM
  • @ Andrew

    Yes, all sorts of interpretations are possible.  I usually expect such speculation to result in 'no - I didn't mean that' or similar.

    It really just amounts to the original poster being somewhat lackadaisical with their question.


    Regards Les, Livingston, Scotland

    Saturday, September 12, 2020 3:26 PM
  • Public Class Form1
        ' I think Wayne A King's first post came close, but still missed.
        ' You want to make a higher-level memory field to contain the 
        ' hidden text if necessary
    

    @Andrew

    > ' I think Wayne A King's first post came close, but still missed.

    As I'm never too old a dog to learn new tricks, could you elaborate on exactly
    how and what my suggestion "missed"? It does what I assumed the OP was trying
    to accomplish: it toggles the text in the textbox "on" and "off" with each
    button click.

    Bearing in mind that the OP said: "I’m new to this whole visual basics thing "
    I believe that the simplest solution will be the most effective in getting the
    OP started on the learning curve. More advanced techniques are best left
    for later IMHO.

    - Wayne

    Sunday, September 13, 2020 1:25 AM
  • Public Class Form1
        ' I think Wayne A King's first post came close, but still missed.
        ' You want to make a higher-level memory field to contain the 
        ' hidden text if necessary
    

    @Andrew

    > ' I think Wayne A King's first post came close, but still missed.

    As I'm never too old a dog to learn new tricks, could you elaborate on exactly
    how and what my suggestion "missed"? It does what I assumed the OP was trying
    to accomplish: it toggles the text in the textbox "on" and "off" with each
    button click.

    Bearing in mind that the OP said: "I’m new to this whole visual basics thing "
    I believe that the simplest solution will be the most effective in getting the
    OP started on the learning curve. More advanced techniques are best left
    for later IMHO.

    - Wayne

    Wow.  71,815 Forum Points and you still get butthurt by random nothing from an 8,355.  Post edited.  Cheeses plucking rice.


    Before you can learn anything new you have to learn that there's stuff you don't know.

    Sunday, September 13, 2020 7:58 AM
  • Always funny this kind of discussions. 

    If I look at your code, than the simple one could be

    Private Sub Button9_Click (sender As Object, e As EventArgs) Handles Button9.Click 
         TextBox1.Visible = Not Textbox1.Visible 
    End sub

    But that does remove the whole box

    If you want what you ask, you need to use the text and the fine VB keyword "static" (it is not in this meaning in other .Net languages)

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
            Static hiddenText As String
            Dim tempText As String = TextBox1.Text
            TextBox1.Text = hiddenText
            hiddenText = tempText
    End Sub


    Success
    Cor

    Monday, September 14, 2020 2:55 PM