Disabling a Button based off a Variable in a form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mathewrb
    New Member
    • Nov 2012
    • 4

    Disabling a Button based off a Variable in a form

    Here's the code I am using:

    Code:
    Public Class Form1
        Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    
        End Sub
    
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    
        End Sub
    
        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    
        End Sub
    
        Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    
        End Sub
    End Class
    Now, if I want to disable Button1 based off of a variable that is read from a *.txt file, how do I code that into this? I have a class that reads from the text file, and outputs the text file data correctly, but I don't know how to implement that here to do a Button1.Visible = False?

    Thanks for any help!
    Last edited by Meetee; Nov 2 '12, 04:51 AM. Reason: Please use code tags <code/> around your code
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    All I see is a bunch of function prototypes and no code within the functions. But assuming that there is code in there that you have left out, if you know how to read the data, then you just need to compare it in an if statement and turn off the visibility if it matches what you're looking for.

    Comment

    • mathewrb
      New Member
      • Nov 2012
      • 4

      #3
      Let me explain in a little more detail, assuming the following is the button I want to return false / true off a variable:

      Code:
          Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
              Dim fileReader As String
              fileReader = My.Computer.FileSystem.ReadAllText("C:\testing.txt")
              MsgBox(fileReader)
              If fileReader = "BRONZE" Then
                  Button1.Enabled = False
              End If
              RunClass1()
          End Sub
      How do I code it so that the button is disabled BEFORE the user clicks the button? (This code will disable the button AFTER the user clicks it, assuming the text in the file is BRONZE.) If I put the IF THEN statement outside of the Sub, I get the error "Declaratio n Expected." If I put the code inside of another Sub, it doesn't run without user interaction, like a button click. I'm sure this is something easy, I am just pretty new to VB.

      Comment

      • PsychoCoder
        Recognized Expert Contributor
        • Jul 2010
        • 465

        #4
        If you want it disabled to start then I would suggest setting it's Enabled Property to false to begin with, then when necessary enable it

        Comment

        • mathewrb
          New Member
          • Nov 2012
          • 4

          #5
          I need it to be disabled or enabled based off what is in that file, so depending if that file says on or off, I need the button to be enabled or disabled.

          The problem is I can't figure out how to run the IF THEN comparison without clicking a button first.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            You just need to choose the right event. When do you want to do the check?

            If you want to do the check as soon as the form is loaded, then use the form's load event. If you want to do it if they change a field, then you would use the field's update event. If it's some other event, then use that event.

            Comment

            • PsychoCoder
              Recognized Expert Contributor
              • Jul 2010
              • 465

              #7
              Then read the value in the forms Load Event

              Comment

              • mathewrb
                New Member
                • Nov 2012
                • 4

                #8
                Thank you both of you. I got it with:

                Public Sub Form_Load() Handles MyBase.Load

                Comment

                Working...