0% found this document useful (0 votes)
23 views8 pages

Sure VB - Prac

The document describes how to create a VB.NET project that uses functions. It includes code to declare a function that adds two integers and returns their sum, and code in the main method to call this function and display the results. It then provides another example of creating a login form project.

Uploaded by

gauravkjha2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

Sure VB - Prac

The document describes how to create a VB.NET project that uses functions. It includes code to declare a function that adds two integers and returns their sum, and code in the main method to call this function and display the results. It then provides another example of creating a login form project.

Uploaded by

gauravkjha2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

create a project to show the use of function in VB.

Net:

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 Console from the Project types list.
5. After you apply the language, platform, and project type filters, choose the Console
App 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 function to your project. Here is an
example of a function that takes two integers as input and returns their sum:

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.”.

8. Press F5 to run your application and test your login form.

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

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


Timer1.Interval = 1000 ' Set the timer interval to 1000 milliseconds (1 second)
startTime = Date.Now ' Set the start time to the current date and time
Timer1.Start() ' Start the timer
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick


Dim elapsedTime As TimeSpan = Date.Now.Subtract(startTime) ' Calculate the elapsed time
Label1.Text = elapsedTime.TotalSeconds.ToString("N0") ' Display the elapsed time in seconds on the label
If elapsedTime.TotalSeconds >= 10 Then ' Check if the elapsed time is 10 seconds or more
Timer1.Stop() ' Stop the timer
MessageBox.Show("Time is up!") ' Show a message box
End If
End Sub
End Class

creating a multiple form project with splash screen and set the splash screen on start up in vb.net. Here
are the steps:

1. Create a new Windows Forms Application project in Visual Studio.


2. Add a new Windows Form to the project and name it SplashScreen.vb. This will be your splash
screen form2.
3. Open the SplashScreen.vb form in the designer mode, right-click on it and open its
properties. Set the FormBorderStyle property to None and the ShowInTaskbar property to False2.
4. Customize the appearance of the splash screen form by adding controls, images, labels, etc. You
can also use the SplashFormBase class or its descendant to inherit from and create a non-skin-
dependent splash screen3.
5. Add another Windows Form to the project and name it MainForm.vb. This will be your main
form that will show after the splash screen1.
6. Open the Project Properties window and select the Application tab. Under the Startup object
drop-down list, select the MainForm1.
7. Under the Splash screen drop-down list, select the SplashScreen1. This will automatically display
the splash screen form on your main form’s startup and close it when your main form has been
completely initialized and displayed4.
8. Optionally, you can add code to the splash screen form or the main form to control the behavior
of the splash screen, such as setting a timer, showing progress, closing on a condition, etc243.

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

Private Sub Timer_Tick(sender As Object, e As EventArgs)


'Close the splash screen and dispose the timer
Me.Close()
CType(sender, Timer).Dispose()
End Sub
End Class

'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:

1. Create a new Windows Forms Application project in Visual Studio1.


2. Drag and drop a ListBox control from the Toolbox onto your form12.
3. To add items to the ListBox, select the ListBox control and open its properties window. Click the
ellipses (…) button next to the Items property. This opens the String Collection Editor dialog box,
where you can enter the values one at a line13.
4. You can also add items to the ListBox at runtime by using the Items.Add method in the code3. For
example, ListBox1.Items.Add("Apple") will add the item “Apple” to the ListBox1.
5. To get or set the selected item in the ListBox, you can use the SelectedItem property in the
code3. For example, Label1.Text = ListBox1.SelectedItem.ToString() will display the selected item in
the Label1.
6. To get or set the index of the selected item in the ListBox, you can use the SelectedIndex
property in the code3. For example, ListBox1.SelectedIndex = 0 will select the first item in the
ListBox1.
7. You can also use the SelectedIndices and SelectedItems properties to get a collection of the
indexes or items that are selected in the ListBox, if the SelectionMode property is set to
MultiSimple or MultiExtended3.
8. You can customize the appearance and behavior of the ListBox by using other properties, such as
BorderStyle, ColumnWidth, MultiColumn, Sorted, etc13.

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

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles


ListBox1.SelectedIndexChanged
'Display the selected item in the Label
Label1.Text = "You selected: " & ListBox1.SelectedItem.ToString()
End Sub
End Class

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:

Dim name As String = InputBox("Enter your name:")


MessageBox.Show("Hello, " & name & "!")

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

7. Save the project and press F5 to run it.

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:

Dim num1 As Double = Val(InputBox("Enter first number:"))


Dim num2 As Double = Val(InputBox("Enter second number:"))
Dim result As Double = num1 + num2
MessageBox.Show("The sum of " & num1 & " and " & num2 & " is " & result)

6. Double-click the “Subtraction” button to open the code editor and add the following code:

Dim num1 As Double = Val(InputBox("Enter first number:"))


Dim num2 As Double = Val(InputBox("Enter second number:"))
Dim result As Double = num1 - num2
MessageBox.Show("The difference between " & num1 & " and " & num2 & " is " & result)

7. Double-click the “Multiplication” button to open the code editor and add the following code:

Dim num1 As Double = Val(InputBox("Enter first number:"))


Dim num2 As Double = Val(InputBox("Enter second number:"))
Dim result As Double = num1 * num2
MessageBox.Show("The product of " & num1 & " and " & num2 & " is " & result)

8. Double-click the “Division” button to open the code editor and add the following code:

Dim num1 As Double = Val(InputBox("Enter first number:"))


Dim num2 As Double = Val(InputBox("Enter second number:"))
Dim result As Double = num1 / num2
MessageBox.Show("The quotient of " & num1 & " and " & num2 & " is " & result)

9. Save the project and press F5 to run it.

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

Private Sub btnOpenCalculator_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


btnOpenCalculator.Click
Process.Start("calc.exe")
End Sub

Private Sub btnOpenCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


btnOpenCmd.Click
Process.Start("cmd.exe")
End Sub
In the above code, Process.Start is used to start an external application2. You just need to pass the name
of the application’s executable file as a string argument to Process.Start.

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

Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


btnGo.Click
WebBrowser.Navigate(txtSearch.Text)
End Sub

Private Sub btnHome_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


btnHome.Click
WebBrowser.GoHome()
End Sub

Private Sub btnForward_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


btnForward.Click
WebBrowser.GoForward()
End Sub

Private Sub btnBack_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


btnBack.Click
WebBrowser.GoBack()
End Sub

Private Sub btnRefresh_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


btnRefresh.Click
WebBrowser.Refresh()
End Sub
End Class
This code creates 6 buttons named btnGo for Navigation, btnSearch for Searching, btnHome for our Home
Page, btnRefresh for Refreshing the URL, btnBack for backing in the previous URL, and btnForward for
forwarding the URL viewed. It also creates a textbox for inputting the URL, named txtSearch1.
7. Run the Program: After designing and coding, run the program. Your program will look like a web
browser. Now, you can type a URL (like www.facebook.com) into your TextBox and click the “Go
Button” to navigate1

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

Public Class Form1


Private ConnString As String = "server=localhost;user id=root;password=;database=your_database_name"

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


btnConnect.Click
Using cn As New MySqlConnection(ConnString)
Try
cn.Open()
MessageBox.Show("Connection to MySQL Database Successful!")
Catch ex As Exception
MessageBox.Show("An error occurred while connecting to the database: " & ex.Message)
End Try
End Using
End Sub
End Class
This code creates a button named btnConnect for connecting to the MySQL database. It also creates a
connection string with the server name, user id, password, and database name. When
the btnConnect button is clicked, it tries to open a connection to the MySQL database

You might also like