错误2类'WindowsApplication1.ManagementObject'无法编入索引,因为它没有默认属性

时间:2016-03-01 12:22:56

标签: vb.net

Public Class frm_SMSconnectionSetting
    Public Shared Comm_Port As Int16 = 0
    Public Shared Comm_BaudRate As Int32 = 0
    Public Shared Comm_TimeOut As Int32 = 0
    Public Shared comm As GsmCommMain
    Private dataSet As DataSet, dataSet1 As New DataSet()
    Private _queryObj As Object

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Property queryObj(p1 As String) As Object
        Get
            Return _queryObj
        End Get
        Set(value As Object)
            _queryObj = value
        End Set
    End Property


    Private Sub button9_Click(sender As Object, e As EventArgs) Handles button9.Click
        'Setting the column grid for fetched data
        dataGridView3.ColumnCount = 5
        dataGridView3.Columns(0).Name = "COM Port"
        dataGridView3.Columns(1).Name = "Connected Device"
        dataGridView3.Columns(2).Name = "Max Baud Rate"
        dataGridView3.Columns(3).Name = "Time Out"
        dataGridView3.Columns(4).Name = "Status"
        dataGridView3.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells

        Try
            Cursor.Current = Cursors.WaitCursor
            Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_SerialPort")
            For Each queryObj As ManagementObject In searcher.[Get]()
                If queryObj IsNot Nothing Then
                    Dim captionObj As Object = queryObj("DESCRIPTION")
                    Dim capdeviceid As Object = queryObj("DEVICEID")
                    Dim MaxBaudRate As Object = queryObj("MAXBAUDRATE")
                    Dim connstatus As Object = queryObj("STATUS")
                    Dim timeoutsec As String = "100"
                    dataGridView3.Rows.Add(capdeviceid, captionObj, MaxBaudRate, timeoutsec, connstatus)
                End If
            Next
        Catch e15 As Exception
            MessageBox.Show("An error occurred while querying for WMI data: " + e15.Message)
        End Try
        Cursor.Current = Cursors.[Default]
        button9.Enabled = False
        button10.Enabled = True
    End Sub

    Private Sub frm_SMSconnectionSetting_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        button10.Enabled = False
        lblConnected.Visible = False
    End Sub

    Private Sub button10_Click(sender As Object, e As EventArgs) Handles button10.Click
        Try
            If Not comm.IsConnected() Then
                MessageBox.Show("No Phone Connected", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                lblNotConnect.Visible = True
                lblConnected.Visible = False
                Return
            Else
                comm.Close()
                lblNotConnect.Visible = True
                lblNotConnect.Visible = False
                button9.Enabled = True
                button10.Enabled = False
                dataGridView3.Rows.Clear()
                Phone_Model.Text = "....."
                Phone_Name.Text = "....."
                Revision_Num.Text = "....."
                Serial_Num.Text = "....."
                MessageBox.Show("Disconnected Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch No_Conn As Exception
            MessageBox.Show("No Phone Connected")
        End Try
    End Sub
End Class

0 个答案:

没有答案