I want the user to click on a radio button and then click the submit button. Once the submit button is pressed it changes a textbox value. However Visual Studio is telling me that I need to use a 'Raised Event'. I don't know how to use that or is it even needed? Here is the code:
Private Sub create_Click(sender As Object, e As RoutedEventArgs)
If RadioButton1.Checked = True Then
Me.creflag.Text += "1"
ElseIf RadioButton2.Checked = True Then
Me.creflag.Text += "4224"
ElseIf RadioButton3.Checked = True Then
Me.creflag.Text += "2"
ElseIf RadioButton4.Checked = True Then
Me.immune.Text += "619659263"
Me.typeflag.Text += "4"
End If
End Sub
In your XAML markup you have
.... Click="Button_Click"
this means that you need to have a RouterEventHandler named Button_Click not create_Click
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
If RadioButton1.Checked = True Then
Me.creflag.Text += "1"
ElseIf RadioButton2.Checked = True Then
Me.creflag.Text += "4224"
ElseIf RadioButton3.Checked = True Then
Me.creflag.Text += "2"
ElseIf RadioButton4.Checked = True Then
Me.immune.Text += "619659263"
Me.typeflag.Text += "4"
End If
End Sub
See MSDN docs and examples
Ok so I found out that I was mixing windows forms and WPF together. In windows forms it's .Checked but in WPF that is an event thus why it was giving me errors. The correct way in WPF is .IsChecked
Final Code:
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs) Handles Button.Click
If RadioButton1.IsChecked = True Then
Me.creflag.Text += "1"
ElseIf RadioButton2.IsChecked = True Then
Me.creflag.Text += "4224"
ElseIf RadioButton3.IsChecked = True Then
Me.creflag.Text += "2"
ElseIf RadioButton4.IsChecked = True Then
Me.immune.Text += "619659263"
Me.typeflag.Text += "4"
End If
End Sub
Related
Can someone guide me or correct my code. I have multiple conditions in my Timer.
Each Button runs on 1 Timer but each Button clicked has different code or conditions on the timer. The problem is, only one condition will work.
If I click the first Button, the condition for the first Button will run but when I click the second Button, the first condition will still run.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
''progressbar(KryptonBorderEdge2) timer
KryptonBorderEdge2.Width = KryptonBorderEdge2.Width + 1
Dim curWidth = KryptonBorderEdge2.Width
Dim percentage = (Double.Parse(curWidth) / Double.Parse(404) * 100.0).ToString("#0.00") ''to get percentage
KryptonLabel5.Text = percentage.ToString + " %" ''404 is the max width or the 100%
If KryptonBorderEdge2.Width = 404 Then
Timer1.Enabled = False
End If
''CONNECT button is clicked
If hardwareConnect = True Then
If KryptonLabel5.Text = "50.00 %" Then
Timer1.Interval = 10
Label2.Text = "Hardware Found. Now attempting to connect."
ElseIf KryptonLabel5.Text.Contains("100") Then
KryptonComboBox1.Text = "OMNIKEY"
Label2.Text = "Hardware successfully connected!"
hardwareConnect = False
Timer1.Enabled = False
End If
End If
''CHECK PORT button is clicked
If checkingPort = True Then
If KryptonLabel5.Text.Contains("100") Then
MsgBox("Card format: JCOP S3CC9P9" + vbNewLine + "Connected on COM4", vbOKOnly)
KryptonComboBox2.Text = "JCOP S3CC9P9"
Label2.Text = "..."
checkingPort = False
Timer1.Enabled = False
End If
End If
''REMOVE SAVED DATA is clicked
If KryptonLabel5.Text = "89.00 %" Then
Label2.Text = "Finishing things up."
If KryptonLabel5.Text.Contains("100") Then
removeAll()
MsgBox("Data Successfully Removed!", MsgBoxStyle.OkOnly)
Label2.Text = "..."
Timer1.Enabled = False
End If
End If
''START/WRITE DATA button is clicked
If writeData = True Then
If KryptonLabel5.Text = "20.00 %" Then
Label2.Text = "Blank Card detected."
ElseIf KryptonLabel5.Text = "30.00 %" Then
Label2.Text = "Writing account name..."
ElseIf KryptonLabel5.Text = "40.00 %" Then
Label2.Text = "Inserting ARQC Key and Master Key..."
ElseIf KryptonLabel5.Text = "50.00 %" Then
Label2.Text = "Confirming Expiry Date..."
ElseIf KryptonLabel5.Text = "60.00 %" Then
Label2.Text = "Generating PIN..."
ElseIf KryptonLabel5.Text = "70.00 %" Then
Label2.Text = "Writing PIN and CVV..."
ElseIf KryptonLabel5.Text = "80.00 %" Then
Label2.Text = "Confirming Track1/Track2/Track3..."
ElseIf KryptonLabel5.Text = "90.00 %" Then
Label2.Text = "Finalizing everything. Please wait for a while..."
Timer1.Interval = 1000
ElseIf KryptonLabel5.Text.Contains("100") Then
Label2.Text = "Successfully writing data to the card!"
Timer1.Enabled = False
writeData = False
End If
End If
End Sub
If you want only one condition to be true at a given time, don't use Booleans. Instead, create an enum with possible operations and a variable to store the current operation
Enum Operation
None
HardwareConnect
CheckingPort
WriteData
End Enum
Dim currentOperation As Operation
Now, when a button is clicked set the current operation. Like this only one type of operation can be active.
Private Sub ConnectButton_Click(sender As Object, e As EventArgs) Handles ConnectButton.Click
currentOperation = Operation.HardwareConnect
End Sub
Your sub then looks like this:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'progressbar(KryptonBorderEdge2) timer
KryptonBorderEdge2.Width = KryptonBorderEdge2.Width + 1
Dim curWidth = KryptonBorderEdge2.Width
Dim percentage = (Double.Parse(curWidth) / Double.Parse(404) * 100.0).ToString("#0.00") ''to get percentage
KryptonLabel5.Text = percentage.ToString + " %" ''404 is the max width or the 100%
If KryptonBorderEdge2.Width = 404 Then
Timer1.Enabled = False
End If
If currentOperation = Operation.HardwareConnect Then 'CONNECT button is clicked
...
ElseIf currentOperation = Operation.CheckingPort Then 'CHECK PORT button is clicked
...
End If
' REMOVE SAVED DATA is clicked
If KryptonLabel5.Text = "89.00 %" Then
...
End If
If currentOperation = Operation.WriteData Then
' START/WRITE DATA button is clicked
...
End If
End Sub
Instead of setting the Booleans to False you can set currentOperation = Operation.None
Sub StopOperations()
Timer1.Enabled = False
currentOperation = Operation.None
End Sub
I have a program that can change values in a database. I have a TrueDBGrid in my form and the Page Down/Up works fine. If I click on a button with TAB, this Page Down/Up still works. How can I prevent this when the focus is on the button?
I've found a solution. That would be:
Private Sub frmZeitcodeListe_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
Try
If e.KeyCode = Keys.PageUp Then
If cmdBearbeiten.Focused = True Or cmdLoeschen.Focused = True Or cmdNeu.Focused = True Or cmdSchliessen.Focused = True Then
e.Handled = False
Else
bsTblZeitcode.MovePrevious()
e.Handled = True
End If
ElseIf e.KeyCode = Keys.PageDown Then
If cmdBearbeiten.Focused = True Or cmdLoeschen.Focused = True Or cmdNeu.Focused = True Or cmdSchliessen.Focused = True Then
e.Handled = False
Else
bsTblZeitcode.MoveNext()
e.Handled = True
End If
End If
Catch ex As Exception
End Try
End Sub
I have 2 buttons on my form and here is the two bits of code.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If LoggedInToAdastra = True Then
LogOutOfAdastra()
Exit Sub
End If
strUsername = txtUsername.Text
strPassword = txtPassword.Text
LoggedInToAdastra = False
ProgressBar1.Value = 10
Try
WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.Navigate("https://nww.awv.nems.nhs.uk/AWA/login.aspx?ReturnUrl=%2fAWA%2fdefault.aspx")
WaitForPageLoad()
WebBrowser1.Document.All("ctl00$MainContent$txtUserName").SetAttribute("value", strUsername)
WebBrowser1.Document.All("ctl00$MainContent$txtPassWord").SetAttribute("value", strPassword)
WebBrowser1.Document.All("ctl00$MainContent$btnLogin").InvokeMember("click")
ProgressBar1.Value = 40
WaitForPageLoad()
ProgressBar1.Value = 80
WebBrowser1.Document.All("ctl00$MainContent$ctl01").InvokeMember("click")
WaitForPageLoad()
ProgressBar1.Value = 100
LoggedInToAdastra = True
txtPassword.Enabled = False
txtUsername.Enabled = False
Button2.Enabled = False
Button1.Text = "Log Out"
Button2.BackColor = Color.MintCream
Button1.BackColor = Color.MintCream
txtPassword.BackColor = Color.MintCream
txtUsername.BackColor = Color.MintCream
ProgressBar1.Value = 0
btnUpdate.Enabled = True
btnDoAll.Enabled = True
btnSearchSelected.Enabled = True
doc = Nothing
Catch ex As Exception
MsgBox("Login Failed")
Button2.BackColor = Color.MistyRose
Button1.BackColor = Color.MistyRose
txtPassword.BackColor = Color.MistyRose
txtUsername.BackColor = Color.MistyRose
txtUsername.Text = ""
txtPassword.Text = ""
ProgressBar1.Value = 0
End Try
End Sub
And the second button, once logged in, you should be able to search for a patient using this button and choosing a patient from a listbox...
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
If LoggedInToAdastra = False Then
MsgBox("Please re-login to Adastra")
End If
strPatientNHS = lstPatients.SelectedItems(0).SubItems(0).Text
EditNote(strPatientNHS)
End Sub
Public Sub EditNote(strPatientNHS As String)
'Try
doc = WebBrowser1.Document
WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.Document.OpenNew(True)
WebBrowser1.Navigate("https://nww.awv.nems.nhs.uk/AWA/login.aspx?ReturnUrl=%2fAWA%2fdefault.aspx")
ProgressBar1.Value = 10
WaitForPageLoad()
ProgressBar1.Value = 20
WebBrowser1.Document.All("ctl00$MainContent$txtUserName").SetAttribute("value", strUsername)
WebBrowser1.Document.All("ctl00$MainContent$txtPassWord").SetAttribute("value", strPassword)
WebBrowser1.Document.All("ctl00$MainContent$btnLogin").InvokeMember("click")
'WebBrowser1.Navigate("https://nww.awv.nems.nhs.uk/AWA/MainMenu.aspx")
WaitForPageLoad()
ProgressBar1.Value = 30
WebBrowser1.Document.All("ctl00$MainContent$ctl01").InvokeMember("click")
WaitForPageLoad()
ProgressBar1.Value = 40
WebBrowser1.Document.GetElementById("ct100_MainContent_txtNationalNumber").SetAttribute("value", strPatientNHS)
WebBrowser1.Document.GetElementById("ct100_MainContent_cboSurgery2").SetAttribute("value", "All Practices")
WebBrowser1.Document.All("ct100$MainContent$bSearch").InvokeMember("click")
End Sub
It errors here on the EditNote sub
WebBrowser1.Document.GetElementById("ct100_MainContent_txtNationalNumber").SetAttribute("value", strPatientNHS)
WebBrowser1.Document.GetElementById("ct100_MainContent_cboSurgery2").SetAttribute("value", "All Practices")
WebBrowser1.Document.All("ct100$MainContent$bSearch").InvokeMember("click")
I have tried all sorts, but not sure why its bringing through the Error (Object reference not set to an instance of an object.)
The control does exist on the web page, and that is the correct ID for it. I have tried using Document.All and looking for the element too but to no avail.
I did resolve this in the end, in quite bizarre fashion...
so it was giving me an error basically because it couldn't find the control
WebBrowser1.Document.All("ctl00$MainContent$txtUserName")
so as a last resort, I copied and pasted the exact control from the page source and it worked... even though every character was the same...
This fixed my problem anyway. Don't type the control out, copy and paste it! Maybe someone with more knowledge can explain how this works? is there hidden characters in the HTML?
My problem:
I'm limiting a text box to 8 characters and showing a tooltip when it's exceeded (>8) rather than reached (=8). Using the .Maxlength function prevents the user from ever exceeding 8 characters so my >8 function is never fulfilled.
If I forgo the .Maxlength function and instead use .Substring to limit the input, my >8 function is fulfilled however the behavior differs from .Substring (the last rather than first 8 inputs are kept and I lose the alert sound).
It would a lot cleaner to be able to check for whenever .Maxlength is exceeded without affecting the first 8 inputs.
To reproduce:
In Visual Studio, in design mode, drag a text box and tooltip onto a fresh form.
Use the following as is:
Code:
Public Class Form1
Private Sub Textbox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
TextBox1.MaxLength = 8
If (Not IsNumeric(TextBox1.Text) And TextBox1.Text.Length > 0) Then
If ToolTip1.GetToolTip(TextBox1) = "" Then
ToolTip1.ToolTipTitle = "Input must be numeric!"
ToolTip1.Active = True
ToolTip1.IsBalloon = True
ToolTip1.ToolTipIcon = ToolTipIcon.Warning
ToolTip1.Show(vbNewLine, TextBox1, 45, -40)
End If
ElseIf TextBox1.Text.Length > 8 Then
'TextBox1.Text = TextBox1.Text.Substring(0, 8)
ToolTip1.IsBalloon = True
ToolTip1.ToolTipTitle = "8 character maximum!"
ToolTip1.Active = True
ToolTip1.ToolTipIcon = ToolTipIcon.Warning
ToolTip1.Show(vbNewLine, TextBox1, 45, -40)
Else
ToolTip1.Active = False
ToolTip1.Hide(TextBox1)
End If
End Sub
End Class
When you replace the text, it resets the caret, so move it back into place at the end:
TextBox1.Text = TextBox1.Text.Substring(0, 8)
TextBox1.Select(TextBox1.TextLength, 0)
It is better to supress the key if it is invalid:
Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim str As String
str = TextBox1.Text
str = str.Insert(TextBox1.SelectionStart, CStr(e.KeyChar))
If e.KeyChar = ChrW(Keys.Back) Then
HideToolTip()
ElseIf str.Length > 8 Then
ShowToolTip("8 character maximum!")
e.Handled = True
ElseIf Not IsNumeric(str) Then
ShowToolTip("Input must be numeric!")
e.Handled = True
Else
HideToolTip()
End If
End Sub
Private Sub HideToolTip()
If ToolTip1.GetToolTip(TextBox1) <> "" Then
ToolTip1.Active = False
ToolTip1.Hide(TextBox1)
End If
End Sub
Private Sub ShowToolTip(ByVal str As String)
'always check if tooltip is visible, to avoid inversion
If ToolTip1.GetToolTip(TextBox1) = "" Then
ToolTip1.ToolTipTitle = str
ToolTip1.Active = True
ToolTip1.IsBalloon = True
ToolTip1.ToolTipIcon = ToolTipIcon.Warning
ToolTip1.Show(vbNewLine, TextBox1, 45, -40, 1000)
End If
End Sub
EDIT
There is a minor "bug" in IsNumeric() function as it allows numeric with spaces and multiple "."
8..888 'is numeric
.9999 'is numeric
To solve everything:
Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim str As String = "0123456789."
If e.KeyChar = ChrW(Keys.Back) Then
HideToolTip()
ElseIf TextBox1.Text.Length = 8 Then
ShowToolTip("8 character maximum!")
e.Handled = True
ElseIf e.KeyChar = "." And (TextBox1.Text.Contains(".") Or TextBox1.SelectionStart = 0) Then 'supress a second "." or a first one
ShowToolTip("Input must be numeric!")
e.Handled = True
ElseIf Not str.Contains(CStr(e.KeyChar)) Then
ShowToolTip("Input must be numeric!")
e.Handled = True
Else
HideToolTip()
End If
End Sub
Add this after the substring call
TextBox1.SelectionStart = 8
I basically want a survey document where the user selects a rating such as "always" and "never." They select a rating by hitting a radio button. The ratings are weighted and are averaged into a total score, like a GPA, i.e. always is worth 3, somewhat is worth 2 and each question should be added and then averaged. Below is a section of my code for one question.
How do I take the value at the end after the button is selected and average them for all the questions?
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
RadioButton3.Checked = False
RadioButton2.Checked = False
RadioButton4.Checked = False
RadioButton5.Checked = False
num1 = 2
End If
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = True Then
RadioButton3.Checked = False
RadioButton1.Checked = False
RadioButton4.Checked = False
RadioButton5.Checked = False
num1 = 3
End If
End Sub
Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
If RadioButton3.Checked = True Then
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton4.Checked = False
RadioButton5.Checked = False
num1 = 1
End If
End Sub
Private Sub RadioButton4_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged
If RadioButton4.Checked = True Then
RadioButton3.Checked = False
RadioButton2.Checked = False
RadioButton1.Checked = False
RadioButton5.Checked = False
num1 = 0
End If
End Sub
Private Sub RadioButton5_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton5.CheckedChanged
If RadioButton5.Checked = True Then
RadioButton3.Checked = False
RadioButton2.Checked = False
RadioButton4.Checked = False
RadioButton1.Checked = False
num1 = 0
End If
End Sub
First of all, you can consolidate all of those RadioButton event handlers into ONE handler. Furthermore, you don't have to "uncheck" the other RadioButtons in the group as this will be done automatically for you. Finally, create a separate method that computes the rating and updates the form, then call that method at the bottom of the handler.
That might look something like this:
Private num1 As Integer
Private Sub set1_CheckedChanged(sender As Object, e As EventArgs) Handles _
RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged, _
RadioButton4.CheckedChanged, RadioButton5.CheckedChanged
If RadioButton1.Checked Then
num1 = 2
ElseIf RadioButton2.Checked Then
num1 = 3
ElseIf RadioButton3.Checked Then
num1 = 1
ElseIf RadioButton4.Checked Then
num1 = 0
ElseIf RadioButton5.Checked Then
num1 = 0
End If
UpdateRating()
End Sub
Private Sub UpdateRating()
' ... compute the rating using "num1", "num2", "num3", etc ...
' ... then update the GUI with the new value ...
End Sub
Note that all five RadioButtons are listed after the "Handles" keyword making them all fire the same handler. You can do a similar thing for each group of RadioButtons...just make sure each set of RadioButtons is in its OWN container like a GroupBox or Panel. A RadioButton is mutually exclusive with all other RadioButttons in its own container.
I'm not so sure about your case .. but you can try this
Declare your var that will count all result ..
Dim num1 as Integer
Dim num2 as Integer '--> for other group radiobuttons
Dim num3 as Integer '--> for other group radiobuttons
Dim nTotal as Integer
Dim nAvg as Integer
Do this in each checkchanged
Private Sub RadioButton_CheckedChanged(..) Handles RadioButton1_CheckedChanged, RadioButton2_CheckedChanged, .....
'codes
countit()
End Sub
Sub CountIt()
nTotal = num1 + num2 + num3
nAvg = (num1 + num2 + num3) / 3
End Sub