HOW TO GET THE SELECTED ITEM(S) IN A LIST VIEW CONTROL ListView returns collections of selected items and indices through the SelectedItems and SelectedIndices properties. Note that these collections are empty, if no item is currently selected( lst.SelectedItems.Count == 0 ).The first item that is selected is lst.SelectedItems[0] The index of this item in the Items collection islst.SelectedIndices[0] .So basically
is the same as
You can also use check boxes. Set CheckBoxes to true for this. Through the CheckedItems and CheckedIndices properties you can see which items are checked. |
This blog aims to help visual basic .net programmers meet solutions to some everyday programming issues.
Saturday, February 2, 2013
HOW TO GET THE SELECTED ITEM(S) IN A LIST VIEW CONTROL
KEYPRESS EVENT IN VB.NET
Handle Keyboard Input at the Form Level in VB.NET
Windows Forms processes keyboard input by raising
keyboard events in response to Windows messages. Most Windows Forms
programs process keyboard input by handling the keyboard events.
How do I detect keys pressed in VB.NET
You can detect most physical key presses by handling the KeyDown or KeyUp events. Key events occur in the following order:
KeyDown
KeyPress
KeyUp
How to detect when the Enter Key Pressed in VB.NET
The following VB.NET code behind creates the
KeyDown event handler. If the key that is pressed is the Enter key, a
MessegeBox will displayed .
If e.KeyCode = Keys.Enter Then
MsgBox("enter key pressd ")
End If
How to get TextBox1_KeyDown event in your VB.Net source file ?
Select your VB.Net source code view in Visual
Studio and select TextBox1 from the top-left Combobox and select KeyDown
from top-right combobox , then you will get keydown event sub-routine
in your source code editor.
Private Sub TextBox1_KeyDown(...)..
End Sub
The following VB.NET source code shows how to capture Enter KeyDown event from a TextBox Control.
How to: Print a Form by Using the PrintForm Component (Visual Basic)
The PrintForm component enables you to quickly print an image of a form exactly as it appears on screen without using a PrintDocument
component. The following procedures show how to print a form to a
printer, to a print preview window, and to an Encapsulated PostScript
file.
To print a form to the default printer
-
In the Toolbox, click the Visual Basic PowerPacks tab and then drag the PrintForm component onto the form.
The PrintForm component is added to the component tray.
-
In the Properties window, set the PrintAction property to PrintToPrinter.
-
Add the following code in the appropriate event handler (for example, in the Click event handler for a Print Button).
PrintForm1.Print()
To display a form in a print preview window
-
In the Toolbox, click the Visual Basic PowerPacks tab and then drag the PrintForm component onto the form.
The PrintForm component is added to the component tray.
-
In the Properties window, set the PrintAction property to PrintToPreview.
-
Add the following code in the appropriate event handler (for example, in the Click event handler for a Print Button).
PrintForm1.Print()
To print a form to a file
-
In the Toolbox, click the Visual Basic PowerPacks tab and then drag the PrintForm component onto the form.
The PrintForm component is added to the component tray.
-
In the Properties window, set the PrintAction property to PrintToFile.
-
Optionally, select the PrintFileName property and type the full path and file name for the destination file.
If you skip this step, the user will be prompted for a file name at run time.
-
Add the following code in the appropriate event handler (for example, in the Click event handler for a Print Button).
Saturday, January 5, 2013
KEYPRESS EVENT IN VB.NET
Handle Keyboard Input at the Form Level in VB.NET
Windows Forms processes keyboard input by raising
keyboard events in response to Windows messages. Most Windows Forms
programs process keyboard input by handling the keyboard events.
How do I detect keys pressed in VB.NET
You can detect most physical key presses by handling the KeyDown or KeyUp events. Key events occur in the following order:
KeyDown KeyPressKeyUp
Note: Before a form can capture its key-related events, its keyPreview property MUST be set to TRUE
How to detect when the Enter Key Pressed in VB.NET
The following VB.NET code behind creates the
KeyDown event handler. If the key that is pressed is the Enter key, a
MessegeBox will displayed .
If e.KeyCode = Keys.Enter Then MsgBox("enter key pressd ")End If
How to get TextBox1_KeyDown event in your VB.Net source file ?
Select your VB.Net source code view in Visual
Studio and select TextBox1 from the top-left Combobox and select KeyDown
from top-right combobox , then you will get keydown event sub-routine
in your source code editor.
Private Sub TextBox1_KeyDown(...)..End Sub
Friday, January 4, 2013
ADD, EDIT OR UPDATE A DATABASE
Before we start the coding for these new buttons, it's important to understand that the DataSet is disconnected from the database. What this means is that if you're adding a new record, you're not adding it to the database: you're adding it to the DataSet! Similarly, if you're updating or Deleting, you doing it to the DataSet, and NOT to the database. After you have made all of your changes, you THEN commit these changes to the database. You do this by issuing a separate command. But we'll see how it all works.
You'll need to add a few more buttons to your form - five of them. Change the Name properties of the new Buttons to the following:
btnAddNew
btnCommit
btnUpdate
btnDelete
btnClear
btnCommit
btnUpdate
btnDelete
btnClear
We'll start with the Update Record button
Updating a Record
To reference a particular column (item) in a row of the DataSet, the code is this:
ds.Tables("AddressBook").Rows(2).Item(1)
As well as returning a value, you can also set a value. You do it like this:
ds.Tables("AddressBook").Rows(2).Item(1) = "Jane"
ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text
ds.Tables("AddressBook").Rows(inc).Item(2) = txtSurname.Text
ds.Tables("AddressBook").Rows(inc).Item(2) = txtSurname.Text
MsgBox("Data updated")
Move to the next record by clicking your Next Record button, and then move back to the first record. You should see that the first record is now "Joan Smithy".
Close down your programme, then run it again. Click the Next Record button to move to the first record. It will still be "John Smith". The data you updated has been lost! So here, again, is why:
"Changes are made to the DataSet, and NOT to the Database"
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text
ds.Tables("AddressBook").Rows(inc).Item(2) = txtSurname.Text
ds.Tables("AddressBook").Rows(inc).Item(2) = txtSurname.Text
da.Update(ds, "AddressBook")
MsgBox("Data updated")
Dim cb As New OleDb.OleDbCommandBuilder(da)
The second new line is where the action is:
da.Update(ds, "AddressBook")
But the Data Adapter will then contact the database. Because we have a Command Builder, the Data Adapter can then update your database with the values from the DataSet.
Without the Command Builder, though, the Data Adapter can't do it's job. Try this. Comment out the Command Builder line (put a single quote before the "D" of Dim). Run your programme again, and then try and update a record. You'll get this error message:
The error is because you haven't got a command builder - a Valid Update Command.
Delete the comment from your Command Builder line and the error message goes away.
You should now be able to make changes to the database itself (as long as the Access database isn't Read Only).
Try it out. Run your programme, and change one of the records. Click the Update button. Then close the programme down, and load it up again. You should see your new changes displayed in the textboxes.
More tutorials like this at http://www.homeandlearn.co.uk
Subscribe to:
Posts (Atom)