Extract text from clicked submenu item of Menustrip - vb.net

I've been dealing and looking for a way of extracting the text of any given subitem when clicked and write the text in textbox1.
this is the code that I have so far, but it does not seem to work.
Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
Dim result As String
If AccionAToolStripMenuItem.Checked = True Then
result = AccionAToolStripMenuItem.Text
TextBox1.Text = result
End If
End Sub

You are using the wrong event. The ItemClicked event works for the items on the menu. You need to add a event for each of the subitems:
Sub SomeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SomeTSMenuItem.Click
TextBox1.Text = Ctype(sender, ToolStripMenuItem).Text
End Sub
You can make a function for each subitem or handle every event on the same function:
Handles item1.Click, item2.Click, item3.CLick

Try reading e.ClickedItem.Text.

Related

How to change selected item text in list box at run time?

I tried with code like this:
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles MyBase.Leave
' This way is not working
ListBox1.SelectedItem = TextBox1.Text
' This is not working too
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text
End Sub
The form is looked like this:
I need to change that list text while user typing in the text box. Is it possible to do that at run time?
You are using the form's leave event MyBase.Leave, so when it fires, it is useless to you.
Try using the TextChanged event of the TextBox instead.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) _
Handles TextBox1.TextChanged
Make sure to check if an item is actually selected in the ListBox:
If ListBox1.SelectedIndex > -1 Then
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text
End If
Use Double click to select line (item) inside list box and change or modify.
Instead of using text box use ListBox1_MouseDoubleClick event
Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
then add this code inside this event
Dim intIndex As Integer = ListBox1.Items.IndexOf(ListBox1.SelectedItem)
Dim objInputBox As Object = InputBox("Change Item :","Edit", ListBox1.SelectedItem)
If Not objInputBox = Nothing Then
ListBox1.Items.Remove(ListBox1.SelectedItem)
ListBox1.Items.Insert(intIndex, objInputBox)
End If
OR
Dim objInputBox As Object = InputBox("Change Item :","Edit", ListBox1.SelectedItem)
If Not objInputBox = Nothing Then
ListBox1.Items(ListBox1.SelectedIndex) = objInputBox
End If

How do you store an output from a combobox in VB.NET?

I am trying to make my code so that I get an output from my ComboBox and then store it then the variable "Planet", how would I go about this? I have tried Planet=ComboBox1 but this does not work.
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim Planet As String
Planet = ComboBox1
If Planet =
End If
End Sub
I have now used:
Planet = Convert.ToString(ComboBox1)
but I get the output "System.Windows.Forms.ComboBox, Items.Count: 8", I have 8 Strings in this ComboBox and it seems that is what it is outputting. When I select an item in the ComboBox I click on one of the planets from a drop down list, which is what I need to retun.
Sorry did not debugged it before SelectedText is the wrong property, you should only use it to retrieve the text that the user selected inside the textbox portion of the combobox. You do get all text selected after changing the index but that doesn't happen until after this event runs. Use SelectedItem.ToString() instead. here's the code:
' I have added a combobox and a lable and the code is written on SelectedIndexChanged Event
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedItem.ToString = "Text1" Then
Label1.Text = "Text1"
End If
If ComboBox1.SelectedItem.ToString = "Text2" Then
Label1.Text = "Text2"
End If
End Sub
Here is the cod that may help:
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim Planet As String
Planet = ComboBox1.SelectedValue.Tostring()
If Planet = "ConditionalValue" Then
'Your Code if True
ELSE
'YOUR CODE IF FALSE
End If
End Sub

Showing Textbox ToolTip

I am trying to work in the tooltip in vb.net. What I am trying to do is whatever I write the text in the textbox control show it in the tooltip. I am able to show text in tooltip but my question is when I edit input text it will show old and new text in the popup tooltip. Here is what I have done so far.
Public Class Form1
Dim s As String = ""
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
s = TextBox1.Text
Dim tooltip1 As System.Windows.Forms.ToolTip = New System.Windows.Forms.ToolTip()
tooltip1.SetToolTip(Button1, s)
End Sub
End Class
Thank you.
It's hard to figure out why this is useful, but try using the TextChanged event of the textbox to update the tooltip:
Private _ToolTip As New ToolTip()
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) _
Handles TextBox1.TextChanged
_ToolTip.Show(TextBox1.Text, TextBox1)
End Sub

How to check focused TextBox in vb.net winforms?

I have multiple textbox in a form. How do I know what textbox the cursor currently is?
Trying to do something like this:
If TextBox2.Focus() = True Then
MessageBox.Show("its in two")
ElseIf TextBox3.Focus = True Then
MessageBox.Show("its in three")
End If
But I think its not working.
TextBox.Focus actually assigns the focus to the given textbox. What you're looking for is TextBox.Focused. :)
In fact, all form controls have the Focused property.
I know this already has an accepted answer but I just think this method is a bit easier and should be up here for people who find this through Google or whatever.
Public focussedTextBox As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each control As Control In Me.Controls
If control.GetType.Equals(GetType(TextBox)) Then
Dim textBox As TextBox = control
AddHandler textBox.Enter, Sub() focussedTextBox = textBox
End If
Next
End Sub
This way you can then just refer to the focussedTextBox at any time. You should make sure that you check that there is a focussedTextBox before you do however becuase when the application first loads there will not be. You can do this using:
If Not focussedTextBox Is Nothing Then
...
End If
Alternatively, you could set focussedTextBox to a TextBox of your choice on form load, either by setting its value or by focussing the TextBox.
Obviously, it will not work if you are calling your code in a Button_Click because when you click the Button then the focus is itself goes to the Button which you have clicked.
You can do two things:
Make a combined Focus event for all TextBoxes and check its Sender object.
Private Sub TextBox_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter, TextBox3.Enter
Dim currTextBox As TextBox = sender
If currTextBox.Equals(TextBox2) Then
MessageBox.Show("it's in two")
ElseIf currTextBox.Equals(TextBox3) Then
MessageBox.Show("it's in three")
End If
End Sub
OR
Take a global string variable, and set its value at each TextBox_Focus event, then check string value in the button click event.
Dim str As String
Private Sub TextBox2_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter
str = "two"
End Sub
Private Sub TextBox3_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.Enter
str = "three"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("it's in " & str)
End Sub

ListView ToolTip only in First Cell - VB.NET

I'm adding a ToolTip to a ListViewItem. However, the ToolTip only shows up when the user hovers over the first cell in the row to which the ToolTip has been applied.
MyListViewItem.ToolTipText = "Important Message"
The only other code I have related to ToolTips is this:
MyListView.ShowItemToolTips = True
Any idea how I can make the ToolTip show up on a specific cell in a row, or even the entire row? Thanks.
If you want a non-wrapper answer unlike the dup mentioned, try this:
The listview FullrowSelect property must be true. Next you need to store the tips for each subitem, I do this inside the subitem tag property. What you want to do, is on the listview mousemove event, you grab the item under the mouse, get it's subitem, and use that tip.
This simple example shows you how to get that subitem tooltip, you can just hack this a bit to suit your needs.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lvw.ShowItemToolTips = True
lvw.Columns.Add("Column A")
lvw.Columns.Add("Column B")
lvw.Columns.Add("Column C")
lvw.Items.Add(New ListViewItem(New String() {"Colors", "Green", "Blue"}))
lvw.Items(0).SubItems(0).Tag = "See the other columns"
lvw.Items(0).SubItems(1).Tag = "Like grass"
lvw.Items(0).SubItems(2).Tag = "Like the sky"
End Sub
Function GetItemTip(ByVal list As ListView, ByVal e As System.Windows.Forms.MouseEventArgs) As String
Dim item As ListViewItem = list.GetItemAt(e.X, e.Y)
If Not IsNothing(item) Then
Dim si As ListViewItem.ListViewSubItem
si = item.GetSubItemAt(e.X, e.Y)
If Not IsNothing(si) Then
Return si.Tag.ToString
Else
Return ""
End If
Else
Return ""
End If
End Function
Private Sub lvw_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvw.MouseMove
Me.Text = GetItemTip(CType(sender, ListView), e)
End Sub