Showing posts with label TextBox. Show all posts
Showing posts with label TextBox. Show all posts

Wednesday, April 24, 2019

VB.Net เมื่อป้อนข้อมูลใน TextBox เสร็จแล้วเคาะ Tab หรือ Enter แล้วเลื่อนไป TextBox อันต่อไปตาม Tab Order


Private Sub AllTextBoxes_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _ Handles TextBox1.KeyDown, TextBox2.KeyDown, TextBox3.KeyDown, TextBox4.KeyDown If e.KeyCode = Keys.Tab OrElse e.KeyCode = Keys.Enter Then e.SuppressKeyPress = True Me.ProcessTabKey(True) End If End Sub

Thursday, October 12, 2017

ตัวอย่างการใช้งาน Decimal.TryParse กับ TextBox มากกว่า 1 TextBox

    
Private Sub TextBox_Leave(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles TextBox1.Leave, _
                TextBox2.Leave, _
                TextBox3.Leave, _
                TextBox4.Leave

        Dim result As Decimal
        If Not Decimal.TryParse(sender.Text.Trim, result) Then
            result = 0.0
        End If
        sender.Text = result.ToString("##0.00")

End Sub

ตัวอย่างการใช้ Decimal.TryParse

Dim result as Decimal = 0.0
if Decimal.TryParse(txt1.text, result) then
  ' valid entry
else
  ' invalid entry
end if

Wednesday, October 11, 2017

ตัวอย่างการใช้ Decimal.TryParse, Leave

Private Sub txtEstimateAmount_Leave(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles txtEstimateAmount.Leave

    ' Take any dollars-and-cents amount and round to the nearest dollar
    Dim est As Decimal
    est = Decimal.TryParse(txtEstimateAmount.Text.Trim, est)
    txtEstimateAmount.Text = If(est <> 0, Math.Round(est).ToString(), String.Empty)

End Sub

ตัวอย่างการใช้ Decimal.TryParse, Validating

Private Sub TextBox1_Validating(ByVal sender As System.Object, _
            ByVal e As System.ComponentModel.CancelEventArgs) _
            Handles TextBox1.Validating
    Dim est As Decimal
    If TextBox1.Text.Length = 0 then Exit Sub   '' optional
    If Not Decimal.TryParse(TextBox1.Text.Trim, est) Then
        e.Cancel = True
        TextBox1.SelectAll()
    Else
        TextBox1.Text = est.ToString("N0")
    End If
End Sub

ตัวอย่างการใช้งาน Decimal.TryParse

Dim a as string
Dim b as Decimal
If Decimal.TryParse(a, b) Then
   a = b.ToString("##,##0.00")
Else
   a = "can not parse"
End If

กำหนดให้ ผู้ใช้สามารถป้อนเฉพาะตัวเลขหรือจุดทศนิยนใน TextBox

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
    Handles TextBox1.KeyPress
  If e.KeyChar <> ControlChars.Back Then
     e.Handled = Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = ".")
  End If
End Sub

Sunday, September 29, 2013

VB.Net ต้องการนำค่าที่อยู่ใน Row ปัจจุบัน ใน Datagridview ไปแสดงใน Label

Private Sub DataGridView_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) _ 
        Handles DataGridView.SelectionChanged
           With DataGridView1
             If .RowCount > 0 Then
                 If Not .CurrentRow.Cells("EMP_NO").Value Is DBNull.Value Then
                    Label1.Text = .CurrentRow.Cells("EMP_NO").Value
                 Else
                    Label1.Text = "-"
                 End If
              End If
            End With
End Sub

   

Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged Dim row As DataGridViewRow = DataGridView1.CurrentRow TextBox1.Text = row.Cells(0).Value.ToString() TextBox2.Text = row.Cells(1).Value.ToString() TextBox3.Text = row.Cells(2).Value.ToString() End Sub

Wednesday, September 19, 2012

วิธี Clear All TextBox in ASP.Net

เคลียร์ TextBox ใน WinForm ก็ไม่ยากแต่มาทำใน WebForm นี่งง... อยู่ตั้งนาน จนมาเจอโค้ดนี้ใช้งานได้ Work จริง ๆ
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  EmptyTextBoxValues(Me)
End Sub
Private Sub EmptyTextBoxValues(ByVal parent As Control)
  For Each c As Control In parent.Controls
    If (c.Controls.Count > 0) Then
        EmptyTextBoxValues(c)
    Else
        If TypeOf c Is TextBox Then
           CType(c, TextBox).Text = ""
        End If
    End If
  Next
End Sub

Monday, September 17, 2012

การเลือก Clear Control ที่เป็น Textbox ทั้งหมด ใน VB.Net

For Each ctrl As Control In Me.Controls
     If ctrl.GetType Is GetType(TextBox) Then        
         ctrl.Text = String.Empty
          ' Do something
      End If
Next



แต่มันจะข้ามที่อยู่ใน Groupbox
ถ้าเราจะให้ใน Groupbox ทำด้วยก็สั่งแบบนี้
For Each ctrl As Control In Groupbox1.Controls
     If ctrl.GetType Is GetType(TextBox) Then         
         ctrl.Text = String.Empty
          ' Do something 
      End If 
Next

การตรวจสอบ Textbox.text ว่าว่างหรือเป็นตัวเลขหรือไม่

        If IsNumeric(TextBox1.Text) Then
            MsgBox("Numeric")
        End If
        If TextBox1.Text = String.Empty Then
            MsgBox("Empty")
        End If

Wednesday, January 25, 2012

VB.net Format TextBox as Number

Dim n As Integer = 10000
TextBox1.Text = n.ToString("##,##,###.00")