By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

Disabling a Button based off a Variable in a form

P: 4
Here's the code I am using:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  3.  
  4.     End Sub
  5.  
  6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.  
  8.     End Sub
  9.  
  10.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  11.  
  12.     End Sub
  13.  
  14.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  15.  
  16.     End Sub
  17.  
  18.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  19.  
  20.     End Sub
  21.  
  22.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  23.  
  24.     End Sub
  25.  
  26.     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
  27.  
  28.     End Sub
  29.  
  30.     Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
  31.  
  32.     End Sub
  33. 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!
Nov 2 '12 #1

✓ answered by Rabbit

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.

Share this Question
Share on Google+
7 Replies


Rabbit
Expert Mod 10K+
P: 12,382
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.
Nov 2 '12 #2

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

Expand|Select|Wrap|Line Numbers
  1.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim fileReader As String
  3.         fileReader = My.Computer.FileSystem.ReadAllText("C:\testing.txt")
  4.         MsgBox(fileReader)
  5.         If fileReader = "BRONZE" Then
  6.             Button1.Enabled = False
  7.         End If
  8.         RunClass1()
  9.     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 "Declaration 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.
Nov 2 '12 #3

PsychoCoder
Expert Mod 100+
P: 465
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
Nov 3 '12 #4

P: 4
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.
Nov 3 '12 #5

Rabbit
Expert Mod 10K+
P: 12,382
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.
Nov 3 '12 #6

PsychoCoder
Expert Mod 100+
P: 465
Then read the value in the forms Load Event
Nov 3 '12 #7

P: 4
Thank you both of you. I got it with:

Public Sub Form_Load() Handles MyBase.Load
Nov 3 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.