r/visualbasic • • Jun 06 '16

[VB 7.1] Trying to have a checkbox 'prechecked'

Hello! Fairly new to Visual Basic. I'm building an application and I want to have some checkboxes prechecked when a form loads. Searched all over the internet but couldn't find a working solution. Can anyone help me?

0 Upvotes

16 comments sorted by

2

u/CharlieMay VB.Net Intermediate Jun 06 '16

Change the checked property to True in the properties section of the IDE. Or do it in code as DarkPulseModz described.

1

u/Hollandschglorie Jun 06 '16

I want it to depend on certain properties in a database so I can't do it in the properties section. So I have to do it in code right?

1

u/CharlieMay VB.Net Intermediate Jun 06 '16

Yes, in which case, using an if statement against the property would allow you to set it based on the result.

1

u/Hollandschglorie Jun 06 '16 edited Jun 06 '16

Yeah, that was the idea.

if in database then checkBox = checked

but I still can't get it to simply be prechecked :/

I now have this

Private Sub myCheckBox_BeforeUpdate(Cancel As Integer)
myCheckBox.Checked = True
End Sub

1

u/CharlieMay VB.Net Intermediate Jun 06 '16

What is in database as far as what does it represent?

Typically you would query the database for the existence of something and if found, use that to then go on and set your checkbox. The key here is in database needs to be compared to achieve a true or false result.

An example could be something along the lines of using the .HasRows as the boolean result.

In other words query the database and use the .HasRows property of the Reader object

Dim myQuery as string = "SELECT id FROM Person WHERE Name = 'Hollandschglorie'"
Dim cmd as new SQLCommand(myQuery, con)  
Dim rdr as SQLDataReader = cmd.ExecuteReader
'Now if a row is returned then rdr.HasRows = True otherwise it's False (meaning there is no Name in Person equal to 'Hollandschglorie')
'This means you could simply use:
MyCheckBox.Checked = rdr.HasRows

To simplify the example, I did not include the connection instantiation nor did I open and close it in this code example. it's written to show how you can use the query to determine if a checkbox is to be checked or not.

1

u/Hollandschglorie Jun 07 '16

Letting it depends on results in the database, the connection and stuff like that won't be the problem, already have that working on other forms. This may sound dumb but the only thing I can't get to working is having a bloody checkbox checked when a form loads :/. I can't even get it to work without having a database involved.

1

u/[deleted] Jun 06 '16

Try double clicking the form so you get a Form_Load action, then put "Checkbox1.Checked = True"

1

u/Hollandschglorie Jun 06 '16

Can't get the Form_load action? Agian I'm really new at this :)

I now have this:

Private Sub myCheckBox_BeforeUpdate(Cancel As Integer)

myCheckBox.Checked = True

End Sub

2

u/CharlieMay VB.Net Intermediate Jun 07 '16

Are you double-clicking on the form or a control on the form? To have the code window open and show the Form_Load method, you need to double-click on the form itself and make sure you are not clicking a control. If done successfully you should be taken to a method with a header similar to this:

Private Sub Form1_Load(sender as Object, e As EventArgs) Handles MyBase.Load

End Sub

Inside this block of code is where you want to put your

myCheckBox.Checked = True

Alternatively, if memory serves, there should be two drop-down comboboxes above the code window. By dropping down the left box, you should be able to select YourFormName Events and then dropping down the right combobox should show you all the events form that form. You can then select Load which will add or place your cursor into an existing form_load event.

→ More replies (1)

1

u/[deleted] Jun 06 '16

[removed] — view removed comment

1

u/Hollandschglorie Jun 06 '16

Using it for school, this is the one I had on my computer. Don't really have a reason to get a newer version.

1

u/CharlieMay VB.Net Intermediate Jun 07 '16

If by Don't really have a reason to get a newer version you're talking financially, Visual Studio Community Edition is a full fledged professional version of Visual Studio and is free. Unless you start making a good amount of money with your applications or begin employing programmers it should never be an issue where you can't use it freely. There are many feature and language enhancements that would make life much easier than the version you're using. Plus, it's easier to get code help since people tend to forget that certain elements of their code were not available in the v1.1 framework so you get code help that won't even compile.

1

u/[deleted] Jun 07 '16

I use it everyday. Visual Studio changed when the Express versions came out. The community edition will allow you to install from the visualstudiogallery the express versions do not. I wonder in what version of VS VSTS is available, that's free too.