Backcolor change of radiobutton, with button appearance - vb.net

I'm creating a laboratory application in VS2013, where the user enters a rack number, and gets back a visual grid of which samples he/she has to weigh for a certain analysis, for that specific rack with samples.
I'm getting the information from an Oracle DB, based on the requested/send SQL string.
I've created radio buttons as a means of selecting/filter the analysis type.
I've given them the appearance of regular buttons, in the object properties.
When a certain radio buttons is clicked, I want to give them a specific color, depending on the type of analysis.
The same color will be used to mark the samples that are to be weighed in the visual grid.
When I test the program, the radio buttons do what they need to do (meaning: getting the correct info from Oracle DB), but I can't seem to manage the backcolor change of the radio buttons.
The backcolor change of the regular buttons (on visual grid) is working correctly.
I trigger the function behind all this, by Function Rbanalysistype (sender as object, e as EventArgs) Handles Rbanalysistype1.Click, Rbanalysistype2.Click, ...
Public Function RbAnaTypeClick(sender As Object, e As EventArgs) Handles RbAnaTypeAcIn.CheckedChanged
Dim SenderName As String = ""
Dim TitrType As String = ""
SenderName = CType(sender, RadioButton).Name
Select Case SenderName
Case Is = "RbAnaTypeAcIn"
TitrType = "AcIn"
'put correct Radiobuttion in GbAnaType in color
If RbAnaTypeAcIn.Checked = True Then
RbAnaTypeAcIn.BackColor = Color.Orange
End If
End Select
End Function

You're handling the wrong event for a start. Here's the sort of thing you should be doing:
Private Sub RadioButtons_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged,
RadioButton2.CheckedChanged
Dim rb = DirectCast(sender, RadioButton)
If rb.Checked Then
rb.BackColor = Color.Red
Else
rb.BackColor = Color.Green
End If
End Sub

Related

Implementing a Text change event from an Array

I am looking to change textbox fore and back colors of multiple textboxes based on a value if any one of the textboxes change it's value either by user input or reading from the DB.
I am not sure how to implement the code once an individual textbox has a change. The below code is as far as I got as I do not know how to implement it to work. Can someone assist?
Private Sub DiffCalcColor_TextChanged(sender As Object, e As EventArgs) Handles tbPMDiffCalc.TextChanged, tbLHDiffCalc.TextChanged, tbRFDiffCalc.TextChanged, tbFSDiffCalc.TextChanged, tbFSADiffCalc.TextChanged
Dim tb = DirectCast(sender, TextBox)
Dim text = tb.Text.Replace("$", "")
Dim number As Decimal
Decimal.TryParse(text, number)
Select Case number
Case < 0D : tb.ForeColor = Color.DarkRed
tb.BackColor = Color.White
Case > 0D : tb.ForeColor = Color.Green
tb.BackColor = Color.White
Case = 0D : tb.ForeColor = Color.DimGray
tb.BackColor = Color.Gainsboro
Case Else
Exit Select
End Select
End Sub
If you want to handle the same event for multiple controls with a single method then you simply include all those controls in the Handles clause, e.g.
Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged,
TextBox2.TextChanged
Dim eventRaiser = DirectCast(sender, TextBox)
'Get the text that just changed.
Dim text = eventRaiser.Text
Dim number As Decimal
'Try to convert it to a number.
Decimal.TryParse(text, number)
'Use the number to decide how to format.
If number = Decimal.Zero Then
'...
Else
'...
End If
End Sub
You can do that manually or you can let the designer do it for you. To do the latter, start by selecting the multiple controls in the designer, then open the Properties window, click the Events button on the toolbar, then double-click the appropriate event. That will generate an event handler, much like double-clicking on a single control does, but it will add all the selected controls to the Handles clause. It also allows you to generate a handler for any event, rather than just the default event. To add a control to that Handles clause, you can select one or more controls, select the event and then select an existing event handler from the drop-down.

Combobox custom control with a lid

I have created a custom combobox with a label to cover the combobox (as it's very ugly) when it's not in use. The label, witch is the lid should show the display member. The covering and uncovering forks fine however the text on the label displayed is the previous value and not the current one. Passing over the label with the mouse, triggers the labels mouse enter event and than the display value is correct.
Here is the code for the custom control.
Public Class ComboBoxWithALid
Inherits ComboBox
Private WithEvents Lid As New Label With {.BackColor = Color.LightCyan, .ForeColor = Color.Black,
.TextAlign = ContentAlignment.MiddleCenter}
Protected Overrides Sub OnDataSourceChanged(ByVal e As EventArgs)
MyBase.OnDataSourceChanged(e)
Lid.Location = Location
Lid.Size = Size
Parent.Controls.Add(Lid)
Lid.BringToFront()
End Sub
Private Sub Lid_MouseEnter(sender As Object, e As EventArgs) Handles Lid.MouseEnter
Lid.SendToBack()
End Sub
Protected Overrides Sub OnMouseLeave(e As EventArgs)
MyBase.OnMouseLeave(e)se
Lid.Text = SelectedText
Lid.BringToFront()
End Sub
Protected Overrides Sub OnDropDownClosed(e As EventArgs)
MyBase.OnDropDownClosed(e)
Lid.BringToFront()
Lid.Text = SelectedText
End Sub
End Class
To test the control, drag the control from the tool box to your form and bind the control to any table you have
I tried to use text in place of selected text - same results
I found the solution. Change the move statement to:
Lid.Text = Items(SelectedIndex)(DisplayMember)
and is works.
SelectedText is for the highlighted portion of the text that is currently selected, not the selected item. You probably want something like this:
If SelectedIndex = -1 Then
Lid.Text = String.Empty
Else
Lid.Text = Items(SelectedIndex).ToString()
End If
Yes, you have an annoying control.

vb.net ComboBox Text Changing When Left

I'm having an issue with my ComboBoxes whereby if I type into it to get a value & then tab out the Text changes to the first item in the list with the first letter typed.
I have:
AutoCompleteMode set to SuggestAppend
AutoCompleteSource set to ListItems
DropDownStyle set to DropDownList
I add the items for the ComboBox in the Load event of the Form the ComboBox is on.
e.g. the below is code from a Load event where I populate a ComboBox that I have set up as below.
`Me.ComboBox1.Text = ""
Me.ComboBox1.Items.Add("a")
Me.ComboBox1.Items.Add("aaa")
Me.ComboBox1.Items.Add("combo")
Me.ComboBox1.Items.Add("combobox")
Me.ComboBox1.Items.Add("combobox test")
Me.ComboBox1.Items.Add("common")
Me.ComboBox1.Items.Add("common dialog")`
After running the code, if I select the ComboBox1 & type in common - common is selected in ComboBox1 but if I leave ComboBox1 the Text reverts to combo.
It gets a bit stranger as if I user the below code in the ComboBox1_Leave event procedure it throws common:
MsgBox(ComboBox1.Text)
I've also tried assigning the value of Text to a string in the ComboBox1_KeyUp event procedure & then assign that to ComboBox1.Text in the ComboBox1_Leave event procedure but that doesn't do anything.
If I put a the above MsgBox code before assigning the strings value to ComboBox1.Text then the Text value does revert to Common but this is isn't a practical solution.
I've also noticed that if I hit Enter before hitting tab it retains the correct value but again I'm don't think this is a particularly practical solution.
Does anyone have any idea what's going on here & how I can fix it?
It is absolutely necessary to have the DropDownStyle set to DropDownList?
Because if you set DropDownStyle to DropDown the selected value will be retained when you press tab or lose the focus.
If it's absolutely necessary to have it that way, you could try this.
Public Class Form2
Dim selectedTextForCombo As String = ""
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.ComboBox1.Text = ""
Me.ComboBox1.Items.Add("a")
Me.ComboBox1.Items.Add("aaa")
Me.ComboBox1.Items.Add("combo")
Me.ComboBox1.Items.Add("combobox")
Me.ComboBox1.Items.Add("combobox test")
Me.ComboBox1.Items.Add("common")
Me.ComboBox1.Items.Add("common dialog")
End Sub
Private Sub ComboBox1_LostFocus(sender As Object, e As System.EventArgs) Handles ComboBox1.LostFocus
ComboBox1.SelectedItem = selectedTextForCombo
'This is just for a visualization of your issue
'Label1.Text = selectedTextForCombo
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
selectedTextForCombo = ComboBox1.Text
'This is just for a visualization of your issue
'Label1.Text = selectedTextForCombo
End Sub
End Class
Warning:
This example works with the tab action.
If the users writes something that doesn't exist like "commun" the
selected value will end up being the visually selected value, in this
case: "common"

Determine which textbox on winform has cursor in it

I am working on a Windows Form via VB.net. The form contains two textboxes and an onscreen keyboard that I have programmed myself.
How can I determine which textbox the cursor is currently in, so that the onscreen keyboard will type in the correct textbox?
The application is meant to be entirely touch based.
I'm going to guess that your onscreen keyboard is a series a buttons representing a keyboard.
Since you have two text boxes, you have to have a flag for both text boxes to indicate that they have focus, and when one has it the other doesn't.
Private Sub TextBox1_GotFocus(sender as Object, e as EventArgs) _
Handles TextBox1.GotFocus
// These flags are class level variables
textbox1HasFocus = true;
textbox2HasFocus = false;
End Sub
Private Sub TextBox2_GotFocus(sender as Object, e as EventArgs) _
Handles TextBox2.GotFocus
// These flags are class level variables
textbox2HasFocus = true;
textbox1HasFocus = false;
End Sub
For your keyboard events
Private Sub KeyBoard_Click(sender As Object, e As EventArgs) Handles ButtonA.Click, ButtonB.Click, ButtonC.Click, etc...
' I don't know how your using your keyboard, but buttonClickLetter could be determined in multiple ways. I would use either the Name of the control or even the Tag property for retrieving which letter to use
String buttonClickLetter = DirectCast(sender, Button).Tag.ToString()
If textbox1HasFocus Then
TextBox1.Text += buttonClickLetter
ElseIf textbox2HadFocus
TextBox2.Text += buttonClickLetter
End If
End Sub

How to detect which event is being handled from multiple events?

My problem:
I need to detect what event (e.g. TextChanged) is currently being handled from within the handling Sub that's handling multiple objects/events. I cannot find examples online (though this may be a terminological issue). I already know how to detect the object but I do not know how to detect the event.
To reproduce:
In Visual Studio, in design mode, drag two text boxes onto a fresh form.
Use the following as is:
Code:
Private Sub TextBox_CombinedEvents(sender As Object, e As EventArgs) Handles TextBox1.Enter,
TextBox2.Enter, TextBox1.TextChanged, TextBox2.TextChanged,
TextBox1.Leave, TextBox2.Leave
Dim detectedEvent as String = "?"
If detectedEvent = "Enter" Then
MessageBox.Show("Text has been entered.")
Elseif detectedEvent = "TextChanged" Then
MessageBox.Show("Text has been changed.")
Elseif detectedEvent = "Leave" Then
MessageBox.Show("Text box has been left.")
Else
MessageBox.Show("Event not detected.")
End If
End Sub