2017-01-12 3 views
-3

의 인스턴스는 내가의 C# - 클래스

drones.ip_drone == cb_drone.SelectedText 

public partial class Arena : Form 
{ 
    private readonly Drone[] drone; 

    public Arena(params Drone[] drone) 
    { 
     InitializeComponent(); 
     this.drone = drone; 

    } 

    private void cb_drone_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     foreach (var drones in drone) 
     { 
      if(drones.ip_drone == cb_drone.SelectedText) 
      { 
       //which index of Drone[] drone?? 

      } 
     } 

    } 
+4

왜 'for'루프를 사용하지 않고 정확히 어떤 색인인지 알고 싶습니다. – Equalsk

+0

당신은 smartttt입니다 –

+0

또한 잘못된 속성을 사용하고 있습니다. SelectedText를 "HighlightedText"라고 생각하십시오. 대신'SelectedItem.ToString()'을 사용하십시오. – LarsTech

답변

0

내가 콤보가 당신의 드론 객체로 채워졌다 생각 콤보 상자와 같은 IP를 가지고 클래스 드론의 배열의 어떤 인덱스를 알고 싶어 . 그렇다면 직접 선택한 것에 액세스 할 수 있습니다.

private void cb_drone_SelectedIndexChanged(object sender, EventArgs e) 
{ 
     var selectedDrone = cb_drone.SelectedItem as Drone; 
     if (selectedDrone != null) 
     { 
    // you can also use Array.IndexOf if need be 
     } 
} 
+0

내 콤보 상자에만 무인 장치의 IP가 있습니다 –

+0

다음과 같이 코드를 사용할 수 있습니다. var index = drones.Select ((d, i) => new {IP = d.ip_drone, Index = i}). 싱글 (d => d.IP == cb_drone.SelectedText). 인덱스. –

0

왜 대신 for에 대한 foreach을 변경하지 마십시오?

foreach (var index = 0; index < drone.Length; index++) 
{ 
    if(drone[index].ip_drone == cb_drone.SelectedText) 
    { 
     //index will have the value you are looking for 
    } 
} 

그러나 이는 잘못된 설계입니다. @Cetin Basoz가 추천하는 것을 따르십시오. 단지 ip가 아닌 combobox에 객체를 연결하는 SelectedItem 속성에 대한 연구.