Imports System.Drawing
Imports System.Text.RegularExpressions
Imports System.Windows.Forms
Module Module1
Public Enum ValidationType
MaxMin = 1
End Enum
Public Sub AssignValidation(ByRef CTRL As TextBox, ByVal Validation_Type As ValidationType, Min As Double, Max As Double)
Dim txt As TextBox = CTRL
Select Case Validation_Type
Case ValidationType.MaxMin
AddHandler txt.TextChanged, AddressOf MaximumMinimum
End Select
End Sub
Public Sub MaximumMinimum(ByVal sender As Object, ByVal e As System.EventArgs)
Dim NO As TextBox = sender
If Val(NO.Text) < Min Then
NO.Focus()
ElseIf Val(NO.Text) > Max Then
NO.Focus()
End If
End Sub
End Module
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AssignValidation(Me.TextBox1, ValidationType.MaxMin,Zo.Min,Zo.Max)
End Sub
I have question about that code. If I have several textboxes and all textboxes will have different maximum and minimum values, those minimum and maximum values are declared in the module for each textbox, then how can I add these values to that code?
Because that code shows at a moment Min=0 and Max=0 but actually I have different values.
You can use a Dictionary of Object to Tuple to store the min/max. (You can add more to the tuple if you want for example a custom error message or colors, etc.)
Option Strict On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AssignValidation(Me.TextBox1, ValidationType.MaxMin, 5, 10)
AssignValidation(Me.TextBox2, ValidationType.MaxMin, 0, 5)
End Sub
End Class
Module Module1
Private ranges As New Dictionary(Of Object, Tuple(Of Double, Double))()
Public Enum ValidationType
MaxMin = 1
End Enum
Public Sub AssignValidation(CTRL As TextBox, Validation_Type As ValidationType, Min As Double, Max As Double)
Select Case Validation_Type
Case ValidationType.MaxMin
If Not ranges.ContainsKey(CTRL) Then ranges.Add(CTRL, New Tuple(Of Double, Double)(Min, Max))
AddHandler CTRL.TextChanged, AddressOf MaximumMinimum
End Select
End Sub
Public Sub MaximumMinimum(sender As Object, e As System.EventArgs)
Dim textbox = DirectCast(sender, TextBox)
Dim value As Double
If Double.TryParse(textbox.Text, value) Then
' SUCCESS - parsed as Double
If value >= ranges(sender).Item1 AndAlso value <= ranges(sender).Item2 Then
' SUCCESS - within min and max
Else
' FAIL - outside min or max
textbox.Focus() ' what does this even do?
End If
Else
' FAIL - did not parse as Double
MessageBox.Show(textbox.Text)
End If
End Sub
End Module
* Edited to use pre .NET 7.0 Tuple syntax
Why not use the Validating event for each text box and the error provider.
Private err As New ErrorProvider()
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If TextBox1.Text = "" Then
e.Cancel = True
err.SetError(TextBox1, "This text box cannot be blank.")
Else
err.Clear()
`enter code here`End If
End Sub
Related
I have 2 codes to change the color to Textbox, and unfortunately none of them work. What's wrong here? Why the code is not good. Every time I try the code, not worked, The first code changes the color to Textbox if there is a value between 1 and 7, and the second changes the value in ascending order from the lowest to the highest and assigns a corresponding color. Please tell me if these 2 codes are written correctly, or there is a write error.
Code 1: Image: http://www.imagebam.com/image/5ac5ee1073004874
Public Class TextBoxColors
Private ColorTable As Dictionary(Of String, Color) = New Dictionary(Of String, Color)()
Public Sub New()
ColorTable.Add("1", Color.Red)
ColorTable.Add("2", Color.Aqua)
ColorTable.Add("3", Color.Chocolate)
ColorTable.Add("4", Color.BlanchedAlmond)
ColorTable.Add("5", Color.BurlyWood)
ColorTable.Add("6", Color.BlueViolet)
ColorTable.Add("7", Color.DarkBlue)
End Sub
Public Function GetColor(ColorMap As String) As Color
Return If(ColorTable.Keys.Contains(ColorMap), ColorTable(ColorMap), Color.White)
End Function
End Class
Private txtColor As TextBoxColors = New TextBoxColors()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each txtDraw As TextBox In Me.Controls.OfType(Of TextBox).Where(Function(txt) txt.Name.StartsWith("txtDraw"))
AddHandler txtDraw.TextChanged,
Sub()
If Not String.IsNullOrEmpty(txtDraw.Text) Then
txtDraw.BackColor = txtColor.GetColor(txtDraw.Text)
End If
End Sub
Next
End Sub
Code 2: This 2nd code must be changed based on my text box, which starts with SumtxtDraw
- Image: http://www.imagebam.com/image/92a4091073004904
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillColorList()
End Sub
Private Sub ColorTextBoxes()
FillTextBoxList(16, 23)
Dim SortedList As List(Of TextBox) = SortList()
Dim index As Integer
For Each txt As TextBox In SortedList
txt.BackColor = lstColor(index)
index += 1
Next
End Sub
Private Sub FillColorList()
lstColor.Add(Color.Red) 'for lowest number
lstColor.Add(Color.BlanchedAlmond)
lstColor.Add(Color.PaleGreen)
lstColor.Add(Color.Chartreuse)
lstColor.Add(Color.CadetBlue)
lstColor.Add(Color.Orange)
lstColor.Add(Color.DarkMagenta)
lstColor.Add(Color.Violet) 'for highest number
End Sub
Private Sub FillTextBoxList(StartNumber As Integer, EndNumber As Integer)
lstTextBox.Clear()
For suffix = StartNumber To EndNumber
lstTextBox.Add(DirectCast(Controls("TextBox" & suffix.ToString), TextBox))
Next
End Sub
Private Function SortList() As List(Of TextBox)
Dim orderedList = From txt In lstTextBox Order By CInt(txt.Text) Descending Select txt '$"{scorer.Score} - {scorer.Name}"
Dim SortedList As List(Of TextBox) = orderedList.ToList
Return SortedList
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ColorTextBoxes()
End Sub
I have 2 codes to change the color to Textbox, and unfortunately none of them work. What's wrong here? Why the code is not good. Every time I try the code, it tells me there are no variables. The first code changes the color to Textbox if there is a value between 1 and 7, and the second changes the value in ascending order from the lowest to the highest and assigns a corresponding color.
Images: http://www.imagebam.com/image/5ac5ee1073004874
http://www.imagebam.com/image/92a4091073004904
Code 1:
Public Class TextBoxColors
Private ColorTable As Dictionary(Of String, Color) = New Dictionary(Of String, Color)()
Public Sub New()
ColorTable.Add("1", Color.Red)
ColorTable.Add("2", Color.Aqua)
ColorTable.Add("3", Color.Chocolate)
ColorTable.Add("4", Color.BlanchedAlmond)
ColorTable.Add("5", Color.BurlyWood)
ColorTable.Add("6", Color.BlueViolet)
ColorTable.Add("7", Color.DarkBlue)
End Sub
Public Function GetColor(ColorMap As String) As Color
Return If(ColorTable.Keys.Contains(ColorMap), ColorTable(ColorMap), Color.White)
End Function
End Class
Private txtColor As TextBoxColors = New TextBoxColors()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each txtDraw As TextBox In Me.Controls.OfType(Of TextBox).Where(Function(txt) txt.Name.StartsWith("txtDraw"))
AddHandler txtDraw.TextChanged,
Sub()
If Not String.IsNullOrEmpty(txtDraw.Text) Then
txtDraw.BackColor = txtColor.GetColor(txtDraw.Text)
End If
End Sub
Next
End Sub
Code 2: This 2nd code must be changed based on my text box, which starts with SumtxtDraw
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillColorList()
End Sub
Private Sub ColorTextBoxes()
FillTextBoxList(16, 23)
Dim SortedList As List(Of TextBox) = SortList()
Dim index As Integer
For Each txt As TextBox In SortedList
txt.BackColor = lstColor(index)
index += 1
Next
End Sub
Private Sub FillColorList()
lstColor.Add(Color.Red) 'for lowest number
lstColor.Add(Color.BlanchedAlmond)
lstColor.Add(Color.PaleGreen)
lstColor.Add(Color.Chartreuse)
lstColor.Add(Color.CadetBlue)
lstColor.Add(Color.Orange)
lstColor.Add(Color.DarkMagenta)
lstColor.Add(Color.Violet) 'for highest number
End Sub
Private Sub FillTextBoxList(StartNumber As Integer, EndNumber As Integer)
lstTextBox.Clear()
For suffix = StartNumber To EndNumber
lstTextBox.Add(DirectCast(Controls("TextBox" & suffix.ToString), TextBox))
Next
End Sub
Private Function SortList() As List(Of TextBox)
Dim orderedList = From txt In lstTextBox Order By CInt(txt.Text) Descending Select txt '$"{scorer.Score} - {scorer.Name}"
Dim SortedList As List(Of TextBox) = orderedList.ToList
Return SortedList
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ColorTextBoxes()
End Sub
I'm trying to use eventwathcer but its not working with 11 characters process name (AbcdEfghIII.exe). If I write 10 characters process name (AbcdEfgIII.exe) its working. Is there character limit or am I doing something wrong? Here is my code:
Imports System.Management
Public Class Form1
Dim WithEvents StopWatch As New ManagementEventWatcher(New WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"))
Private Sub StopWatch_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs)
If e.NewEvent.Properties("ProcessName").Value.ToString = "AbcdEfghIII.exe" Then
MsgBox("Closed")
End If
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
StopWatch.Stop()
RemoveHandler StopWatch.EventArrived, AddressOf StopWatch_EventArrived
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
AddHandler StopWatch.EventArrived, AddressOf StopWatch_EventArrived
StopWatch.Start()
Catch g As Exception
MsgBox("Please, run as admin.", MsgBoxStyle.Critical, "Error")
Me.Close()
End Try
End Sub
End Class
As there doesn't seem to be a way to make this work properly, I suppose you'll just have to use a hacky workaround: Checking if the process name starts with Customization.
Doing so will of course make it react to processes like CustomizationWHATEVER.exe, but since the code you're using for some reason doesn't return the full name there is no other way if you want it to work.
Using your initial code:
If check.StartsWith("Customization", StringComparison.OrdinalIgnoreCase) Then
MessageBox.Show("Closed")
End If
PREVIOUS ANSWER:
Here is a workaround:
Get the ProcessID field instead of ProcessName, and use that together with Process.GetProcessById() to get a .NET friendly Process class instance (from which you can extract the name).
Private Sub StopWatch_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs)
Dim PID As Integer = CType(CType(e.NewEvent.Properties("ProcessID").Value, UInteger) And Integer.MaxValue, Integer)
Dim p As Process = Process.GetProcessById(PID)
'NOTE: The ".exe" part is excluded in Process.ProcessName.
If String.Equals(p.ProcessName, "Customization", StringComparison.OrdinalIgnoreCase) Then
MessageBox.Show("Closed")
End If
End Sub
I have made a simple calculator and I would like to know if you put in a string instead of a double and if you press close or not. The program is a simple calculator where the menu is a box with buttons for addition, subtraction, multiplication, and division. I would like it if you enter a string a messagebox pops up that tells you to enter a number and when you enter an empty message or click cancel it takes you back to the start of the program. Here is the code
Public Class CalcForm
Property num1 As Double = Nothing
Property num2 As Double = Nothing
Private Sub CalcForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Addition_Click(sender As Object, e As EventArgs) Handles Addition.Click
Getnum()
Dim Answer As Double = num1 + num2
MessageBox.Show("The answer is " & Answer, "Answer")
End Sub
Private Sub Subtraction_Click(sender As Object, e As EventArgs) Handles Subtraction.Click
Getnum()
Dim Answer As Double = num1 - num2
MessageBox.Show("The answer is " & Answer, "Answer")
End Sub
Private Sub Multiplication_Click(sender As Object, e As EventArgs) Handles Multiplication.Click
Getnum()
Dim Answer As Double = num1 * num2
MessageBox.Show("The answer is " & Answer, "Answer")
End Sub
Private Sub Division_Click(sender As Object, e As EventArgs) Handles Division.Click
Getnum()
Dim Answer As Double = num1 / num2
MessageBox.Show("The answer is " & Answer, "Answer")
End Sub
Private Sub Getnum()
num1 = InputBox("Enter your first number", "Number 1")
num2 = InputBox("Enter your second number", "Number 2")
End Sub
End Class
Also I don't care about the difference between an empty ok or a cancel. But since I use doubles do I have to make it a string then convert to an double? If I do, how do I detect if the string has letters and ask the user to re-enter their number?
Do you really need your message box?
If you follow Hans Passant's recommendations and start using TextBox, you could simply bind your variable properties and parse and format them.
Have a look at the example below, it is more lines of code but gets you an opportunity to look at binding parsing and formatting.
It is a simple form with your four buttons and 3 text boxes. User cannot "get out" of the TextBox value is not numeric. Should you go for this solution, I would recommend to set the TextBox property for the result to ReadOnly :)
Imports System.Math
Imports System.ComponentModel
Public Class Form1
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
Private _num1 As Double
Property num1 As Double
Get
Return _num1
End Get
Set(value As Double)
If (value <> _num1) Then
_num1 = value
NotifyPropertyChanged("num1")
End If
End Set
End Property
Private _num2 As Double
Property num2 As Double
Get
Return _num2
End Get
Set(value As Double)
If (value <> _num2) Then
_num2 = value
NotifyPropertyChanged("num2")
End If
End Set
End Property
Private _Answer As Double
Property Answer As Double
Get
Return _Answer
End Get
Set(value As Double)
If (value <> _Answer) Then
_Answer = value
NotifyPropertyChanged("Answer")
End If
End Set
End Property
Private Sub CalcForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' TextBox for result
TextBox3.ReadOnly = True
Set_bindings()
End Sub
Private Sub Addition_Click(sender As Object, e As EventArgs) Handles Addition.Click
Answer = num1 + num2
End Sub
Private Sub Substraction_Click(sender As Object, e As EventArgs) Handles Substraction.Click
Answer = num1 - num2
End Sub
Private Sub Multiplication_Click(sender As Object, e As EventArgs) Handles Multiplication.Click
Answer = num1 * num2
End Sub
Private Sub Division_Click(sender As Object, e As EventArgs) Handles Division.Click
Answer = num1 / num2
End Sub
Private Sub Set_bindings()
Dim binH As Binding
TextBox1.DataBindings.Clear()
binH = New Binding("Text", Me, "num1", True, DataSourceUpdateMode.OnPropertyChanged)
AddHandler binH.Parse, AddressOf NumParser
AddHandler binH.Format, AddressOf NumFormatter
TextBox1.DataBindings.Add(binH)
TextBox2.DataBindings.Clear()
binH = New Binding("Text", Me, "num2", True, DataSourceUpdateMode.OnPropertyChanged)
AddHandler binH.Parse, AddressOf NumParser
AddHandler binH.Format, AddressOf NumFormatter
TextBox2.DataBindings.Add(binH)
TextBox3.DataBindings.Clear()
binH = New Binding("Text", Me, "Answer", True, DataSourceUpdateMode.Never)
AddHandler binH.Parse, AddressOf NumParser
AddHandler binH.Format, AddressOf NumFormatter
TextBox3.DataBindings.Add(binH)
End Sub
Private Sub NumParser(ByVal sender As Object, ByVal e As ConvertEventArgs)
If IsNumeric(e.Value) Then
e.Value = e.Value
End If
End Sub
Private Sub NumFormatter(ByVal sender As Object, ByVal e As ConvertEventArgs)
e.Value = FormatNum(e.Value)
End Sub
Private Function FormatNum(ByVal x As Double) As String
Dim sFormat As String
sFormat = ""
Select Case Abs(x)
Case 1 To 1000
sFormat = "##0.000"
Case Is > 10000
sFormat = "0.000E+00"
Case Is = 0
sFormat = "#0.0"
Case Is < 0.001
sFormat = "0.000E-00"
Case Is < 1
sFormat = "#0.0000"
End Select
FormatNum = Format(x, sFormat)
End Function
End Class
I have two forms (1 and 2). I have been battling with some code that would prevent the user from selecting an item in the checkedListBox that was not added into ListBox2 from the previous form (form1).
The code I have is kind of weird because even if the item was added to listbox2 from form1, it continues to display the msgBox. I need the msgBox to display only to those items that were not added to listbox2, form1.
Here is what I have:
Public Class Form1
Dim ActSubject As Boolean
Public Function ActivateSubject() As String
Return ActSubject
End Function
Private Sub ListBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.TextChanged
Dim x As New Items
x.AvailableItems = ListBox2.Items.ToString
For Each x In ListBox2.Items
If ListBox2.Items.Contains(x) Then
ActSubject = True
Else
ActSubject = False
End If
Next
End Sub
End Class
Public Class Form2
Dim HaveActSubject As Boolean = Form1.ActivateSubject
Private Sub CheckedListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedValueChanged
If HaveActSubject = False Then
MsgBox("Sorry! Subject should be activated six month before registration.")
End If
Return
End Sub
End Class
Public Class Form1
Public Function ActivateSubject(itm as string) As String
Return ListBox2.Items.Contains(itm)
End Function
End Class
Public Class Form2
Dim HaveActSubject As Boolean = Form1.ActivateSubject
Private Sub CheckedListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedValueChanged
If Form1.ActivateSubject(CheckedListBox1.selectedValue) = False Then
MsgBox("Sorry! Subject should be activated six month before registration.")
End If
Return
End Sub
End Class