Home > Enterprise >  How do I bind a data from a separate listbox?
How do I bind a data from a separate listbox?

Time:10-12

I have two listboxes and I have a button to display the selected item in a message box but I need to select from both listboxes. Is there a way for me to bind the data from the second listbox to the first one.

This is the interface:
This is the interface

This is how the data would be shown when selected

here

and what I meant by binding the data I want the 200 calory value to stay with rice even though something else is selected

here

Public Class Form1
Public strfood As String

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    Dim calory As Single

    strfood = InputBox("Enter food item", "Food List")
    calory = InputBox("Enter calory", "Calory List")

    FoodList.Items.Add(strfood)
    CaloryList.Items.Add(calory)
End Sub

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

    Dim selecteditem As String = FoodList.SelectedItems(0).ToString
    Dim selectedcalory As String = CaloryList.SelectedItems(0).ToString

    MessageBox.Show("Food :"   selecteditem & " " & "Calories :"   selectedcalory)
End Sub

CodePudding user response:

Agree with djv's suggestion; these things should be in a single grid

  • right click the project in Solution Explorer
  • add new item
  • add a file of type DataSet
  • double click it, an empty surface appears. Rename the dataset to something sensible like CalTrackerDataSet (props grid)
  • right click the surface and choose Add..DataTable
  • name it Foods
  • right click the DataTable, choose Add..Column
  • name it Food
  • add another column named Calories, and set its type to Int32 (integer) in properties grid
  • save
  • go to your form
  • open the data sources tool panel (on the view menu, inside Other Windows) and find the Foods node, drag it into your form
  • an item appears in the bottom tray, called CalTrackerDataSet - change this to CalTrackerDS - I recommend this purely because it's a great point of confusion when vb names instances of things the same as the type

you don't need your BtnAdd code any more because new rows can be created just by typing into the grid, but if you did want to add a row like this, programmatically, you would:

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    Dim f = InputBox("Enter food item", "Food List")
    Dim c = Convert.ToInt32(InputBox("Enter calory", "Calory List"))

    CalTrackerDS.Foods.AddFoodsRow(f,c)
End Sub

And displaying the row the user clicked on is more subtle; a device called a BindingSource tracks the current row. Because it works with a wide range of data sources it returns quite basic objects that hide the specifics of the bound data so you have to cast to turn them back

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

    Dim cur and DirectCast(DirectCast(FoodsBindingSource.Current, DataRowView).Row, CalTrackerDataSet.FoodsRow) 

    MessageBox.Show("Food :" & cur.Food & " " & ", Calories :" & cur.Calories)
End Sub
  • Related