none
disable button as comma RRS feed

  • Question

  • I have problems with calculator:
    how to disable button as comma bottun or + - * / bottons to have double character as
    ,,, or +++ in the label

    I have tried this code but it not work

     Private Sub delatbtn_Click(sender As Object, e As EventArgs) Handles delatbtn.Click
            Dim b As Button = sender
            If Label1.Text = "0" Or Label1.Text = "/" Then
                b = Nothing
    
            Else
                Label1.Text = Label1.Text + b.Text
            End If
        End Sub

    Wednesday, January 10, 2018 10:49 AM

Answers

  • I have problems with calculator:
    how to disable button as comma bottun or + - * / bottons to have double character as
    ,,, or +++ in the label

    I have tried this code but it not work

     Private Sub delatbtn_Click(sender As Object, e As EventArgs) Handles delatbtn.Click
            Dim b As Button = sender
            If Label1.Text = "0" Or Label1.Text = "/" Then
                b = Nothing
    
            Else
                Label1.Text = Label1.Text + b.Text
            End If
        End Sub

    Hi

    Don't know if this will help, but it shows one way to limit the user entry to numbers with leading '+' or '-' and only permits one '.'

    Here, it is used for a TextBox.

    ' NumericTextBox
    ' Form1 with TextBox1
    ' Limit user key entry to number
    ' with '+-' only as first char and
    ' '.' allowed only once
    Option Strict On
    Option Explicit On
    Public Class Form1
    
      Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    
    	' allows only "0123456789.+-" and BackSpace
    	' allows "-+" only as first char.
    	' allows "." only once.
    
    	Dim t As TextBox = DirectCast(sender, TextBox)
    
    	Dim tb() As Char = t.Text.ToCharArray
    
    	Dim allowed As String = "0123456789.+-" & Chr(8)
    
    	If allowed.IndexOf(e.KeyChar) = -1 Or (e.KeyChar = "." And tb.Count(Function(c) c = ".") > 0) Or (e.KeyChar = "-" And tb.Count > 0) Or (e.KeyChar = "+" And tb.Count > 0) Then
    	  e.Handled = True
    	End If
      End Sub
    
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Majed Abbas Wednesday, January 10, 2018 3:30 PM
    Wednesday, January 10, 2018 3:07 PM

All replies

  • Majed, 

    Nothing means in VB that it has no reference, not that it does not exist. 

    To remove a button from a form you have to remove it from controls. 

    Controls.Remove(DirectCast(sender, button))

    Put on Option Strict On in top of your code, the plus sign with that off means adding. The concatination character is &.


    Success Cor


    • Edited by Cor Ligthert Wednesday, January 10, 2018 12:48 PM
    Wednesday, January 10, 2018 12:45 PM
  • I have problems with calculator:
    how to disable button as comma bottun or + - * / bottons to have double character as
    ,,, or +++ in the label

    I have tried this code but it not work

     Private Sub delatbtn_Click(sender As Object, e As EventArgs) Handles delatbtn.Click
            Dim b As Button = sender
            If Label1.Text = "0" Or Label1.Text = "/" Then
                b = Nothing
    
            Else
                Label1.Text = Label1.Text + b.Text
            End If
        End Sub

    Hi

    Don't know if this will help, but it shows one way to limit the user entry to numbers with leading '+' or '-' and only permits one '.'

    Here, it is used for a TextBox.

    ' NumericTextBox
    ' Form1 with TextBox1
    ' Limit user key entry to number
    ' with '+-' only as first char and
    ' '.' allowed only once
    Option Strict On
    Option Explicit On
    Public Class Form1
    
      Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    
    	' allows only "0123456789.+-" and BackSpace
    	' allows "-+" only as first char.
    	' allows "." only once.
    
    	Dim t As TextBox = DirectCast(sender, TextBox)
    
    	Dim tb() As Char = t.Text.ToCharArray
    
    	Dim allowed As String = "0123456789.+-" & Chr(8)
    
    	If allowed.IndexOf(e.KeyChar) = -1 Or (e.KeyChar = "." And tb.Count(Function(c) c = ".") > 0) Or (e.KeyChar = "-" And tb.Count > 0) Or (e.KeyChar = "+" And tb.Count > 0) Then
    	  e.Handled = True
    	End If
      End Sub
    
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Majed Abbas Wednesday, January 10, 2018 3:30 PM
    Wednesday, January 10, 2018 3:07 PM