Visual Basic - Grouped radio buttons - vb.net

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.

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 Deleting Text in Textbox Using Checkbox

"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

How to manage dynamically created controls in VB.NET?

I just understand how to make controls dynamically in VB.NET (I mean, only part of adding a new one)
But, unlike VB6, it seems hard to handle those dynamic things.
When I click the DONE button, I want to make an array filled with the text of textboxes.
At the same time, I want to make a Delete button that removes the button itself and the textbox in the same line.
Is there any simple method or an sample code for this?
Thank you!
Drop a TableLayoutPanel on your form, called pnlLayout, and also the Add button called btnAdd. Configure TableLayoutPanel to have two columns, adjust column width as needed.
Paste below code into your form:
Public Class Form1
Dim deleteButtons As List(Of Button)
Dim textBoxes As List(Of TextBox)
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
deleteButtons = New List(Of Button)
textBoxes = New List(Of TextBox)
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim elementCount As Integer = deleteButtons.Count
Dim txt As New TextBox
txt.Width = 100
txt.Height = 20
textBoxes.Add(txt)
Dim btn As New Button
btn.Width = 100
btn.Height = 20
btn.Text = "Delete " & elementCount.ToString
AddHandler btn.Click, AddressOf btnDelete
deleteButtons.Add(btn)
pnlLayout.SetCellPosition(txt, New TableLayoutPanelCellPosition(0, elementCount))
pnlLayout.SetCellPosition(btn, New TableLayoutPanelCellPosition(1, elementCount))
pnlLayout.Controls.Add(btn)
pnlLayout.Controls.Add(txt)
End Sub
Private Sub btnDelete(sender As Object, e As EventArgs)
Dim senderButton As Button = DirectCast(sender, Button)
Dim txt As TextBox = textBoxes(deleteButtons.IndexOf(senderButton))
pnlLayout.Controls.Remove(senderButton)
pnlLayout.Controls.Remove(txt)
End Sub
End Class
By default, it will have no textboxes and no Delete buttons, you can add as many rows of "Textbox + Delete button" as you want. When you press Delete, the row will be removed (and everything shifted to accommodate the empty space).
For the textbox'ex part:
Dim strcol() As String = {TextBox2.Text, TextBox3.Text}
For Each strtxt In strcol
MessageBox.Show(strtxt)
Next
It really depends on your code, but, if you have their name use this to delete the buttons &/ textbox'es:
For i As Integer = Me.Controls.Count - 1 To 0 Step -1
If TypeOf Me.Controls(i) Is TextBox Then
If Me.Controls(i).Name = "TextBox2" Then
Me.Controls.RemoveAt(i)
End If
End If
If TypeOf Me.Controls(i) Is Button Then
If Me.Controls(i).Name = "Button3" Then
Me.Controls.RemoveAt(i)
End If
End If
Next
But it depends on your code...

contextmenustrip opening event determining sender

I have 30 labels. They can have any value I want.
I need to be able to assign one context menu to them all then determine which label was clicked in order to use my x variable.
Private Sub Label_Click(sender As Object, e As MouseEventArgs) Handles Label1.MouseClick, Label2.MouseClick, Label3.MouseClick, Label4.MouseClick, _
Label5.MouseClick, Label6.MouseClick, Label7.MouseClick, Label8.MouseClick, Label9.MouseClick, Label10.MouseClick, Label11.MouseClick, _
Label12.MouseClick, Label13.MouseClick, Label14.MouseClick, Label15.MouseClick, Label15.MouseClick, Label16.MouseClick, Label17.MouseClick, _
Label18.MouseClick, Label19.MouseClick, Label20.MouseClick, Label21.MouseClick, Label22.MouseClick, Label23.MouseClick, Label24.MouseClick, _
Label25.MouseClick, Label26.MouseClick, Label27.MouseClick, Label28.MouseClick, Label29.MouseClick, Label30.MouseClick
Dim x As String = sender.Text
xmlinteraction.appCall(x)
End Sub
I received awesome help the other day passing variable into contextmenustrip
But I am too new to put it all together and make it work. I understand what we are trying to do, but not all the syntax. Please help.
Jay,
Here is what I put together from the code you gave me. Is this what you were thinking? I feel like I missing something still and further clean the code. Possibly removing the case statements.
Private Sub rcmenuOption(x, y)
' x is equal to what the menu item was clicked
' Create case stament for that to call the correct xmlinteraction passing in y
Select Case x
Case "Copy Link"
copyClipboard(y)
End Select
End Sub
Private Sub rcmenuClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles rcmenu.ItemClicked
' Get the Label clicked from the SourceControl property of the clicked ContextMenuStrip.
Dim contextMenu = DirectCast(sender, ContextMenuStrip)
Dim label = DirectCast(contextMenu.SourceControl, Label)
Dim var2 As String = label.Text
' Get the clicked menu strip and update its Text to the Label's Text.
Dim toolStripItem = e.ClickedItem
Dim var As String = toolStripItem.Text
rcmenuOption(var, var2)
End Sub
contextmenustrip opening event determining sender
OK, you have a number of Labels on a form and all of them use the same ContextMenuStrip (all the Labels have their ContextMenuStrip property set to the same ContextMenuStrip control).
When the user right clicks a Label and selects a menu item, you want that menu item's Text to change to the clicked Label's Text.
You can do this using your ContextMenuStrip ItemClicked event handler. Use the handler's sender and ToolStripItemClickedEventArgs parameters to get the Label's Text and a reference to the ToolStripItem clicked.
Private Sub ContextMenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ContextMenuStrip1.ItemClicked
' Get the Label clicked from the SourceControl property of the clicked ContextMenuStrip.
Dim contextMenu = DirectCast(sender, ContextMenuStrip)
Dim label = DirectCast(contextMenu.SourceControl, Label)
' Get the clicked menu strip and update its Text to the Label's Text.
Dim toolStripItem = e.ClickedItem
toolStripItem.Text = label.Text
End Sub