Im trying to delete an item from a combobox ussing a rightclick event but i can seam to get the code for it
what I do have is the following:
Private Sub combobox1_RightClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.RightClick
Dim DELETEITEM as string= combobox1.text
System.IO.File.Delete( "C:\" & DELETEITEM & ".txt" )
MsgBox("File Deleted")
End Sub
If you are trying to delete the file with that name, try this:
Private Sub combobox1_MouseDown(sender As Object, e As MouseEventArgs) Handles combobox1.MouseDown
If e.Button = MouseButtons.Right Then
Dim DELETEITEM as string= combobox1.text
Kill("C:\" & DELETEITEM & ".txt")
MsgBox("File Deleted")
End if
End Sub
I have searched for an answer to the same question to no avail. I have however used the following workaround. Instead of using right click event, it runs a different procedure if you hold down the Ctrl Button and left click. Not perfect but it works.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
If My.Computer.Keyboard.CtrlKeyDown Then
' run function a using ComboBox1 selected item
Else
' run function b using ComboBox1 selected item
End If
Catch
add err handler
End Try
End Sub
Related
I've searched everywhere, I can't find a way.
I want to find a way to add text after every textbox line but I can't find a way to do it.
I have a textbox1 with:
example1
example2
example3
And so on...
and another textbox2 with #gmail.com
I want the textbox2 to be added to the end of every line in textbox1 like:
example1#gmail.com
example2#gmail.com
example3#gmail.com
And so on...
Any way to do it? Thanks in advance.
This solution is concise, and removes empty lines.
Private Function appendTextToOtherTextLines(textToAppend As String, otherText As String) As String
Return String.Join(Environment.NewLine, otherText.
Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries).
Select(Function(s) s & textToAppend))
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox3.Text = appendTextToOtherTextLines(TextBox2.Text, TextBox1.Text)
End Sub
Here's your example working
And if you had an empty line, it is removed in the resulting string
Of course, you could overwrite the original textbox instead, but careful not to click the button twice!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = appendTextToOtherTextLines(TextBox2.Text, TextBox1.Text)
End Sub
Other option is an event handler which will make this happen automatically when pressing enter at the end of a new line. This is only useful if you are actively entering the lines manually.
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Enter Then
TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 2) & TextBox2.Text & Environment.NewLine
TextBox1.SelectionStart = TextBox1.Text.Length
End If
End Sub
(this option requires some discipline when pressing enter)
This is full working code.Enjoy it!!!!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Me.TextBox1.Lines.Length = 0 Then
MsgBox("Please enter some data in textbox1")
Exit Sub
End If
'---------------------------
Dim b(Me.TextBox1.Lines.Length - 1) As String
Dim i As Integer = 0
For Each a As String In Me.TextBox1.Lines
b(i) = a + Me.TextBox2.Text
i = i + 1
Next
'-----------------
Me.TextBox1.Clear()
Me.TextBox1.Lines = b
End Sub
I have a combo box and textbox.
The combo box will have options for user to chose.
Textbox1 has text:- Troubleshooting Steps: set in properties of the textbox.
If user selects: Rebooted PC from combo box list, then presses a button it will add the text after Troubleshooting Steps:.
Example: Troubleshooting Steps: Rebooted PC
what i want to be able to do is add multiple selects one after other.
Example:
Troubleshooting Steps: Rebooted PC- Problem returned after reboot- Replaced part
I did some googling and found this code which ive changed to do part of it.
But the problem i have it adds the last text selected Troubleshooting Steps: pushing the first text selected to the end.
Whats Happening Example:
Troubleshooting Steps: (3rd)Replaced part- (2nd)Problem returned after reboot- (1st)Rebooted PC
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim text As String = TextBox1.Text
Dim index As Integer = text.IndexOf("Troubleshooting Steps:")
Dim countChars As Integer
countChars = "Troubleshooting Steps:".Length
If index >= 0 Then
text = text.Insert(index + countChars, ComboBox1.Text)
TextBox1.Text = text
End If
End Sub
here's the updated one, have a good one!
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'you can set the TextBox text either here or in the properties panel
TextBox1.Text = "Troubleshooting Steps: "
TextBox2.Text = "Follow up: "
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If ComboBox1.SelectedIndex < 0 Then
'MsgBox("Your message if nothing is selected in ComboBox1 when the button is pressed")
Else
TextBox1.Text = TextBox1.Text & " - " & ComboBox1.SelectedItem.ToString
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If ComboBox2.SelectedIndex < 0 Then
'MsgBox("Your message if nothing is selected in ComboBox2 when the button is pressed")
Else
TextBox2.Text = TextBox1.Text & " - " & ComboBox2.SelectedItem.ToString
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ComboBox1.SelectedText = "Anaheim" Then Dim finalpath As String = "C:\hockey055f\data\model\ice0.png"
My.Resources.anaice.Save("C:\hockey055f\data\model\ice0.png")
MsgBox("Installed!", MsgBoxStyle.Information, "Installed by GoLeafsGo")
ElseIf ComboBox1.SelectedText = "Boston" Then If System.IO.File.Exists("c:\hockey055f\data\model\ice0.png") Then
System.IO.File.Delete("c:\hockey055f\data\model\ice0.png")
My.Resources.bosice.Save("C:\hockey055f\data\model\ice0.png")
MsgBox("Installed!", MsgBoxStyle.Information, "Installed by GoLeafsGo")
End Sub
This is my code. I am trying to make it so that when an item is selected by the user in the combobox and the user presses the button, the file will transfer from my resources to the directory shown above. When I click on the first value (Anaheim), the file "anaice.png" is transferred, but then the "bosice.png" from the (Boston) value is put in and overwrites the the other image. Help please!
Try this, But I don't know if this is what you want:
private finalpath As String = "C:\hockey055f\data\model\ice0.png"
private res = nothing
private sub ComboBox_Selectedindexchanged(sender As Object, e As EventArgs) _
handles ComboBox1.Selectedindexchanged
Select case sender.Selectedindex
case is 0:res= My.Resources.anaice
case is 1:res= My.Resources.bosice
case else
msgbox ("select case is out of range")
end select
end sub
Private Sub Button1_Click(sender As Object, e As EventArgs) _
Handles Button1.Click
try:io.File.Delete(finalpath):catch:end try
res.Save(finalpath)
MsgBox("Installed!", MsgBoxStyle.Information, "Installed by GoLeafsGo")
End Sub
PS: Code is written on the fly maybe it contains syntax error(s).
I'm trying to dynamically create tabcontrols which works fine; the tabs are being created however. Once created I would also like them to become clickable and execute other code, this now posing a problem.
The code I’m using to create a tab is as follows
' do whatever wtih filename
Dim myTabPage As New TabPage()
myTabPage.Text = TextBox4.Text
TabControl1.TabPages.Add(myTabPage)
TabPage1.Hide()
Not so nice cause I can now fill my form with as many tabs as I like however none of them can be clicked to execute futher code???
EDIT:
Private Sub TabControl_SelectedIndexchaged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
Dim TabName As String
TabName = TabControl1.SelectedTab.Name
If TabName.Contains("TabPage") Then
' Do something
MsgBox("new tab created")
End If
End Sub
You have to add an event handler for the TabPage click event:
Dim myTabPage As New TabPage
myTabPage.Text = TextBox4.Text
AddHandler myTabPage.Click, AddressOf TabPage1_Click
TabControl1.TabPages.Add(myTabPage)
Which will call this code:
Private Sub TabPage1_Click(sender As Object, e As EventArgs)
MessageBox.Show(DirectCast(sender, TabPage).Text)
End Sub
Per your edit, you would have to add the name property:
myTabPage.Name = TextBox4.Text
And your SelectedIndexChanged event:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab IsNot Nothing Then
MessageBox.Show(TabControl1.SelectedTab.Name)
End If
End Sub
the string is looks like 11,33,44
i made a split into three strings into 3 textboxes, and then when i do ListBox1.Items.Remove(ListBox1.SelectedItem) it doesn't work.
it says ss.Split(",") Object reference not set to an instance of an object.
here is my code
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim ss As String = ListBox1.SelectedItem
Dim aryTextFile(2) As String
aryTextFile = ss.Split(",")
TextBox1.Text = (aryTextFile(0))
TextBox2.Text = (aryTextFile(1))
TextBox3.Text = (aryTextFile(2))
ss = String.Join(",", aryTextFile)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text + "," + TextBox2.Text + "," + TextBox3.Text)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
When you remove an item from the ListBox by pressing the Button2, the SelectedIndexChanged of the ListBox1 is being called. There, the selected item will be nothing, so to solve this, add the following lines inside the SelectedIndexChanged event before assigning the string variable.
If ListBox1.SelectedItem Is Nothing Then
Exit Sub
End If
Try this:
listbox.selecteditem.remove()
It will remove the selected item in the listbox.