大家看第一个张图片,是我数显卡尺发送的数据,默认为MM状态这数据是仪器传来处理过后的数据,代码
Private Sub MSComm1_OnComm()
    Dim inputbuffer As Variant
    Dim data As String
    Dim dblValue As Double
    Select Case MSComm1.CommEvent
           Case comEvReceive
            inputbuffer = MSComm1.Input()
            data = StrConv(inputbuffer, vbUnicode)
            dblValue = Val(Left$(data, 1) & "1") * Val(Mid$(data, 3, 6)) / 100 
            Text1.Text = Format$(dblValue, "0.00")
            Text2.Text = "MM"
我想点击单位选择按钮的时候,数据变成英制状态(因为我的数显卡尺上面也有单位选择)~~~~
简单点说就是我点击软件上面的单位选择按钮,数据显示就变成图2代码是我改成以下可以实现的~~
Private Sub MSComm1_OnComm()
    Dim inputbuffer As Variant
    Dim data As String
    Dim dblValue As Double
    Select Case MSComm1.CommEvent
           Case comEvReceive
            inputbuffer = MSComm1.Input()
            data = StrConv(inputbuffer, vbUnicode)
            dblValue = Val(Left$(data, 1) & "1") * Val(Mid$(data, 3, 6)) / 100 
            Text1.Text = Format$(dblValue/25.4, "0.000")
            Text2.Text = "IN"

我的意思是我按一下单位选择按钮显示的是英制,再按一下又换回"MM"状态......
问题就在
Private Sub Command8_Click()'单位选择按钮End Sub
还有Private Sub MSComm1_OnComm() 里面怎么写,要单独一个状态的我代码上面可以实现

解决方案 »

  1.   

    Private Sub Command8_Click()'单位选择按钮 
    if Text2.Text = "IN" then
       Text1.Text = Format$(dblValue*25.4, "0.000") 
       Text2.Text = "MM" 
    else
       Text1.Text = Format$(dblValue/25.4, "0.000") 
       Text2.Text = "IN" 
    end if
    End Sub 
      

  2.   

    不是这样了~~可能说得不明白
    图一是我开始的界面,当我点击单位选择按钮的时候会变成图二
    图二显示的代码是我重新写在Private Sub MSComm1_OnComm()里面
    意思就是我知道要显示出英制的代码,但是不知道用Private Sub Command8_Click()来转
    大概应该是这样写的吧private m_bSwitchUnit as boolean '是否变更单位'private sub Command8_click()
        dim dblValue as double
        m_bSwitchUnit = not m_bSwitchUnit
        dblValue = Val(Text1)
        if m_bSwitchUnit then
            Text1.Text = Format$(dblValue / 25.4, "0.000")
            Text2.Text="IN"
        else
            Textcom1.Text = Format$(dblValue * 25.4, "0.00")
            Text2.Text="MM"
        end if 
    Private Sub MSComm1_OnComm()
        ...
        data = StrConv(inputbuffer, vbUnicode) 
        dblValue = Val(Left$(data, 1) & "1") * Val(Mid$(data, 3, 6)) / 100 
        if m_bSwitchUnit then
            Text1.Text = Format$(dblValue / 25.4, "0.000")
            Text2.Text="IN"
        else
            Text1.Text = Format$(dblValue, "0.00")
            Text2.Text="MM"
        end if 
        ...
    但是这代码也用不了
      

  3.   


    Private Sub Form_Load()
        Text1.Text = 5.9
        Text2.Text = "MM"
    End SubPrivate Sub Command8_click()
        Static w_Flg As Boolean
        w_Flg = Not w_Flg
        If w_Flg Then
            Text1.Text = FormatNumber(Text1.Text / 25.4, 4, vbTrue)
            Text2.Text = "IN"
        Else
            Text1.Text = FormatNumber(Text1.Text * 25.4, 2, vbTrue)
            Text2.Text = "MM"
        End If
     End Sub
     
    Private Sub MSComm1_OnComm()
        ...
        Data = StrConv(inputbuffer, vbUnicode)
        dblValue = Val(Left$(Data, 1) & "1") * Val(Mid$(Data, 3, 6)) / 100
        If Text2.Text = "MM" Then
           Text1.Text = FormatNumber(dblValue, 4, vbTrue)
        Else
            Text1.Text = FormatNumber(dblValue / 25.4, 4, vbTrue)
        End If
        ...
    End Sub
      

  4.   

    楼主,把你的 Private Sub MSComm1_OnComm() 中定义的:
    Dim dblValue As Double移到窗体代码通用节中,声明成模块级的变量。
      

  5.   

    楼主试一试这个:Option Explicit
    Dim bolInch As Boolean  '标记单位  True:英寸  False:毫米
    Private Sub Command8_Click()
    On Error GoTo errSub
        bolInch = Not bolInch   '切换单位类型
        If bolInch Then
            Text2.Text = "IN"
            If Text1.Text <> "" Then Text1.Text = Val(Text1.Text) / 25.4
        Else
            Text2.Text = "MM"
            If Text1.Text <> "" Then Text1.Text = Val(Text1.Text) * 25.4
        End If
        Exit Sub
    errSub:End SubPrivate Sub Form_Load()
        bolInch = False
        Text2.Text = "MM"
        Rem 下面是串口控件初始化语句
        
    End SubPrivate Sub MSComm1_OnComm()
        Dim inputbuffer As Variant
        Dim data As String
        Dim dblValue As Double
    On Error GoTo errSub
        Select Case MSComm1.CommEvent
            Case comEvReceive
                inputbuffer = MSComm1.Input()
                data = StrConv(inputbuffer, vbUnicode)
                dblValue = Val(Left$(data, 1) & "1") * Val(Mid$(data, 3, 6)) / 100  '读出有效的数据(单位是:mm)
                
                If bolInch Then dblValue = dblValue / 25.4  '如果单位是英寸,那么转换为英寸
                Text1.Text = Format$(dblValue, "#0.00")
        End Select
    errSub:End Sub