문제

string KaloriSorgusu = "use FoodDB select drink_kal from Drinks";
SqlConnection baglanti2 = new SqlConnection("Data Source=" + IPFORM.ip.ToString() + "\\SQLEXPRESS;Initial Catalog=UserDB;User Id=Levent; Password=21012101;Trusted_Connection=False;Integrated Security=False");
 using (baglanti2)
 {
     using (SqlCommand KaloriKomutu = new SqlCommand(KaloriSorgusu, baglanti2))
     {
         baglanti2.Open();
         using (SqlDataReader KALORİokuyucu = KaloriKomutu.ExecuteReader())
         {
              while (KALORİokuyucu.Read())
              {
                  lb_Kalori.Items.Add(KALORİokuyucu.GetValue(0).ToString());
                  total = lb_Kalori.Items.Count;
              }
         }
     }
}

This code populates the listbox and when I try to get values from selected index I can't seem to find a way to get the value of an index.

도움이 되었습니까?

해결책

May be this little snippet will help you.

// Get the currently selected item in the ListBox. 
   string curItem = listBox1.SelectedItem.ToString();

 // Find the string in ListBox2. 
  int index = listBox2.FindString(curItem);
 // If the item was not found in ListBox 2 display a message box, otherwise select it in        ListBox2. 
 if(index == -1)
    MessageBox.Show("Item is not available in ListBox2");
 else
   listBox2.SetSelected(index,true);`
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top