Hi everyone,
I am using Visual Studio 2008 Express Edition.
On my opening form I have list that displays the first column of my database.
Does anybody know how I can do the following?:
When I double click an item from the list (or select it and click an open button), the entire row of the database is displayed in another form I have created as a display template.
All help is greatly appreciated.
Thanks for looking.
I assume the first column you are displaying in the list is the primary key/ unique key for the table in your database.
So on double click, just pass that value to the other form. The other form should query your database for that value and show the entire record.
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
Since the listbox items collection is of object type, you can store anything in it. So store both the things.. display text (Fault) as well as the value (ID).
Add a class like this:
vb.net Code:
Public Class MyListItem
Public DisplayText As String
Public Value As Integer
Public Sub New(ByVal displayText As String, ByVal value As Integer)
Me.DisplayText = displayText
Me.Value = value
End Sub
Public Overrides Function ToString() As String
Return Me.DisplayText
End Function
End Class
Then you can fill your listbox with objects of this class.
e.g.
In your other form, overload the New method (constructor) to accept a value.
vb.net Code:
Public Sub New(ByVal ID As Integer)
InitializeComponent()
'do whatever you way you want to process the ID here
'get data from database, fill controls with those values etc.
End Sub
And call that form, something like this:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim selectedId As Integer = CType(List1.SelectedItem, MyListItem).Value
Dim f As New Form2()
f.ShowDialog()
End Sub
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
Public Class MyListItem
Public DisplayText As String
Public Value As Integer
Public Sub New(ByVal displayText As String, ByVal value As Integer)
Me.DisplayText = displayText
Me.Value = value
End Sub
Public Overrides Function ToString() As String
Return Me.DisplayText
End Function
End Class
Then where the list box is:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
I have added the following before End Sub:
ListBox1.Items.Add(New MyListItem("whatever display text", whateverValue))
What do i have to chenage "whatever display text" and "whatever value" to?
Then where the list box is:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
I have added the following before End Sub:
ListBox1.Items.Add(New MyListItem("whatever display text", whateverValue))
What do i have to chenage "whatever display text" and "whatever value" to?
Many thanks for your help i really appreciate it.
SelectedIndexChanged event is fired when the selection of listbox is changed. You don't add items to the listbox there, isn't it? That add code goes wherever you are populating the listbox with values. Can you show the code you are using to add your listbox items? I'll be able to suggest appropriate changes to it.
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
Hi again,
I have attatched a word file containing the code i have for my form to display the list so far.
I was going to go to bed so i closed the form but i guess i didnt save the changes as the list is no longer displaying.
I think i did something with the databinding options in visual basic.
Thanks.
What you posted in that file is the Designer generated code. You need to post the class file where the code you code is present.
Also not everyone opens attachments. It would be better if you just copy/paste the code here in [CODE]...[/CODE] tags.
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
Oh sorry about that, im new to all this visual basic.
Is this the code that you requested?:
Code:
Public Class Index
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
Private Sub Index_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Panel1.Left = Me.Width / 2 - Panel1.Width / 2
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
End Class
Hi again, i have been working with your code and this is what i have came up with so far if it is correct:
Code:
Public Class Index
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
Private Sub Index_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Panel1.Left = Me.Width / 2 - Panel1.Width / 2
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
ListBox1.Items.Add(New MyListItem("Fault", ID))
End Sub
End Class
Public Class MyListItem
Public DisplayText As String
Public Value As Integer
Public Sub New(ByVal Fault As String, ByVal ID As Integer)
Me.DisplayText = Fault
Me.Value = ID
End Sub
Public Overrides Function ToString() As String
Return Me.DisplayText
End Function
End Class
This is as far as i have got but it is returning an error stating that the ID in the following line is not declared:
Where were you adding the ListBox contents originally (when not using my code)? Surely it was not the ListBox1_SelectedIndexChanged. You have to add that ListBox1.Items.Add statement there.
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
Hi, yes i think it was there the code was placed.
Please forgive me if this is basic for you it is all brand new to me.
I created the link from the list box to the database in the design view of visual basic, not in the code view.
Thanks
Hi again, i think iv sorted out some confusion. There must have been something wrong with my code as it was not displaying the Fault column in the list box. I have recreated my project and it is now displaying the Fault column in the list box sucessfully. I have put the new code below:
Code:
Public Class Index
Private Sub Index_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DatabaseDataSet.Table1' table. You can move, or remove it, as needed.
Me.Table1TableAdapter.Fill(Me.DatabaseDataSet.Table1)
End Sub
End Class
ok I see now. So you are using databinding. Then use the DisplayMember and ValueMember properties. You do not need anything else.
You specify which field to use to display items in listbox as DisplayMember, and the field whose value to be used as ValueMember.
Try this:
(not tested code)
vb.net Code:
Private Sub Index_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
In your other form, overload the New method (constructor) to accept a value.
vb.net Code:
Public Sub New(ByVal ID As Integer)
InitializeComponent()
'do whatever you way you want to process the ID here
'get data from database, fill controls with those values etc.
End Sub
And call that form, like this:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim selectedId As Integer = ListBox1.SelectedValue
Dim f As New Form2(selectedId)
f.ShowDialog()
End Sub
Last edited by Pradeep1210; Sep 21st, 2009 at 08:33 PM.
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
What do you mean when you say 'do whatever you way you want to process the ID here 'get data from database, fill controls with those values etc.???
Does this mean i have to insert the code to get the info from the database and insert it into the text boxes? The only way i know how to populate the text boxes on the view form (form that displays all of the data) is to assign the databindings from the properties menu in visual basic.
The code for my view page is:
Code:
Public Class View
Private Sub View_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Panel1.Left = Me.Width / 2 - Panel1.Width / 2
End Sub
Private Sub Button1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseClick
Index.Show()
Me.Hide()
End Sub
End Class
Thanks for your help once again.
Last edited by leemp5; Sep 22nd, 2009 at 04:55 AM.