Sure VB - Prac
Sure VB - Prac
Net:
Module Module1
Function Add(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Return num1 + num2
End Function
End Module
8. You can call this function from the Main method of your console app like this:
Module Module1
Function Add(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Return num1 + num2
End Function
Sub Main()
Dim num1 As Integer = 5
Dim num2 As Integer = 10
Dim sum As Integer = Add(num1, num2)
Console.WriteLine("The sum of {0} and {1} is {2}", num1, num2, sum)
Console.ReadLine()
End Sub
End Module
This code declares a function named Add that takes two integer parameters and returns their sum.
The Main method calls this function and displays the result in the console window.
Login page
1. Open Visual Studio.
2. On the start window, choose Create a new project.
3. In the Create a new project window, choose Visual Basic from the Language list.
4. Next, choose Windows from the Platform list and Windows Forms App (.NET Framework) from
the Project types list.
5. After you apply the language, platform, and project type filters, choose the Windows Forms App
(.NET Framework) template, and then choose Next.
6. In the Configure your new project window, enter a name for your project and choose Create.
7. Visual Studio opens your new project. Now, you can add a login form to your project. Here are
the steps to create a login form:
a. From the Toolbox, drag a Label control to the form. b. Change the Text property of the label to
“Username”. c. Drag a TextBox control to the form. d. Drag another Label control to the form. e.
Change the Text property of the label to “Password”. f. Drag another TextBox control to the
form. g. Drag a Button control to the form. h. Change the Text property of the button to “Login”.
i. Double-click the Login button to open the code editor. j. In the code editor, add the following
code to the Click event handler of the Login button:
If txtUsername.Text = "admin" And txtPassword.Text = "password" Then
MessageBox.Show("Login successful!")
Else
MessageBox.Show("Invalid username or password.")
End If
This code checks if the values entered in the Username and Password text boxes match the expected
values. If they do, a message box is displayed with the text “Login successful!”. If they don’t, a message
box is displayed with the text “Invalid username or password.”.
Timer
1. Drag and drop a Timer control onto your form from the Toolbox1.
2. Set the Interval property of the Timer to the desired time interval in milliseconds. For example,
1000 means one second1.
3. Double-click on the Timer control to generate the Tick event handler. This is where you write the
code that will execute every time the Timer ticks1.
4. In the Tick event handler, you can use the Date.Now property to get the current date and time,
and use the Subtract method to calculate the elapsed time since the start of the program2.
5. You can also use a Label control to display the elapsed time on the form. To do this, drag and
drop a Label control onto your form, and set its Text property to the elapsed time in the Tick
event handler3.
6. To start the Timer, you can use the Start method in the Form_Load event handler or in a
Button_Click event handler. To stop the Timer, you can use the Stop method in another
Button_Click event handler or when a certain condition is met1.
Here is an example of a program that shows the use of timer in a project in vb.net. It displays the elapsed
time in seconds on a label, and stops the timer when it reaches 10 seconds.
Public Class Form1
Dim startTime As Date ' Declare a variable to store the start time
creating a multiple form project with splash screen and set the splash screen on start up in vb.net. Here
are the steps:
Here is an example of a multiple form project with splash screen and set the splash screen on start up in
vb.net. It displays a simple splash screen with a label and an image for 3 seconds before showing the
main form.
'SplashScreen.vb
Public Class SplashScreen
Private Sub SplashScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Set a timer to close the splash screen after 3 seconds
Dim timer As New Timer()
timer.Interval = 3000
AddHandler timer.Tick, AddressOf Timer_Tick
timer.Start()
End Sub
'MainForm.vb
Public Class MainForm
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Show a message box when the main form loads
MessageBox.Show("Welcome to the main form!")
End Sub
End Class
creating a project to show the use of list box in vb.net. Here are the steps:
Here is an example of a project that shows the use of list box in vb.net. It displays a list of fruits in a
ListBox and shows the selected fruit in a Label.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Add some items to the ListBox
ListBox1.Items.Add("Apple")
ListBox1.Items.Add("Banana")
ListBox1.Items.Add("Cherry")
ListBox1.Items.Add("Date")
ListBox1.Items.Add("Elderberry")
End Sub
create a project in VB.NET that demonstrates the use of InputBox and MessageBox:
1. Open Visual Studio and create a new VB.NET Windows Forms Application project.
2. In the Solution Explorer, right-click on the project name and select “Add” -> “New Item”.
3. In the “Add New Item” dialog box, select “Windows Form” and name it “Form1”.
4. Drag and drop two buttons from the toolbox onto the form.
5. Double-click the first button to open the code editor and add the following code:
6. Double-click the second button to open the code editor and add the following code:
Dim result As DialogResult = MessageBox.Show("Do you want to close this window?", "Close Window",
MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
Me.Close()
End If
This project demonstrates the use of InputBox and MessageBox in VB.NET. The first button uses InputBox
to prompt the user to enter their name and then displays a message box with a personalized greeting.
The second button displays a message box with a Yes/No prompt and closes the window if the user
selects “Yes”.
create a simple calculator in VB.NET that performs addition, subtraction, multiplication, and division:
1. Open Visual Studio and create a new VB.NET Windows Forms Application project.
2. In the Solution Explorer, right-click on the project name and select “Add” -> “New Item”.
3. In the “Add New Item” dialog box, select “Windows Form” and name it “Form1”.
4. Drag and drop four buttons from the toolbox onto the form and name them “Addition”,
“Subtraction”, “Multiplication”, and “Division”.
5. Double-click the “Addition” button to open the code editor and add the following code:
6. Double-click the “Subtraction” button to open the code editor and add the following code:
7. Double-click the “Multiplication” button to open the code editor and add the following code:
8. Double-click the “Division” button to open the code editor and add the following code:
This project demonstrates the use of InputBox and MessageBox in VB.NET to create a simple calculator
that performs addition, subtraction, multiplication, and division. The user is prompted to enter two
numbers and then select an operation. The result is displayed in a message box.
create a VB.NET project that can open Notepad, Calculator, and Command Prompt:
1. Create a new project: Open Visual Studio and create a new VB.NET project1.
2. Add buttons: Add three buttons to your form, one for each application (Notepad, Calculator,
Command Prompt).
3. Add button click events: Double-click each button to generate a click event handler.
4. Write code to open applications: Inside each button’s click event handler, write the code to
open the corresponding application. Here’s an example of how you can do this:
Private Sub btnOpenNotepad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnOpenNotepad.Click
Process.Start("notepad.exe")
End Sub
5. Run your project: Now, when you run your project and click on the buttons, it should open the
corresponding applications.
Please note that this is a basic example. Depending on your specific requirements, you might need to add
error handling or additional functionality234. Also, make sure that the paths to the executables are correct
and that they exist on the system where this code will be run. If the executables are not in the system
path, you will need to provide the full path to the executable. For
example, Process.Start("C:\\Windows\\System32\\notepad.exe").
create a project in VB.NET that uses the Web Browser control:
1. Start a new project: Open Visual Studio and start a new Windows Forms Project. Name it
“WebBrowser” or any name you prefer12.
2. Design the Form: Change the size of Form1 to your liking. For example, you can set its size
property to 640,4802.
3. Add the WebBrowser control: This is the engine that allows your web browser to connect to the
Internet1. You can find the WebBrowser control in the Toolbox on the left side. Set its size
property (for example, to 500, 371) and change its name to “WebBrowser”. Set its Dock property
to "Left"1.
4. Add a TextBox: This will serve as the address bar where the user can enter the URL. Place it at
the top of the WebBrowser control1.
5. Add Navigation Buttons: Place a button beside the TextBox and change its text to “GO”. This will
serve as the navigation button. Change its name to "btnGo"1. You can also add more buttons and
change their texts to “Home”, “Back”, “Forward”, “Refresh”, and “Search” respectively1.
6. Write the Code: Here’s a simple example of how you can code the buttons and TextBox:
Public Class Form1
Private Sub Form1_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
' WebBrowser.Navigate ()
End Sub
create a project in VB.NET that uses MySQL database connectivity with XAMPP:
1. Install Necessary Software: Download and install Visual Studio, XAMPP, and MySQL
Connector/Net.
2. Start XAMPP: Start the Apache and MySQL modules in XAMPP1.
3. Create a Database in phpMyAdmin: Log in to phpMyAdmin using the credentials provided
during the XAMPP installation. Click on the “Databases” tab and enter the name of your
database in the “Create database” field. Click on the “Create” button to create your new
database2.
4. Start a new project: Open Visual Studio and start a new Windows Forms Project3.
5. Add MySQL Connector to Your Project: You need to add the MySQL Connector/Net to your
project. This can be done by adding the DLL files to Visual Studio3.
6. Write the Code: Here’s a simple example of how you can code the buttons and TextBox:
Imports MySql.Data.MySqlClient