YouTube | Facebook | X(Twitter) | RSS

ComboBox に Layer オブジェクトを追加して表示名をレイヤー名にする方法

2016/9/1 (木)

// クラスを作らない方法
private ILayer[] layers = null;
 
private void Form1_Load(object sender, EventArgs e)
{
    AxMapControl map = (AxMapControl)m_Main.Controls["axMapControl1"];
    layers = new ILayer[map.LayerCount];
 
    for (int i = 0; i < map.LayerCount; i++)
    {
        layers[i] = map.get_Layer(i);
    }
    comboBox1.DataSource = layers;
    comboBox1.DisplayMember = "Name";
}
 
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
    IFeatureLayer fl = comboBox1.SelectedValue as IFeatureLayer;
        :
        :
}
Private layers As ILayer() = Nothing
 
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim map As AxMapControl = DirectCast(m_Main.Controls("axMapControl1"), AxMapontrol)
 
    layers = New ILayer(map.LayerCount - 1) {}
    For i As Integer = 0 To map.LayerCount - 1
        
        layers(i) = map.get_Layer(i)
    Next
    comboBox1.DataSource = layers
    comboBox1.DisplayMember = "Name"
End Sub
 
Private Sub comboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e A EventArgs)
    Dim fl As IFeatureLayer = TryCast(comboBox1.SelectedValue, IFeatureLayer)
    
End Sub
 
 
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVa e System.EventArgs) Handles ComboBox1.SelectedIndexChanged
 
    Dim pLayer As ILayer
    pLayer = CType(ComboBox1.SelectedItem, ComboLayer).Layer
    MessageBox.Show(pLayer.Name)
 
End Sub
 
 
Private Sub subAddLayerList()
 
    ComboBox1.Items.Clear()
 
    Dim pEnumLayer As IEnumLayer
    pEnumLayer = axMapControl1.Map.Layers(, True)
 
    Dim pLayer As ILayer
    pEnumLayer.Reset()
    pLayer = pEnumLayer.Next
 
    Dim pArrLayer As ComboLayer
    Dim pArrayList As New ArrayList()
 
    Do Until pLayer Is Nothing
 
        pArrLayer = New ComboLayer
        pArrLayer.Layer = pLayer
 
        pArrayList.Add(CType(pArrLayer, ComboLayer))
 
 
        'ComboBox1.Items.Add(CType(pLayer, Object))
 
        pLayer = pEnumLayer.Next
 
    Loop
 
    'ComboBoxにArrayListを追加して該当の名称を指定
    ComboBox1.DataSource = pArrayList
    ComboBox1.ValueMember = "Layer"
    ComboBox1.DisplayMember = "Name"
 
End Sub
 
'ComboBoxにレイヤリストを追加するための独自クラス
'参考:http://www.bnote.net/vb/relate_combobox.shtml
Private Class ComboLayer
    Protected m_strName As String
    Protected m_pLayer As ILayer
 
    Public Sub New()
    End Sub
 
    Public ReadOnly Property Name() As String
        Get
            Return m_pLayer.Name
        End Get
        'Set(ByVal value As String)
        '    m_strLayerName = value
        'End Set
    End Property
 
    Public Property Layer() As ILayer
        Get
            Return m_pLayer
        End Get
        Set(ByVal value As ILayer)
            m_pLayer = value
        End Set
    End Property
 
    Public Overrides Function ToString() As String
        Return Me.Name
    End Function
End Class
  • この記事を書いた人

羽田 康祐

伊達と酔狂のGISエンジニア。GIS上級技術者、Esri認定インストラクター、CompTIA CTT+ Classroom Trainer、潜水士、PADIダイブマスター、四アマ。WordPress は 2.1 からのユーザーで歴だけは長い。 代表著書『"地図リテラシー入門―地図の正しい読み方・描き方がわかる』 GIS を使った自己紹介はこちら。ESRIジャパン(株)所属、元青山学院大学非常勤講師を兼務。日本地図学会第31期常任委員。発言は個人の見解です。

-プログラミング, ArcGIS
-, ,