根据在第一个combox中选择的值,第二个combox的值会改变! [英] Based on the value selected in the First combox , second combox value changes !

查看:80
本文介绍了根据在第一个combox中选择的值,第二个combox的值会改变!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DataGrid中有两个彼此相邻的组合框.一个组合框具有OID,而另一个组合具有该OID的描述.现在,我希望在运行时更改我的第二个组合框项目(OID的描述) wrt到在第一个组合框中选择的OID.是否可能?如何?


I have two ComboBoxes in a DataGrid adjacent to one another..One combobox has OIDs and the other one has the description for that OIDS..now I want my second Combobox Items(Description of the OIDs) to be change at runtime w.r.t to the OID selected in the first Combobox..Is it possible??How?


Imports System
Imports SnmpSharpNet
Public Class Form1
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.Columns.Add("NameOne", "Column One")
        DataGridView1.Columns.Add("NameTwo", "Column Two")
        DataGridView1.Columns.Add("NameThree", "Column Three")
        Dim dataGridRow As New DataGridViewRow()
        Dim cells As DataGridViewCell() = New DataGridViewCell(1) {}
        Dim txt1A As New DataGridViewTextBoxCell()
        Dim txt1B As New DataGridViewTextBoxCell()
        txt1A.Value = "Host"
        txt1B.Value = "115.186.113.88"
        dataGridRow.Cells.Add(txt1A)
        txt1A.[ReadOnly] = True
        dataGridRow.Cells.Add(txt1B)
        dataGridRow.Height = 20
        DataGridView1.Rows.Add(dataGridRow)
        dataGridRow = New DataGridViewRow()
        cells = New DataGridViewCell(1) {}
        Dim txt2A As New DataGridViewTextBoxCell()
        Dim ComboBox1 As New DataGridViewComboBoxCell()
        ComboBox1.Items.Add("1.3.6.1.2.1.1.1.0")
        ComboBox1.Items.Add("1.3.6.1.2.1.1.2.0")
        ComboBox1.Items.Add("1.3.6.1.2.1.1.3.0")
        ComboBox1.Items.Add("1.3.6.1.2.1.1.4.0")
        ComboBox1.Items.Add("1.3.6.1.2.1.1.5.0")
        ComboBox1.Items.Add("1.3.6.1.2.1.1.6.0")
        ComboBox1.Items.Add("1.3.6.1.2.1.1.7.0")
        ComboBox1.Value = ComboBox1.Items(0)
        ComboBox1.[ReadOnly] = False
        txt2A.Value = "OIDs"
        dataGridRow.Cells.Add(txt2A)
        txt2A.[ReadOnly] = True
        dataGridRow.Cells.Add(ComboBox1)
        dataGridRow.Height = 20
        DataGridView1.Rows.Add(dataGridRow)
        Dim requestOid() As String
        requestOid = New String() {ComboBox1.Selected}
        dataGridRow = New DataGridViewRow()
        cells = New DataGridViewCell(1) {}
        Dim txt3A As New DataGridViewTextBoxCell()
        Dim cbo2 As New DataGridViewComboBoxCell()
        cbo2.Items.Add("Get")
        cbo2.Items.Add("GetNext")
        cbo2.Value = cbo2.Items(0)
        cbo2.[ReadOnly] = False
        txt3A.Value = "SNMP Operation"
        dataGridRow.Cells.Add(txt3A)
        txt3A.[ReadOnly] = True
        dataGridRow.Cells.Add(cbo2)
        dataGridRow.Height = 20
        DataGridView1.Rows.Add(dataGridRow)
        dataGridRow = New DataGridViewRow()
        cells = New DataGridViewCell(1) {}
        Dim txt4A As New DataGridViewTextBoxCell()
        Dim txt4B As New DataGridViewTextBoxCell()
        txt4A.Value = "Community String"
        txt4B.Value = "public"
        dataGridRow.Cells.Add(txt4A)
        dataGridRow.Cells.Add(txt4B)
        dataGridRow.Height = 20
        DataGridView1.Rows.Add(dataGridRow)
        Dim comboBoxCell As New DataGridViewComboBoxCell
        comboBoxCell.Items.Add("sysDescr")
        comboBoxCell.Items.Add("sysObjectID")
        comboBoxCell.Items.Add("sysUpTime")
        comboBoxCell.Items.Add("sysContact")
        comboBoxCell.Items.Add("sysName")
        comboBoxCell.Items.Add("sysLocation")
        comboBoxCell.Items.Add("sysServices")
        DataGridView1(2, 1) = comboBoxCell
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim host As String
        Dim community As String
        host = DataGridView1.Rows(0).Cells(1).Value.ToString
        community = DataGridView1.Rows(3).Cells(1).Value.ToString
        Dim txt4B As New DataGridViewTextBoxCell()
        txt4B.Value = "public"
        Call ListView1_SelectedIndexChanged(sender, e)
    End Sub
    Public Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        Dim result As Dictionary(Of Oid, AsnType)
        Dim snmp As New SimpleSnmp
        snmp = New SimpleSnmp(DataGridView1.Rows(0).Cells(1).Value.ToString, DataGridView1.Rows(3).Cells(1).Value.ToString)
        ''abc.Text = txtsnmpaction.SelectedItem
        result = snmp.Get(SnmpVersion.Ver1, New String() {DataGridView1.Rows(1).Cells(1).Value.ToString()})
        ''result = snmp.GetNext(SnmpVersion.Ver1, requestoid)
        ''If (txtsnmpaction = "GetBulk")
        ''result = snmp.GetBulk(New String() {".1.3.6.1.2", ".1.3.6.1.3"})
        '' End If
        If Not snmp.Valid Then
            DataGridView2.Rows(0).Cells(0).Value = "Invalid hostname/community"
        End If
        If result IsNot Nothing Then
            Dim kvp As KeyValuePair(Of Oid, AsnType)
            For Each kvp In result
                ListView1.Items.Add(kvp.Key.ToString)
                ListView1.Items.Add(SnmpConstants.GetTypeName(kvp.Value.Type))
                ListView1.Items.Add(kvp.Value.ToString())
            Next kvp
        Else
            DataGridView2.Rows(0).Cells(0).Value = "No results received"
        End If

    End Sub

End Class

推荐答案

好,这是个主意:您不必在ComboBox的列表项中存储字符串.您可以存储任何类型的对象.因此,您可以使用定义第二个组合框中的操作所需的所有信息来创建某种数据类型.我们称其为ListItemHelper类型.处理事件System.Windows.Forms.ComboBox.SelectedIndexChangedSystem.Windows.Forms.ComboBox.SelectionChangeCommitted时,使用属性Items通过其索引提取选定的项目,将该项目下播到ListItemHelper,并用于从该事件处理程序中操作第二个ComboBox.

现在,问题在于,由于ListItemHelper不是字符串,因此组合框将不会以任何有用的方式在列表中显示其字符串表示形式.这个问题很容易解决. ComboBox实际上在屏幕上显示方法object.ToString返回的内容.将此方法重写为ListItemHelper.ToString以返回有关每个项目的一些语义信息.

现在,我要指出:您喜欢硬编码的字符串,因为立即常量将使您无所适从.根据经验,除了像0、1和null这样的少数常量外,不要使用任何立即数.为了使您的代码至少具有最低限度的支持,您需要使用数据文件,属性,资源,并使用Reflection来从类型中提取名称-很多技术,但不是硬编码.您可以在我的文章中找到基于枚举类型的此类技术的一些示例(特别适合像您这样的列表):枚举类型不要枚举!解决.NET和语言限制 [ ^ ]和人类可读的枚举元数据 [
OK, here is the idea: you don''t have to store strings in the list items of the ComboBox. You can store objects of any type. So you can create some data type with all the information needed to define what to do in the second combo box. Let''s call it ListItemHelper type. When you handle the event System.Windows.Forms.ComboBox.SelectedIndexChanged or System.Windows.Forms.ComboBox.SelectionChangeCommitted, use the property Items to extract selected item by its index, down-cast the item to ListItemHelper and use to operate the second ComboBox from this event handler.

Now, the problem is that as ListItemHelper is not string, the combo box will not show its string representation in the list in any informative manner. This problem is easy to solve. ComboBox actually shows on screen what is returned by the method object.ToString. Override this method as ListItemHelper.ToString to return some semantic information about each item.

Now, I want to note: you manner to hard-coded strings as immediate constants will lead you nowhere. As a rule of thumb, you should never use any immediate constants except few like 0, 1 and null. To make you code at least minimally supportable you need to use data files, attribute, resources, extract names from type using Reflection — a lot of techniques, but not hard-coding. You can find some examples of such techniques (especially good for lists like yours) based on enumeration type in my articles: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^] and Human-readable Enumeration Meta-data[^].

—SA


其价值-在 ^ ]和 Windows的其余部分表单数据绑定 [ ^ ]的东西.

阅读Frans Boumas的博客:真正复杂的数据绑定:具有弱类型集合的ITypedList [
For what it''s worth - Read up on BindingSource[^] and the rest of the Windows Forms Data Binding[^] stuff.

Read Frans Boumas'' blog: Really complex databinding: ITypedList with weakly typed collections[^] - for more background material.

It''s a lot of material - and for working your way through this problem it may seem like overkill. On the other hand - if you get used to think in terms of organizing the data to support the UI - you are well on your way to comprehend MVVM - where databinding is quite essential.

Best regards
Espen Harlinn


u必须在第一个组合框上编写代码,
私人子组合1_Change()
//然后为combo2项目编写代码
u have to write code on first combobox ,under
Private Sub Combo1_Change()
// then write code for combo2 items


这篇关于根据在第一个combox中选择的值,第二个combox的值会改变!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆