I am working on a VB program for school. I am having some trouble extracting a substring from a string and I would really appreciate some help.
The form has different text boxes and one of them is where you type in a person's full name into one text box. The listbox on the form, when hitting the compute button, is supposed to display only the person's last name.
I am not sure how I am supposed to extract just the last name of the string of whatever name is typed into the text box.
All I got so far is:
Dim name As String
name = txtName.Text
(txtName is the name of the text box)
Okay, so I added:
lstOut.Items.Add(name.Substring(6))
That to my code. The name I typed in for an example when I ran the program was Helen Woods. 6 is in the substring because that is where the space starts and when I clicked compute, it listed only the last name, just like I wanted. But, this only works if the first name is five letters long. I need a way to make the program automatically find the space in between the two names.
EDIT:
When I add:
lstOut.Items.Add(name.IndexOf(""))
The listbox gives me a 0 whenever I type in a name and hit the compute button.
try this:
Private Sub GetLastName()
dim lsName as new List(Of String)
dim name as string
for each name in txtName.text.split(" ")
lsName.Add(name)
next
lstOut.items.Add(lsName.item(lsName.count-1))
end sub
You can call this procedure at your button event.
Related
There are two controls in my form GridControl and TextEdit Control. I have bulk data of Product names. I need to write 15 to 20 product names in single TextEdit control separating them by spaces. When I type in TextEdit first product name It can find correctly first product name in GridControl by GridView1.ApplyFindFilter("SomeProductName"). But when I type second Product name it includes previous Product Name in query but I need the previous product name automatically remove from query by pressing space key but not from TextEdit.
You could use something like this to search only on the last term:
Public Sub ApplyFindFilterToLastTerm(terms As String)
Dim lastTerm = terms.Split(" "c).Last
ApplyFindFilter(lastTerm)
End Sub
And call that each time the user enters a space by handling the KeyDown event
I'm programming in Visual Basic.
I have one form.
Form 1 contains:
nameTextBox
addNameButton
namesListBox
generateButton
week1TextBox
week2TextBox
week3TextBox
week4TextBox
The user needs to go to Form 1 and type a name in the text box, then add the name to the List Box. The user will add 4 names to the List Box. So, the ListBox will contain the names: Adam, Brenda and Carol.
When the generateButton is clicked, the 3 names have to be written to the text boxes in that order. So week1TextBox should contain "Adam", week2TextBox should contain "Brenda", etc... but once the last name (in this case "Carol") is written into the text box, the loop should start over. Ultimately, there may be up to 50 week text boxes (so week50TextBox). So the loop needs to repeat over and over.
As there is a lack of source code in your question, I'm really not sure exactly how the layout should look, I can only offer some advice/suggestions.
I would recommend creating your listbox control, input textbox, and button to add names to the listbox. In addition to these, though, also add a scrollable panel. (Not sure what the exact term for that control is in VB.net; it's been a long time since I've worked with that language.) Because it sounds like there might be a variable number of items on the panel, when the user goes to generate the list of names, I would use the following rough pseudocode:
Dim OutputTexts As New ArrayList ' This is only here if you want to work with these textboxes later
Private Sub CreateOutput() Handles btnGenerate.Click
pOutputPanel.Controls.Clear()
OutputTexts.Clear()
Dim NextX As Integer = 0 ' Pretty much unnecessary value, but included in case you want to mess with this
Dim NextY As Integer = 0
For i As Integer = 0 To Convert.ToInt32(txtWeekCount.Text)
Dim txtName As New TextBox
txtName.Text = lbNameList.Item(i Mod lbNameList.Items.Count)
txtName.Location = new Point(NextX, NextY) ' Play with this as necessary
NextY += 50 ' Play with this as necessary
OutputTexts.Add(txtName)
pOutputPanel.Controls.Add(txtName)
Next
End Sub
Again, this is very much pseudocode, so I would not encourage copying and pasting, but give it a read, make sure you understand all of it, and then try implementing something similar. There might be an easier way to do it, but I have not programmed in VB.NET in probably over 2 years (at least). Nonetheless, the most important thing in here is the following line: lbNameList.Item(i Mod lbNameList.Items.Count). By Mod-ing your indexing variable, you will be accessing items sequentially, and then repeating from the start of the ListBox items collection once i is out of range.
I would also encourage you to dynamically generate your TextBox controls as needed rather than manually adding in 50 or more TextBox controls.
i've got two textboxes in my form.
According to title, when I write something in one textbox (a random one), i need that at the same time, in the other textbox a text (given in the code) appears.
1 letter for 1 letter
1)Example with random text:
1 textbox ) How are you?
2 textbox) Let's just c
2)Example with random text:
1 textbox ) What is the aim of the project?
2 textbox) Let's just chill out for a mome
thanks so much
You will need to determine which responses you would like to which questions. However, the tackle to letter for letter problem. I would use the substring method and get the length of the current text.
Dim Response as String = "Hello World!"
Private Sub Text(ByVal..) Handles Textbox1.changed
Textbox2.text = Response.Substring(0, Textbox1.text.length)
End Sub
As the text changes in the textbox you are typing in, this will output the response corresponding to the length of the text input. Since the event is textbox.changed, each character entered will update the second textbox
You need to use an event based code here. I would use a loop per stroke of the key and make the event "Key Press." Write the code onto the textbox you are inserting data into.
I'm working on a project for my Visual Basic class in which I'm supposed to read in a file and display the information (employee names and salaries) in a list box.
I have a total of 4 forms.
The first form doesn't actually display anything, it simply has the menu items to open the file, select any of the other 3 forms, and exit the form.
In the second form (names), only the employee names read in from the file are displayed in the list box.
In the third form (salaries), only the employee salaries read in from the file are displayed in the list box.
The fourth form is basically just like the second and displays the employee names read in from the file.
The problem is, I don't know how to do this so that only certain parts of the file are displayed in the list boxes (names and salaries). Also, in the fourth form I have to ask the user to enter the amount of months that they would like to calculate the salary for the selected employee and then multiply their salary by the number of months entered by the user. I know how to do this, except for how I would go about getting the salary. For example, I'm thinking it would be something like this:
lblTotal.Text = dblSalary * intMonths
But I don't know how to store just the salary of the selected employee in the dblSalary variable?
Here's what code I have written so far, however it's simply opening an open file dialog box when the user clicks File->Open from the main form
Public Class Main
Private Sub OpenFileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenFileToolStripMenuItem.Click
Dim open As New OpenFileDialog
open.Filter = "text files |*.txt|All Files|*.*"
open.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
End Sub
End Class
And here is my text file titled employees.txt
Steve McGarret
1500.00
Danny Williams
1300.00
Matthew Casey
1700.00
Kelly Severide
1750.00
I hope that's clear, if not let me know and I can try to clarify.
Thanks in advance.
Since this is homework, I won't post a specific solution, but I'll post the idea of how I would do it...
I would:
Create 2 new List variables - Dim Names as new List(Of String) & Dim Salaries as new List(Of Double)
Read the text file line by line and, given the file format, each even line will add to the Salaries list and every odd line will add to the Names list
Set each list as the DataSource for the appropriate listbox
Hope this helps and gives you an idea of how to proceed, at the very least.
UPDATE:
Given your comment saying you wanted to store the data in a class, you could do something more along the following:
Create your Class with the 2 attributes and whatever other methods you need - Call it EmployeeData, say
Create a List Of(EmployeeData)
Loop through the text file 2 lines at a time (meaning read in the first line, capture the name, read in the next line, read in the salary, THEN loop)
In each loop, once you've captured the data, add a new EmployeeData to your list with the 2 pieces of captured information.
Write a small bit of code that will extract from your EmployeeData list only the salaries or only the names as a new list that you can use to bind to your listbox as the datasource.
Hope this makes sense.
I am using macrobutton in VBA to have a field whose value is calculated by accessing some other system, and at the same time, I want to be able to double-click on that field to specify some settings used to retrieve data from that other system.
The whole macro stuff works fine but I can not figure out how to change the label on the macrobutton.
Typically the macrobutton looks like this { MACROBUTTON macro_name label {some_arg}{some_arg2} }
I tried accessing selection.fields(1).code.text and even doing regexp to replace 'label' by something else but that just does not work, i.e. either I lose the args or I screw up the label.
Any advice for this issue, or perhaps a suggestion of some other type of field I could use to achieve this? I wouldn't mind using DOCVARIABLE but these can not respond to clicks and carry arguments?
You should be able to do something like this:
Sub Testit1()
Dim strText As String
Dim strLabel As String
Dim strNewLabel As String
strLabel = "Chew"
strNewLabel = "Devour"
' Replace the field code values in the first field to change the label
strText = ActiveDocument.Fields(1).Code.Text
ActiveDocument.Fields(1).Code.Text = Replace(strText, strLabel, strNewLabel)
End Sub
This will basically do a search and replace inside the field code for the macrobutton field you want to change. ActiveDocument.Fields(1).Code.Text is the part I think you are looking for.