how to enumerate listbox? - vb.net

Hi I'm new to Visual Studio and I am in need of a help in Visual Studio on a Windows Form App. also I don't really know if this is vb.net
I would like to make an application where if a user inputs something in the TextBox, it is presented in the ListBox whilst having some sort of an enumeration.
like for example.
If I type "Wow amazing" in the TextBox and confirm it. Then type another text like "I love you" in the TextBox and confirm it again
It should show up in the Listbox as "1. Wow Amazing" and "2. I love you".
Here's my code. I am not able to get it right and I don't really know how. I tried doing the for Loops and Do While but it would just duplicate the texts or am I doing something wrong?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer = 0
ListBox1.Items.Add(i + 1 & ". " & TextBox1.Text)
End Sub

You are so close!
On every button click you need to:
Take number of elements in list, to determine the number.
Insert text concatenated with number.
So, you should use this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Step 1
Dim i As Integer = ListBox1.Items.Count + 1
'Step 2
ListBox1.Items.Add(i & ". " & TextBox1.Text)
End Sub

Related

How do I bind a data from a separate listbox?

I have two listboxes and I have a button to display the selected item in a message box but I need to select from both listboxes. Is there a way for me to bind the data from the second listbox to the first one.
This is the interface:
This is how the data would be shown when selected
and what I meant by binding the data I want the 200 calory value to stay with rice even though something else is selected
Public Class Form1
Public strfood As String
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim calory As Single
strfood = InputBox("Enter food item", "Food List")
calory = InputBox("Enter calory", "Calory List")
FoodList.Items.Add(strfood)
CaloryList.Items.Add(calory)
End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim selecteditem As String = FoodList.SelectedItems(0).ToString
Dim selectedcalory As String = CaloryList.SelectedItems(0).ToString
MessageBox.Show("Food :" + selecteditem & " " & "Calories :" + selectedcalory)
End Sub
Agree with djv's suggestion; these things should be in a single grid
right click the project in Solution Explorer
add new item
add a file of type DataSet
double click it, an empty surface appears. Rename the dataset to something sensible like CalTrackerDataSet (props grid)
right click the surface and choose Add..DataTable
name it Foods
right click the DataTable, choose Add..Column
name it Food
add another column named Calories, and set its type to Int32 (integer) in properties grid
save
go to your form
open the data sources tool panel (on the view menu, inside Other Windows) and find the Foods node, drag it into your form
an item appears in the bottom tray, called CalTrackerDataSet - change this to CalTrackerDS - I recommend this purely because it's a great point of confusion when vb names instances of things the same as the type
you don't need your BtnAdd code any more because new rows can be created just by typing into the grid, but if you did want to add a row like this, programmatically, you would:
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim f = InputBox("Enter food item", "Food List")
Dim c = Convert.ToInt32(InputBox("Enter calory", "Calory List"))
CalTrackerDS.Foods.AddFoodsRow(f,c)
End Sub
And displaying the row the user clicked on is more subtle; a device called a BindingSource tracks the current row. Because it works with a wide range of data sources it returns quite basic objects that hide the specifics of the bound data so you have to cast to turn them back
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim cur and DirectCast(DirectCast(FoodsBindingSource.Current, DataRowView).Row, CalTrackerDataSet.FoodsRow)
MessageBox.Show("Food :" & cur.Food & " " & ", Calories :" & cur.Calories)
End Sub

Visual Basic - Displaying a combo box entry over a textbox

to keep it short and simple, I am attempting to display a number selected through a combo box on a second form through a label.
Here are the bits that are relevant to the issue I am having:
for i = 1 To 31
cmb_days.Items.Add(i)
next
' Populating combo box
days = cmb_days.text
frm_result.lbl_renting = "Renting for " & days & " Days"
I have tried using other variants such as cmb_days.selecteditem to no avail
I am also kinda having issues with like telling my code to do things with whatever number is actually selected in the combo box, idk I am veryyyyy new
That looks more or less correct.. But the question is where your call to update the label occurs in your code...
This is a minimal working example, with just a ComboBox on a form:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 1 To 31
ComboBox1.Items.Add(i)
Next
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.Text = ComboBox1.Text
End Sub
End Class
The update of the label (in this case the text of the form, but it works exactly the same with a label on a different form) occurs in the SelectedIndexChanged event of ComboBox1.
If you copied your code straight from the project, you haven't had time to select anything yet, so the Text property will be empty.

Visual Basic - Create an application to pick a number

Hye,
I'm new with Visual Basic and i had a few problems.
I've create a new Windows Forms Application in Visual Basic. Using one TextBox and two Button. The TextBox for displaying the number. One button for Generate and another one for Help.
I want to create a simple application that will pick one of the listed number instead of generating it.
Example :
Each time I click on the Generate button, it will pick either 14412GG or TE921W or 13123SA only. The number will appear in the TextBox.
Evertime i click on Help button, a windows will pop-up with help messages. Multiple line messages.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
**This is for Generate button**
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
**This is for Help button**
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
**This is to display the number**
End Sub
End Class
Save those 3 values in an arrayList, create a Random class.
private static ArrayList values1 = new ArrayList{"14412GG","TE921W","13123SA"};
Random rnd = new Random();
int r = rnd.Next(values1.Count);
(string)values1[r]
Display the string when Generate button clicks.
TextBoxVariable.Text = (string)values1[r];
Use Messagebox.Show to display the help message.
MessageBox.Show(""Help message line1" + Environment.NewLine + "Help message line2";");
Hope this helps!
Here is what you need :
'Button 1
Dim pool As String = "ABCDEFGHIJKLMNOPQURSTUVWXYZ1234567890"
Dim cc As New Random
Dim count = 0
While count <= 6
textbox1.text = cc.Next(0, pool.Length)
'button2
msgbox("message line1" + vbnewline + "message line2.")
i hope it be useful to you .

autocomplete combobox for vb.net word by word or with delimiters

I am trying to add word by word (and/or with delimiter support) autocomplete functionality to a combobox (or textbox) in a vb.net application. This is the desired behavior, stripped down to one delimiter for this question:
When the user types 'invoke XX ' in the box, a list of suggested words should pop up. The user should still be able to type through, continually filtering those words, until he/she selects one. For example:
'invoke 121 ' (should prompt a list of words such as Display, DVD, CD, still with typethrough)
I can handle much of the actual logic, but I'm pretty new to vb.net, and I'm unsure of the best way to trigger an autocomplete popup on a space (or period), word by word. (Regular combobox autocomplete doesn't support word by word.) This post looks helpful but I don't know how to convert the accepted answer's developer-express tools into regular .net components: autocomplete text box for .net with support for delimiter. Ideally it'd work a lot like intellisense.
Here is my attempt so far (just a form with a textbox), stripped down. I need help figuring out how to allow typing to filter through the contextmenustrip. I also wonder if I'd be better off with something other than a contextmenustrip. Thanks
Public Class Form1
Private WithEvents firstContextMenu As ContextMenuStrip
' Populate items for the contextmenustrip
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
firstContextMenu = New ContextMenuStrip()
firstContextMenu.Items.Add("Display")
firstContextMenu.Items.Add("DVD")
firstContextMenu.Items.Add("CD")
End Sub
' Bring up the contextmenustrip if typing a space, if after 'invoke XX '
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = " " And TextBox1.Text.StartsWith("invoke ", StringComparison.CurrentCultureIgnoreCase) And TextBox1.Text.Split().Length = 2 Then
TextBox1.Text = TextBox1.Text + " " ' Add a space
TextBox1.Select(TextBox1.Text.Length, 0) ' To put the cursor at the end of the textbox
Dim point As New System.Drawing.Point(TextBox1.GetPositionFromCharIndex(DirectCast(sender, TextBox).SelectionStart - 1).X, TextBox1.Height - 5)
firstContextMenu.Show(TextBox1, point, ToolStripDropDownDirection.BelowRight)
End If
End Sub
' Adds the clicked item to the textbox
Private Sub firstContextMenu_ItemClicked1(sender As Object, e As ToolStripItemClickedEventArgs) Handles firstContextMenu.ItemClicked
TextBox1.Text = TextBox1.Text + e.ClickedItem.ToString()
TextBox1.Select(TextBox1.Text.Length, 0)
End Sub
' Moves cursor to end of textbox if you 'escape' out of the contextmenustrip
Private Sub firstContextMenu_Closed(sender As Object, e As ToolStripDropDownClosedEventArgs) Handles firstContextMenu.Closed
TextBox1.Select(TextBox1.Text.Length, 0)
End Sub
End Class

Perform Swap between textboxes without variables

I was attending an interview, the interviewer asked me the question that: Perform Swap between textboxes without variables. i am familiar with c/c++ concept of swap values without using temp, i tried them but it wont give me the result, can anyone suggest me how it become possible?
Thanks in advanceā™„
Using some string handling functions we can easily swap(Interchanging) the contents of two text boxes. see the following code snippet :
Private Sub btnSwap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox2.Text &= TextBox1.Text
TextBox1.Text = TextBox2.Text.Substring(0,TextBox2.Text.LastIndexOf(TextBox1.Text))
TextBox2.Text = TextBox2.Text.Replace(TextBox1.Text, "")
End Sub
How It Works:
Concatenate both texts and assign it into TextBox2
Find the SubString based on the index of TextBox1.Text in the concatenated string which gives the TextBox2.Text and are assigned to the TextBox1.Text.
Replace the TextBox1.Text from the Concatenated string will give the initial text in the TextBox1.Text which is assigned to the TextBox2.Text
Example
Let TextBox1.Text="aaa" and TextBox2.Text="bbaaabb"
Step 1: TextBox2.Text= "aaabbaaabb"
step 2: TextBox1.Text= "bbaaabb" ' since textbox2 contains aaabbaaabb and textbox1 contains aaa
step 3: TextBox2.Text= "aaa" 'since textbox2 contains bbaaabb and textbox1 contains bbaaabb