473,387 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Disabling a Button based off a Variable in a form

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.

7 1771
Rabbit
12,516 Expert Mod 8TB
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
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
465 Expert Mod 256MB
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
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
12,516 Expert Mod 8TB
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
465 Expert Mod 256MB
Then read the value in the forms Load Event
Nov 3 '12 #7
Thank you both of you. I got it with:

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

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

Similar topics

1
by: Billy Jacobs | last post by:
Is there a way to make a button on a web form the default button so that when the user hits the enter button the code for that button executes? I have a web page Login.aspx where I have only 1...
1
by: John Bailo | last post by:
I was thinking: a cool thing would be a time based variable. So, I'm always doing things like: (a) Set variable to value (b) do something (c) check variable and compare to original.
3
by: Carl | last post by:
Hi, I have a from called frmBatchUpdate. On it I have created 10 text boxes in one column txtTRN1, txtTRN2 etc.. (Tenancy Reference Numbers) and another 10 to the right txtRPA1, txtRPA2 etc.....
3
by: CroDude | last post by:
Hi all! I've made a button from scratch derived from a Control class. The main reason why Control and not Button class is used for inheritance is that when deriving from a control class it's...
1
by: David Hearn | last post by:
I have a javascript function that is fired on the KeyDown event of a textbox. I am adding the attribute to the textbox at page load to run this javascript function. The javascript runs as expected...
7
by: Marco Castro | last post by:
How can I disable and then re-enable the close button on a form? Thanks.
8
by: helpful sql | last post by:
Hi, I have 2 radio buttons on my Windows form control. The radio button's CheckedChanged event disables or enables other controls on the form based on the value of the Checked property. When the...
3
by: gazelle04 | last post by:
Which happens first AfterUpdate event of a form or Clicked event of a button on a form. I have these codes on an AfterUpdate of a form: If UpdateDone = True Then ...
5
by: freelancex | last post by:
Hi, Im new to this forum, and new to scripting in general. Im an ICT Technician for a high school and i am designing a Form in visual basic 2005. The intended purpose of the Form, is to prevent...
3
by: extrym | last post by:
I have 2 forms, "Staff" and "Security" (without the ""'s). The Staff form has the following fields: staff_ID staff_name staff_number staff_DOB priority The Security form contains the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.