VB.NET Deleting Text in Textbox Using Checkbox - vb.net

"can anyone help me in my problem
So i have this checkboxes, so what It does is if a select a checkbox the text of that checkbox will be passed to the HistoryTextbox
My main problem is that I am able to add new checkboxes to the HistoryTextbox, but when i uncheck the checkbox i want the text of the checkbox to be deleted in the HistoryTextbox
Example: When i click chkbox1 value of chkbox1 is ASTHMA
the HistoryTextbox should contain ASTHMA
Then i click again a new checkbox for hypertension
the HistoryTextbox should contain ASTHMA,HYPERTENSION
But When i uncheck the checkbox of ASTHMA the asthma will be the only one to be deleted or remove
and the HistoryTextbox will only contain the hypertension.".

It's a little difficult to give you guidance without seeing your code but you seem to be looking for something like this:
Private Sub CheckBoxes_CheckedChanged(sender As Object,
e As EventArgs) Handles CheckBox1.CheckedChanged,
CheckBox2.CheckedChanged,
CheckBox3.CheckedChanged ', etc.
Dim currentCheckBox = DirectCast(sender, CheckBox)
Dim textToAddOrRemove As String = currentCheckBox.Text & ","
If currentCheckBox.Checked Then
' Add the text.
HistoryTextbox.Text &= textToAddOrRemove
Else
' Remove the text.
HistoryTextbox.Text = HistoryTextbox.Text.Replace(textToAddOrRemove, String.Empty)
End If
End Sub
Another option (especially useful if some of the CheckBoxes have the same text) is to generate the text each time based on the checked CheckBoxes. For example:
Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged,
CheckBox2.CheckedChanged,
CheckBox3.CheckedChanged ', etc.
Dim checkedCheckBoxes = Me.Controls.OfType(Of CheckBox).Where(Function(c) c.Checked)
' Or:
'Dim checkedCheckBoxes = {CheckBox1, CheckBox2, CheckBox3}.Where(Function(c) c.Checked)
HistoryTextbox.Text = String.Join(",", checkedCheckBoxes.Select(Function(c) c.Text))
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.

Multi select listbox's last clicked item to textbox

I want to show the last clicked item on my ListBox that has multi-select. It only shows the first item on the list selected when I use the following:
Textbox1.text = listbox1.text
I think your only option would be to store a list of the selected indices and add/remove to/from it whenever the selection changes.
Something like this should do the job (assuming WinForms):
Private selectedIndices As New List(Of Integer)
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
' Add the newly selected items.
selectedIndices.AddRange(
ListBox1.SelectedIndices.Cast(Of Integer).
Where(Function(i) Not selectedIndices.Contains(i)))
' Remove the unselected items.
selectedIndices.RemoveAll(Function(i) Not ListBox1.SelectedIndices.Contains(i))
' Update the TextBox text. You can move this code to a different method
' if you want to trigger it using a button or something.
If selectedIndices.Count = 0 Then
TextBox1.Text = String.Empty
Else
Dim lastIndex As Integer = selectedIndices.Last()
TextBox1.Text = ListBox1.GetItemText(ListBox1.Items(lastIndex))
End If
End Sub
See it in action:

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"

autocomplete combobox for vb.net word by word or with delimiters

I am trying to add word by word (and/or with delimiter support) autocomplete functionality to a combobox (or textbox) in a vb.net application. This is the desired behavior, stripped down to one delimiter for this question:
When the user types 'invoke XX ' in the box, a list of suggested words should pop up. The user should still be able to type through, continually filtering those words, until he/she selects one. For example:
'invoke 121 ' (should prompt a list of words such as Display, DVD, CD, still with typethrough)
I can handle much of the actual logic, but I'm pretty new to vb.net, and I'm unsure of the best way to trigger an autocomplete popup on a space (or period), word by word. (Regular combobox autocomplete doesn't support word by word.) This post looks helpful but I don't know how to convert the accepted answer's developer-express tools into regular .net components: autocomplete text box for .net with support for delimiter. Ideally it'd work a lot like intellisense.
Here is my attempt so far (just a form with a textbox), stripped down. I need help figuring out how to allow typing to filter through the contextmenustrip. I also wonder if I'd be better off with something other than a contextmenustrip. Thanks
Public Class Form1
Private WithEvents firstContextMenu As ContextMenuStrip
' Populate items for the contextmenustrip
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
firstContextMenu = New ContextMenuStrip()
firstContextMenu.Items.Add("Display")
firstContextMenu.Items.Add("DVD")
firstContextMenu.Items.Add("CD")
End Sub
' Bring up the contextmenustrip if typing a space, if after 'invoke XX '
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = " " And TextBox1.Text.StartsWith("invoke ", StringComparison.CurrentCultureIgnoreCase) And TextBox1.Text.Split().Length = 2 Then
TextBox1.Text = TextBox1.Text + " " ' Add a space
TextBox1.Select(TextBox1.Text.Length, 0) ' To put the cursor at the end of the textbox
Dim point As New System.Drawing.Point(TextBox1.GetPositionFromCharIndex(DirectCast(sender, TextBox).SelectionStart - 1).X, TextBox1.Height - 5)
firstContextMenu.Show(TextBox1, point, ToolStripDropDownDirection.BelowRight)
End If
End Sub
' Adds the clicked item to the textbox
Private Sub firstContextMenu_ItemClicked1(sender As Object, e As ToolStripItemClickedEventArgs) Handles firstContextMenu.ItemClicked
TextBox1.Text = TextBox1.Text + e.ClickedItem.ToString()
TextBox1.Select(TextBox1.Text.Length, 0)
End Sub
' Moves cursor to end of textbox if you 'escape' out of the contextmenustrip
Private Sub firstContextMenu_Closed(sender As Object, e As ToolStripDropDownClosedEventArgs) Handles firstContextMenu.Closed
TextBox1.Select(TextBox1.Text.Length, 0)
End Sub
End Class

Visual Basic - Grouped radio buttons

Basically what i'm wanting to work is for the text of the selected radio button to be inserted into a ListView that i already have.
Here is my code (Listview):
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles AddButton.Click
Dim Col1 As String = ComboBox1.Text
Dim Col2 As String =
Dim Col3 As String = ComboBox3.Text
Dim order As New ListViewItem
order.Text = Col1 'Adds to First column
order.SubItems.Add(Col2) 'Adds to second column
order.SubItems.Add(Col3) ' Adds to third column
ListView1.Items.Add(order)
End Sub
If i put RadioButton1.Text In DIM Col2 and select it when it runs then it displays it but i would like it so that it finds out which radio button is selected and displays the correct text. I Have four radio buttons all in a group called GroupBox1 and each radio button is RadioButton1, 2, 3 etc
The Combo boxes are getting used as the same but i need some sort of radio button in my program. Any help is appreciated.
With two RadioButtons inside one GroupBox, and one TextBox:
Private Sub RadioButtons_CheckedChanged(sender As Object, e As EventArgs) _
Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
If CType(sender, RadioButton).Checked Then
Dim rButton As RadioButton =
GroupBox1.Controls.
OfType(Of RadioButton)().
Where(Function(r) r.Checked = True).
FirstOrDefault()
Me.TextBox1.Text = CType(sender, RadioButton).Text
End If
End Sub
When a RadioButton is clicked, the TextBox text gets updated. I don't understand how you want to insert into the ListView, so just take the text and put it where you need it.