Ввод чисел в textbox - VB

Узнай цену своей работы

Формулировка задачи:

Помогите, пожалуйста. Хочу создать простой калькулятор, но только в бэйсике, все арифметические функции сделал. Суть вопроса: нужно сделать так, чтобы при нажатии допустим на кнопку "7" в текстбокс допустим 1, где находится курсор писалась 7, а если курсор находится в текстбоксе 2 то 7 писалась там и не где больше

Решение задачи: «Ввод чисел в textbox»

textual
Листинг программы
Option Explicit
'
'Калькулятор (by the fever.brain 2017)
'
Const r = 90, m = "_"
Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Dim WithEvents cbx As ComboBox, WithEvents txb As TextBox, WithEvents but As CommandButton
Dim i&, j&, ii&, l&, t&, w&, h&, x&, y&, s$, u$, v, sz&(1), Script As Object, pic As PictureBox
 
Sub EnCode()
    Dim b() As Byte, i&, f$, h$(): ReDim h$(1): On Error Resume Next: ChDir App.Path: If Len(Dir("41.ico")) > 0 Then Exit Sub
    h(0) = "526172211a0700cf907300000d00000000000000a24c74208026001a030000f6090000028a810acd26488a3a1d3506002020000034312e69636f081d955108bd20163aecb5353484dfcf7c1116196b6668a6145d89fe132d12dfc8289190490891859f5bc2f6bd61bc268a5d14654219d1a516f4bc6f0d7c256d410d9508541151773373b19cee7745a2e77315ddca544bd6e1bc39cef3bf7d99b9d0cfbecff35867ef0fc74f8e1f8fdfbece73b9d3b8773033cbbe0424211a12262515d993bb0c0705fda3ae2c08557c838e3c20cc5aa80a21bc2103282bca60e3e4895d44b1ebed98f57695f4f6606fe1bab825ad32c5a2cfb2940b206c6900b005c51c17452cb05e92e6a80f7d4ab7065cd42be003dddf56da9d5f6f789a947cfcf42b75755607c3c3033a997f6399b8c0e3933afab00fa79899a12f80cebfad570027a33f997b82e0c3b3078f70acccda11278e4e3cb75f1a5897844feff25ef4f1dd1dbd23ec9faaaed8edb330c01e07f4dd60f48ce0d7803d14b16ec012a79024c80130530b7d983fd92cafdeea1c5131f3940141be5bcb10492b39f2477c7c64fffa150e6b62192faa31b36e1b69a916d23c48c46086929be349bf64e4a4f8b3628a92167390d4d539c7d80eb89e772b4a392847fb7129cb6a7b94db9ddb4ecace9ee5620ced65b9ed56b96da196b312f2f3f2f313d3d3"
    h(1) = "f9e1a99dfb7e8b7e6375ff74380677d1fcdeb3111fd2de8ebddc870baec448486f0cc76e683b06ae9d06e8cc6f522a65a50f566685a45348d090f086c439a6802d90e9bbd93ffc5b00fe20e66acc5908121753e6f58771a4d0aaa5d7b2814198d054928a7e5e454368df5802c9a2f51ce80a0500f2eba9cc0ad546454c2aba7562fad355fb0040a01e5d872c60d7bfddb828ae3abd4402b18b5d44ccce28c76f0acd79159c6f9fc8d0265c7520b37ef1cac3cb75fb3acd501b13b00f2ed7b41f16edff8978c54dec20b2a566616f8a2c72c0ca3b3acd501b5a666646d8b3832e82b234afbd05d1aef74c081403cbba0516a8e64bb5eaf83bfeef4c1b6a76661784a2e93ff330ca2d31b7a002da95cd43330be863371c6718a2db8dbd8018b8a93e82fa947e08584b28a2e2ead2a26c02e6a8fa5d5582a3d587ca8bd2355950050268debe31df45502fe4c3f198bae61013f847b1231ebd983f6eff54616c7e10c43d7b00400700"
    h(0) = Join(h, vbNullString): ReDim b(Len(h(0)) / 2 - 1): For i = 0 To UBound(b): b(i) = CLng("&H" & Mid$(h(0), i * 2 + 1, 2)): Next: Erase h: f = "tmpEnCoderArc.rar": i = FreeFile
    Open f$ For Binary As #i: Put #i, 1, b: Close #i: Call CreateObject("WScript.Shell").Run("WinRAR x -y """ & f & "", 1, True): Kill f
End Sub
 
Sub ApplylProp(Optional ByVal Caption$, _
                            Optional ByVal Icon$, _
                            Optional ByVal BorderStyle& = 1, _
                            Optional ByVal MaxButton As Boolean = 0, _
                            Optional ByVal CenterScreen As Boolean = 1, _
                            Optional ByVal Align As Boolean = 1)
    With Me
        If BorderStyle Then .BorderStyle = BorderStyle
        If Align Then
            For Each v In Controls
                If v.Left + v.Width > sz(0) Then sz(0) = v.Left + v.Width
                If v.Top + v.Height > sz(1) Then sz(1) = v.Top + v.Height
            Next: .Move .Left, .Top, sz(0) + (.Width - .ScaleWidth) + r, sz(1) + (.Height - .ScaleHeight) + r
        End If
        If CenterScreen Then .Move (Screen.Width - .Width) / 2, (Screen.Height - .Height) / 2
        If Len(Caption) Then .Caption = Caption
        If Len(Icon) Then Set .Icon = LoadPicture(Icon): For i = 0 To 1: Call SendMessage(hwnd, &H80, i, ByVal .Icon.Handle): Next
        If Not MaxButton Then SetWindowLong hwnd, -16, GetWindowLong(hwnd, -16) And Not &H10000
    End With
End Sub
 
 
Private Sub Form_Load()
    Def l, r, t, r, w, r * 31, h, r * 3, x, Screen.TwipsPerPixelX, y, Screen.TwipsPerPixelY
    Set cbx = Controls.Add("vb.ComboBox", "cbx"): With cbx
        .Move l, t, w
        .Visible = 1
    End With
    Set txb = Controls.Add("vb.textbox", "txb"): With txb
        .Move l + x, t + y, w - r * 3 - x * 2, h: t = t + .Height + r
        .ZOrder 0: .Text = 0: .BorderStyle = 0: .Visible = 1
    End With
    Def l, r, w, r * 6, h, r * 4, ii, 0, s, "123=C456+-789*/,0^()", v, Split("sin cos tan sqr abs atn exp log rnd und")
    For i = 1 To 6: If i = 5 Then t = t + r
        For j = 1 To 5
            With Controls.Add("vb.CommandButton", "but" & ii)
                 If j = 4 Then l = l + r
                .Move l, t, w, h: Def l, l + w, ii, ii + 1
                .FontBold = 1
                If i < 5 Then .Caption = Mid$(s, ii, 1) Else .Caption = v(ii - 21)
                .Visible = 1
            End With
    Next: Def l, r, t, t + h: Next
    Set Script = CreateObject("MSScriptControl.ScriptControl"): Script.Language = "VBScript"
    Call EnCode: ApplylProp "Калькулятор", "41.ico"
End Sub
 
Private Sub but_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    ii = txb.SelStart
    If Mid$(but.Caption, 1, 1) Like "[a-z]" Then txb.SelText = but.Caption & "(": ii = ii + 1 Else txb.SelText = but.Caption
    txb.SelStart = ii + Len(but.Caption)
End Sub
 
Private Sub txb_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then KeyAscii = 0: txb.Text = txb.Text & "="
End Sub
 
Private Sub cbx_Click()
    With txb: .Text = cbx.Text: .SelStart = Len(.Text): End With
End Sub
 
Private Sub txb_Change()
    With txb
        On Error Resume Next
        s = .Text:: If Len(u) = 0 Then u = s
        If InStr(1, s, "=") > 0 Then
            s = Replace(Replace(Replace(s, " ", ""), "=", ""), ",", "."): u = s
            s = Script.eval(s)
            If Err Then MsgBox Err.Description & vbLf & Err.Number Else cbx.AddItem s, 0
            .Text = s
        ElseIf InStr(1, s, "C") Then
            u = Replace(s, "C", "")
            .Text = 0
        ElseIf InStr(1, s, "u", 1) Then
            .Text = u
        End If
        If Len(s) > 1 Then If Mid$(s, 1, 1) = "0" And IsNumeric(Mid$(s, 2, 1)) Or IsNumeric(Mid$(s, 1, 1)) And Mid$(s, 2, 1) Like "[a-z]" Then .Text = Mid$(s, 2)
        .SelStart = Len(s)
    End With
End Sub
 
 
Sub Def(ParamArray w()): Dim i&: For i = 0 To UBound(w) Step 2: w(i) = w(i + 1): Next: End Sub
Sub Pro(ByVal typ&, ByVal val, ParamArray w()): Dim i&: For i = 0 To UBound(w): CallByName Controls(w(i)), Choose(typ, "enabled", "caption", "text"), 4, val: Next: End Sub
Private Sub but_LostFocus(): On Error Resume Next: Set but = ActiveControl: End Sub
Private Sub cbx_LostFocus(): but_LostFocus: End Sub
Private Sub txb_LostFocus(): but_LostFocus: End Sub

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 3.75 из 5
Похожие ответы