How to add buttons to a ListBox? - vb.net

In my project, I'm adding a ListBox whose items are retrieved from a database. What I want to do is to add a button to the right of each item on the ListBox that, when pressed, deletes that item from the database and refreshes the ListBox.
Now, getting the button to do that will be easy (quite possibly a Click() event on the button), but my problem is that I can't find a way to actually add a button to the ListBox.
Does anyone know how to do this?
BTW this is in a VB desktop app project.

A DataGridView would be an easier solution, it already includes all the functionality you require. Simply add a text column and a button column, then you can subscribe to the Cell ContentClick event For example:
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
'your database code here
DataGridView1.Rows.RemoveAt(e.RowIndex)
End Sub

There's an article on the msdn about putting controls into DataGridViews. Check this out and see if it gets you any closer to what you want.
http://msdn.microsoft.com/en-us/library/7tas5c80.aspx

Related

All Handles disappeared vb.net

I was working on a project and for no reason all Handles disappeared from the form
For example:
Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.Click
has become
Private Sub Button17_Click(sender As Object, e As EventArgs)
I have a lot of tools in the form
Is there a shortcut to retrieve Handles?
This happens if a control is being cut and pasted back on the form or if it's deleted and recreated with the same name.
To recreate the Handles you can click behind the closing bracket and press SPACE - TAB - SPACE and fill the rest up with the help of IntelliSense.
There's nothing to retrieve. They are just regular methods now. You need to attach them to the appropriate events like you would any other method you wrote yourself. Open the Properties window in the designer and click the Events button. Select a control on the form, click the drop-down for the event of interest and select the correct method.

How do I take a combobox and a textbox in one form to transfer to another form combined?

I am creating a template generator and have added a sub form to add new templates vs what is already coded in. I am able to input the new template name in one Textbox and the data in a second textbox. When I hit the add button it sends the template name to the combobox on the first form like it is supposed to but it sends the data to the textbox in the first form not attached to the Template name. How do I link the two boxes when they send over?
I have done research in various websites and consulted with several books on trying to figure this out and the code I have is the closest I have gotten to figuring it out. I feel like I am only missing one link command but not sure what it is. I am reteaching myself how to use visual basic.
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.ComboBox1.Items.Add(TextBox1.Text)
Form1.TextBox1.AppendText(TextBox2.Text)
Close()
End Sub
What I am expecting to happen is when I click the button it sends the newly created template over to the combobox on form1 to add to dropdown list of Templates and be usable. What is happening is Textbox1 is going to the combobox perfectly but textbox2 is not attaching to it.

vb.net buttons won't click after editing them

I made a nice program while learning and everything was going good until I did something, but I forgot what I did. Now the buttons won't click but they all have the functions.
I know it's something in the properties but I forgot what it was.
vb.net express 2013
Without code provided, we can't really help that much. But what you can do is create another button, then copy whatever the code of your old button was, and paste it to your new button created.
In Visual Studio when you cut and paste any Control instead of simply drag it in a new position it will lost every event handler already added.
In your case simply add Handles clause to your YourButtonName_Click sub:
Private Sub YourButtonName_Click(sender As System.Object, e As System.EventArgs) Handles YourButtonName.Click

Visual Basic 2008 auto-scan controls

I have a form containes some controls (buttons) how i can make the program autoscan these controls one by one and whenever a button higlited I can bress enter to press this button. Using Visual Basic 2008
You don't have to autoscan the controls, pressing TAB cycles through the controls on the form. Just press TAB until you have the right control selected and then use the KeyDown method for the control:
Private Sub Button1_KeyDown(byVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown
If e.KeyCode = Windows.Forms.Keys.Enter Then
' Do whatever
End If
End Sub
In the If Statement, I recommend you reference a custom Sub that you'll also reference in the Button1_Click event. This will allow you to do make sure that hitting Enter on the selected control and clicking on it do the same thing.
The only downside to this is that you have to write Event Handlers as shown above for every control on your form if that's what you want.
HTH

How can I limit a textchanged event for a textbox to keyboard input only?

Please allow me to explain what I have and what I am trying to achieve.
I have a textbox (called txtb1) and a button under it (called btn_browse) on a winform in a vb.net project.
When the user clicks the button a folder browser dialog appears. The user selects his desired folder and when he/she clicks 'ok' the dialog closes and the path of the folder selected appears in the textbox. I also want to store that value in a variable to be used somewhere else(the value will be copied to an xml file when the user clicks 'apply' on the form, but this has no effect nor is related to my problem).
To achieve that I have the following code:
Public myVar As String
Private Sub btn_browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_browse.Click
Dim f As New FolderBrowserDialog
If f.ShowDialog() = DialogResult.OK Then
txtb1.Text = f.SelectedPath
End If
myVar = txtb1.text
f.Dispose()
End Sub
This part works with no problems.
Now, what if the user either:
1- decides to enter the path manually rather than use the browse button. or,
2- after using the browse button and selecting the folder they decide to manually change the location
In trying to solve this I added a textchanged event to the textbox as follows:
Private Sub txtb1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtb1.TextChanged
myVar = txtb1.Text
End Sub
However, this is not working. Apparently, and I don't know if this is relevant, when the user selects the desired folder using the browse button the textchanged event is also triggered. and when I click on the textbox (to give it focus) and press any keyboard key the application simply stops responding.
So my questions are: am I going about this the right way? if my logic is flawed, could someone point me to how usually such a thing could be achieved? is it possible to limit the triggering events to only keyboard input as a way around this? I tried the keydown and keypress events but I am getting the freeze.
Set the TextBox.ReadOnly property to true and then set the backcolor to white and forecolor to black to look like a normal textbox but they can't edit it.
Then you have no need to worry about handling any events from the textbox like u are doing.
I think your solution is pretty simple. Just treat the textbox as a File upload control in web forms. Make it readonly. Don't let the users to edit the text. This solves two problems:
The user will always a select a folder using a known mechanism (clicking on button and seleting folder)
No need to use any variable since you can always get the location from the textbox.
HTH
Why do you need to store this value in an additional variable? So long as the textbox is visible to the user, it contains the definitive value and could be accessed directly. So, in this case you would have clicking the "Apply" button read the value from the text box instead of the variable, thus avoiding this problem with events altogether.