How do you store an output from a combobox in VB.NET? - 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

Related

Use ComboBox to Display Specific ImageList

I have a program I created in VisualBasic that is very similar to a slideshow image viewer. On the left side the user is able to select an image from a location and display it. On the right side I would like them to be able to use the combo box to select a category which would only display images in that category.
I am currently able to get an image list loaded in the right side and the user can cycle through that list, but I am having trouble figuring out how to connect the combobox and imagelist. Maybe there is another route I should try?
Public Class Form1
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
End Sub
Private Sub Btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
btnPrev.Enabled = True
Static i As Integer
Dim incp As String
incp = +1
i += 1
PictureBox2.Image = ImageList1.Images(i)
If i = ImageList1.Images.Count - 1 Then
i = -1
End If
End Sub
Private Sub Btnprevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
Static i As Integer
Dim incp As String
incp = +1
i += 1
PictureBox2.Image = ImageList1.Images(i)
If i = ImageList1.Images.Count - 1 Then
i = -incp
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
End Sub
End Class
So you're saying that you have multiple ImageList objects and you want the user to select one using the ComboBox? If so then you do it like you would for selecting from any other list of objects using a ComboBox, e.g.
category1ImageList.Tag = "Category 1"
category2ImageList.Tag = "Category 2"
categoriesComboBox.DisplayMember = "Tag"
categoriesComboBox.DataSource = {category1ImageList, category2ImageList}
The user selects from the Tag values and then the SelectedItem of the ComboBox is the selected ImageList.

how to use placeholder for Textbox in VB.Net 2010 like html

I have a form that does some mathematical calculation.
I want to be able users to enter data quickly without erasing the value.
I thing its good to use placeholder like I did in html
but how can i use it in VB.Net 2010?
Thanks
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gtp.Text = "0.00"
vatt.Text = "0.00"
wht.Text = "0.00"
npr.Text = "0.00"
End Sub
This code clears the text box if the current value is the placeholder value, otherwise it retains the input value.
Public Class Form1
Private Sub TextBox1_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "0.00" Then
TextBox1.Text = ""
End If
End Sub
Private Sub TextBox1_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
If TextBox1.Text = "" Then
TextBox1.Text = "0.00"
End If
End Sub
End Class
If you always want it to clear the textbox then use this.
Public Class Form1
Private Sub TextBox1_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
TextBox1.Text = ""
End Sub
Private Sub TextBox1_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
If TextBox1.Text = "" Then
TextBox1.Text = "0.00"
End If
End Sub
End Class
To simulate an actual placeholder such as the one in HTML5 you'll need to overlay a label control on top of your textbox and set it's visability based on the event keyDown and LostFocus/Leave Event which will depend on your version of VS
I created DLL for this work.
https://1drv.ms/u/s!AmR1BM6vUcAGgYsLPvjrec0Z92OTlQ
Or you can download the project PlaceHolder class
https://1drv.ms/u/s!AmR1BM6vUcAGgYsMNFGbtW5HL_4Ifw
How to use?
In your winform project add the DLL.
(I added groupbox for this example)
CS Code:
var textBoxWithPlaceHolder = new Placeholder.PlaceholderTextBox();
textBoxWithPlaceHolder.PlaceholderText = "Search text";
textBoxWithPlaceHolder.Location = new Point(x: 10, y: 20);
GBTextBox.Controls.Add(textBoxWithPlaceHolder);
Result winform

vb2010 Getting Button Name on MouseDown Event

How do I capture the name of a button so I can use it in another form to query a database. I am new to vb and still at the very early learning stage so any help would be greatfully appreciated.
frmMain
Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Right) Then
frmRacks.Show()
ElseIf (e.Button = Windows.Forms.MouseButtons.Left) Then
MessageBox.Show("Left clicked")
End If
End Sub
frmRacks
Here is where I need to capture name to query database
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
Dim name As String = DirectCast(sender, Button).Name
End Sub
There are many ways to do this, one is to declare public field/variable in frmRack, another is using ShowDialog instead of Show:
I'll go with the first one (public) first:
Public buttonName as String
And in the button click from your frmMain you pass the value like:
Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Right) Then
frmRacks.buttonName = "btnA" ' Or you could use DirectCast as proposed by dbasnett
frmRacks.Show()
ElseIf (e.Button = Windows.Forms.MouseButtons.Left) Then
MessageBox.Show("Left clicked")
End If
End Sub
And then in Loading your frmRacks you have now the option of assigning button Name, like:
Private Sub frmRacks_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim query as String = "SELECT * FROM " + buttonName 'This is only an example you could make your own here
End Sub
It should be MouseButtons.Left instead of Windows.Forms.MouseButtons.Left
If e.Button = MouseButtons.Left Then
MsgBox("Left Button Clicked") 'OR WHATEVER YOU WANT IT TO DO?!
End If

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

How can i search using a Text box and Listbox in vb.Net?

give me code, in which i can enter a word in a textbox and a listbox appear with a item that has same string in which i enter in a textbox.
Please Help me...
I found the following via Google, which sound like the type of things you want to do:
Autosearch ListBox in VB.NET
(WinForms)
Search Listboxes as You Type
(WinForms or is this VB6?)
Searching for items in a ListBox
(WPF)
Using # 1, here is some code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
List1.Items.Add("Computer")
List1.Items.Add("Screen")
List1.Items.Add("Modem")
List1.Items.Add("Printer")
List1.Items.Add("Scanner")
List1.Items.Add("Sound Blaster")
End Sub
Private Sub Text1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Text1.TextChanged
Dim i As Integer = List1.FindString(Text1.Text)
List1.SelectedIndex = i
If Text1.Text = "" Then
List1.SelectedIndex = -1
End If
End Sub
Think pseudocode, you can do this.
Grab the text from the textbox.
Set a pointer / counter to the listbox and loop through each item until the end of the list. If the textbox value has the same value as the listboxitem.text then you've found a match exit the for loop.
Add this code to texboxchange
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
ListBox1.SelectedIndex = ListBox1.FindString(TextBox1.Text.Trim)
End Sub