以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  求助 全局代码  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=119886)

--  作者:jackyfashion
--  发布时间:2018/6/3 12:51:00
--  求助 全局代码
老师:
您好!
我想把英吋转厘米我代码写在全局代码内,窗口Button的代码如下:

Public amcm As String

Public cmovrnd As Decimal

If amcm.Contains(" ") = False AndAlso amcm.Contains("/") Then

    Public slam() As String = amcm.split("/")

    Public ihcam As Decimal = CSng(slam(0))

    Public ihdam As Decimal = CSng(slam(1))

    Public ihaam As Decimal = ihcam / ihdam

    cmovrnd = Format(Eval(ihaam * 2.54,e.DataRow),"###0.###")

Else

    Public sam() As String = amcm.split(" ")

    Public iham As Decimal = CDec(sam(0))

    If sam.Length = 1 Then

        cmovrnd = Format(Eval(iham * 2.54,e.DataRow),"###0.###")

    Else

        Public slam() As String = sam(1).split("/")

        Public ihcam As Decimal = CSng(slam(0))

        Public ihdam As Decimal = CSng(slam(1))

        Public ihaam As Decimal = ihcam / ihdam

        cmovrnd = Format(Eval((iham + ihaam) * 2.54,e.DataRow),"###0.###")

    End If

End If

厘米转英吋

表事件DataColChanging代码:如下:

Public cmam As Decimal

Public amovrnd As String

                         z =  cmam

                    If z > "" Then

                        Dim s() As String = z.split(".")

                        If s.Length = 1 Then

                            amovrnd = (s(0))

                        Else

                            Dim i As Long = CInt(s(1))

                            Dim j As Long  = 10 ^ s(1).Length

                            Dim k = j *1000 / 16

                            Dim l = i*1000\\k

                            If l = 0 Then

                                amovrnd = (s(0))

                            Else

                                Dim m = l / 16

                                Dim d As Decimal = m

                                Dim sl() As String = d.ToString().split(".")

                                If sl.Length = 1 Then

                                   amovrnd = (sl(0))

                                Else

                                    Dim b As Long = CInt(sl(1))

                                    Dim a As Long  = 10 ^ sl(1).Length

                                    Dim c As Long  = a Mod b

                                    Dim x = a

                                    Dim y = b

                                    Do While c <> 0

                                        a=b

                                        b=c

                                        c = a Mod b

                                    Loop

                                    Dim r As String

                                    If (s(0)) = 0  Then

                                        r = y/b & "/" & x/b

                                    Else

                                        r = s(0) &" " & y/b & "/" & x/b

                                    End If

                                    amovrnd = (r)

                                End If

                            End If

                        End If

                    End If

            End Select

               Select Case e.DataCol.name

            Case "siudamkf"

                Dim z As String

                z =  e.NewValue

                If z > "" Then

                    Dim s() As String = z.split(".")

                    If s.Length = 1 Then

                        e.NewValue = (s(0))

                    Else

                        Dim i As Long = CInt(s(1))

                        Dim j As Long  = 10 ^ s(1).Length

                        Dim k = j *1000 / 16

                        Dim l = i*1000\\k

                        If l = 0 Then

                            e.NewValue = (s(0))

                        Else

                            Dim m As String = l / 32

                            Dim d As Decimal = m

                            Dim sl() As String = d.ToString().split(".")

                            If sl.Length = 1 Then

                                e.NewValue = (sl(0))

                            Else

                                Dim b As Long = CInt(sl(1))

                                Dim a As Long  = 10 ^ sl(1).Length

                                Dim c As Long  = a Mod b

                                Dim x = a

                                Dim y = b

                                Do While c <> 0

                                    a=b

                                    b=c

                                    c = a Mod b

                                Loop

                                Dim r As String

                                If (s(0)) = 0  Then

                                    r = y/b & "/" & x/b

                                Else

                                    r = s(0) &" " & y/b & "/" & x/b

                                End If

                                e.NewValue = (r)

                            End If

                        End If

                    End If

                End If

        End Select

    End If

  请老师指教!

谢谢老师!!!


[此贴子已经被作者于2018/6/3 13:06:18编辑过]

--  作者:有点甜
--  发布时间:2018/6/3 12:53:00
--  

封装成一个方法调用,如

 

http://www.foxtable.com/webhelp/scr/1938.htm

 


--  作者:jackyfashion
--  发布时间:2018/6/3 13:18:00
--  
老师:
您 好!
我真是不懂得怎样写代码在全局代码,现在开始学习全局代码,
求老师帮一下忙,事后我在领会,看能不能理解全局代码
谢谢老师的教导!!!
谢谢!

--  作者:有点甜
--  发布时间:2018/6/3 17:56:00
--  

Public Function convertcm(amcm As String, value As DataRow) As Decimal
Dim cmovrnd As Decimal

If amcm.Contains(" ") = False AndAlso amcm.Contains("/") Then
   
    Dim slam() As String = amcm.split("/")
   
    Dim ihcam As Decimal = CSng(slam(0))
   
    Dim ihdam As Decimal = CSng(slam(1))
   
    Dim ihaam As Decimal = ihcam / ihdam
   
    cmovrnd = Format(Eval(ihaam * 2.54,value),"###0.###")
   
Else
   
    Dim sam() As String = amcm.split(" ")
   
    Dim iham As Decimal = CDec(sam(0))
   
    If sam.Length = 1 Then
       
        cmovrnd = Format(Eval(iham * 2.54,value),"###0.###")
       
    Else
       
        Dim slam() As String = sam(1).split("/")
       
        Dim ihcam As Decimal = CSng(slam(0))
       
        Dim ihdam As Decimal = CSng(slam(1))
       
        Dim ihaam As Decimal = ihcam / ihdam
       
        cmovrnd = Format(Eval((iham + ihaam) * 2.54,value),"###0.###")
       
    End If
   
End If
return cmovrnd
end function

 

调用

 

dim cmovrnd = convertcm(“123”, Tables("表A").current.DataRow)
msgbox(cmovrnd)


--  作者:jackyfashion
--  发布时间:2018/6/3 18:39:00
--  
谢谢老师!
谢谢!!!