Visual Studio 'null' is not defined - vb.net

I am trying to learn Visual Studio with VB and started by using the Microsoft training by creating a Picture Viewer form.
Everything was going fine and I figured out how to use VB code which is slightly different than the examples in C#. I have most of the training done and can bring in a pictures from the app, but I am having trouble with the closeButton based on the code provided. I am at a loss after searching for many hours. The following is the code from the Open Picture and Close Code. As I was inputting the code from the interface there was no null so I typed it in and that didn't work.
Any help would be greatly appreciated. Thanks
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles showButton.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
End If
PictureBox1.Load(OpenFileDialog1.FileName)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles clearButton.Click
PictureBox1.Image = null
End Sub

Think if you change null to Nothing, you should be all good
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles clearButton.Click
PictureBox1.Image = Nothing
End Sub

Change the keyword null to nothing please .
For non-nullable value types, Nothing in Visual Basic differs from null in C#. In Visual Basic, if you set a variable of a non-nullable value type to Nothing, the variable is set to the default value for its declared type. If a variable is of a value type, the behavior of Nothing depends on whether the variable is of a nullable data type. If a variable is of a reference type, assigning Nothing to the variable sets it to a null reference of the variable's type.
See this document for more details and demo.

Related

How can I let a code run permanent in the background, which waits for an input ? (Visual Basic)

I want to program a round based rpg like game in visual basic.
For that I want to make it possible, to show a picture of the current selected unit in the right bottom corner and add some information about the heal points and some other stats.
So I want to make a program part, which permanently asks, which of the units is selected and based on that let a other program part running, which changes the picture and the information about hp and this stuff, matching to the current unit.
But I am not able to run a program which runs in the background and doesn't freezes the main program while running (may I need a background worker ?)
Also I am not very sure, what for a program type I need to used for this (like a sub or something).
I don't know other types than sub's in vb, but I could derive it from my knowledge of java programming (functions and objects).
I know this is a bit much to ask, but I need to know, which program type I need to use and how I can let it run permanent in the background + how to transfer some information between these parts.
Would be nice, if someone could help me and explain a bit instead of just saying something like: you need a background worker use this link xy.
I tried to use a variable, which becomes a different number, when a other unit is clicked, unfortunately you cant see much of my code right now, since for this form, I didn't created much more than the graphical interface.
Public Class FormBattlescreen
Dim marked As Integer = 0
Private Sub Formsounds_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
FormMainCampagne.Close()
End Sub
Private Sub OwnSoldier1_Click(sender As Object, e As EventArgs) Handles OwnSoldier1.Click
marked = 1
End Sub
Private Sub OwnSoldier2_Click(sender As Object, e As EventArgs) Handles OwnSoldier2.Click
marked = 2
End Sub
Private Sub OwnSoldier3_Click(sender As Object, e As EventArgs) Handles OwnSoldier3.Click
marked = 3
End Sub
Private Sub EnemyOfficer1_Click(sender As Object, e As EventArgs) Handles EnemyOfficer1.Click
marked = 4
End Sub
Private Sub RunGame()
Select Case marked
Case 0 'nothing is selected//at the beginning
Case 1
PictureBoxStats.Image = My.Resources.Soldier2
Case 2
Case 3
Case 4
End Select
End Sub
End Class
Thank you, I appreciate your help.

How do i update different table from the same access database using visual basic .net

I added my Enrollment system access Database, into my Enrollment System vb.net form, as a data source. The Database has 2 tables in it, the accountTable and studentEnrollmentInformation. I dragged The accountTable's details and data grid view into my form designer. The following code automatically appeared in the code designer:
Private Sub AccountTableBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.AccountTableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.EnrollmentSystemDataBaseDataSet)
End Sub
Private Sub enrollmentSystem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AccountTableTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.accountTable)
End Sub
The following code works for updating the accounTableDataGridView but it does not work for studentEnrollmentInformationDataGridView so i manually created one
for studentEnrollmentInformation.
Function updateStudent()
Me.Validate()
Me.StudentEnrollmentInformationBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.EnrollmentSystemDataBaseDataSet)
Me.StudentEnrollmentInformationTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.studentEnrollmentInformation)
End Function
This is the function that contains the update code, that i manually created for updating the studentEnrollmentDataGridView. Adding new Row works fine but when i try to update studentEnrollmentDataGridView the texts in the table disappears and does not update/save. I also had function for updating the accountTableDataGridView which works fine.
Function update() 'THIS FUNCTION CONTAINS PRE-MADE CODE TO MAKE UPDATING SHORTER IN WRITING CODE.
Me.Validate()
Me.AccountTableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.EnrollmentSystemDataBaseDataSet)
Me.AccountTableTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.accountTable)
End Function
My Question is how do i update multiple Tables in my system? Updating the other table works fine but the other is not.
In the original auto-generated code, this is the line that retrieves the data in the first place:
Private Sub enrollmentSystem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AccountTableTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.accountTable)
End Sub
When the form loads, the Account data is retrieved into a DataTable that is already bound. If you want to retrieve Student Enrollment data too, do it in the same place:
Private Sub enrollmentSystem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AccountTableTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.accountTable)
Me.StudentEnrollmentInformationTableAdapter.Fill(Me.EnrollmentSystemDataBaseDataSet.studentEnrollmentInformation)
End Sub
Now you're populating both bound DataTables when the form loads. When it comes to saving, you do the same thing, i.e. add the code to save the changes to the other DataTable where you already have the code to save the first:
Private Sub AccountTableBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.AccountTableBindingSource.EndEdit()
Me.StudentEnrollmentInformationBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.EnrollmentSystemDataBaseDataSet)
End Sub
You don't need any code to specifically save the changes from the DataTable because the whole point of UpdateAll is that it updates all DataTables in the DataSet.
As is always the case, if it doesn't seem to be working as you expect then you debug it. In that case, that would mean setting a breakpoint on the UpdateAll line and examining the exact state of the DataSet before and after the call, as well as possibly examing the sate of the database too.

Visual Basic GUI Input Validation

I took an entry level computer programming class this past term and I'm having problems with my final project. I have to design a program in visual basic GUI that asks the player to accurately guess a number between 1-100 within a limited number of guesses.
My first form asks the user to set the number of guesses allowed. It has one textbox and an "enter" button, among other buttons that I've gotten to work.
I'm trying to get code to work that will validate the input on the guesses allowed. Specifically, I want a message box to pop up if the player enters letters or special characters instead of numbers, or enters a number less than zero, or greater than twenty. Here's what I have:
Public Class Noofguesses
Shared maxguesscnt As Integer
Private Sub Numberofguesses_TextChanged(sender As Object, e As EventArgs) Handles Numberofguesses.TextChanged
End Sub
Private Sub Quit_Click(sender As Object, e As EventArgs) Handles Quit.Click
End
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form3.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Val(Numberofguesses) > 20 Then MsgBox("Number of Guesses Cannot Exceed 20")
If Val(Numberofguesses) < 0 Then MsgBox("Number of Guesses Must Be Greater Than 0")
If Not IsNumeric(Numberofguesses) Then MsgBox("Entry Cannot be Letters or Characters")
Me.Close()
Form2.Show()
End Sub
End Class
What am I doing wrong? Please let me know.
Thanks
I would generally suggest using a NumericUpDown rather than a TextBox, in which case no validation is required. As this is an assignment though, I'm guessing that validating a TextBox is a requirement. In that case, you should use Integer.TryParse to validate a String, i.e. the Text of the TextBox and convert it if it is valid. You can then test the converted value to make sure that it isn't less than zero, etc. I'm not going to write the code for you, given that this is homework, but that should be enough for you to do it yourself or, if you feel you must, find examples online.

VB.Net 2013 How do I programatically update a bindingsource without getting late binding errors/warnings?

I created a DataSet with a DataTable & TableAdapter that contains Select/Update/Insert/Delete commands that relate to my MSSQL database. The DataTable selects all the fields from a Person table (two fields in particular a LastName string and an UpdateUserID int).
Then I made a simple test form with a textbox and a button. From the Data Sources tab, I dragged the LastName field onto the form. This creates MyDataset, MyTableAdapter, and MyBindingSource on the form. It also databinds the textbox to the LastName field of the BindingSource.
Here's the complete code for the form:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
loadData()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Validate()
Me.MyBindingSource.EndEdit()
' Me.MyBindingSource.Current("updateUserID") = 123
Me.MyTableAdapter.Update(Me.MyDataset.myDatatable)
loadData()
End Sub
Private Sub loadData()
Me.MyTableAdapter.Fill(Me.MyDataset.myDatatable)
End Sub
End Class
This all works completely fine. The problem occurs when I uncomment the line: "Me.MyBindingSource.Current("updateID") = 123". I'm trying to set the UpdateID to save the ID of the user that most recently made a change.
In Visual Studio 2008, this worked fine. But I just upgraded to 2013 and now I'm getting an error that says "Option Strict On disallows late binding."
(PS, this is a test program. In the actual program - converted from 2008 - I only get warnings saying "Late bound resolution; runtime errors could occur.")
I understand why I'm getting these errors/warnings: because the IDE doesn't know what DataType the "updateUserID" DataColumn is.
But what can I do to manually update the BindingSource before saving? I don't think turning Option Strict off is a good idea.
The DataColumns in the DataTable have their DataTypes specified. Is there a way to reference the DataColumn directly instead of through the string of its name?
I'm looking for something like:
Me.MyBindingSource.Current.DataColumns.UpdateUserID.value = 123
Thanks for any help!

Access database connecting to VS but no data showing and runtime error

Trying to use an access database with Visual studio 15. After failure I found a number of tutorials and followed them with a new project and new database.
The database is connecting but the data inside the database won't display (although no error) and even using the built in save function in VB results in a run time error.
If anyone can point me in the right direction I'd be grateful. Code below.
Public Class Form1
Private Sub CustomersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles CustomersBindingNavigatorSaveItem.Click
Me.Validate()
Me.CustomersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.CustomersDataSet) 'Error is here****
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'CustomersDataSet.Customers' table. You can move, or remove it, as needed.
Me.CustomersTableAdapter.Fill(Me.CustomersDataSet.Customers)
End Sub
Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorDeleteItem.Click
End Sub
Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
End Sub
End Class
The error message I get is An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Unspecified error.
Here is a video that I made a while ago that should be able to help you, If you have any questions about it, i can definitely help you out.
https://www.youtube.com/watch?v=vvzY0LsAUNE
Also do you definitely need to use Access? I would recommend using an SQL database because when you publish your program, the end user may have to have a link to your access database in the same spot as yours, which can be a pain.
Nevertheless here is a video i also made regarding setting up an SQL database via Visual Studio.
https://www.youtube.com/watch?v=NLs44hxV514
If you have any issue, leave a comment and i will be happy to help you out :)
Keep In Mind
Don't forget that you need to also have a Number/unique word/or something a like in the Primary key column even if you haven't added any details to the database. eg Name and so on. If not your program may crash, so you could use the Try statement to fix this issue if the Primary Key column is left empty.