How do I sum numbers in multiple text boxes while simulatneously putting the sum of those boxes in a seperate box without a button - vb.net

How do I sum numbers in multiple text boxes while simulatneously putting the sum of those boxes in a seperate box without a button. Ok, I am looking for the code to sum numbers from multiple boxes (2 in this case) and automatically fill another box with the sum without clicking a button or anything. I'm talking about displaying the sum in the third box while numbers are being inputted into the first two boxes. Is this possible? If so, how?

Yes it is possible. You need to add an event handler for the TextChanged event. Add the handler for the two text boxes where the addends are being placed and update the sum in the event handler. The link has an example of parsing the text into numbers and doing something with them. In your case you need to parse two text boxes, you can either share the handler and parse both boxes each time, or add a separate handler for each box and update a member variable in the event and update the sum from the member variables (add a method called UpdateSum or something like that that you call after updating the member variables).
I am a C++/C# programmer so my VB syntax may be slightly off
Private Sub txtMonT_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles txtMonT.TextChanged
Dim number1 As Double
Dim number2 As Double
If Double.TryParse(txtMon1.Text, number1)
And Double.TryParse(txtMon2.Text, number2) Then
txtMonResult.Text = (number1 + number2).ToString()
End If
End Sub
Edit - Example of UpdateSum method
Note: _addend1 and _addend2 are assumed to be Private members of the class containing UpdateSum and that they are of type Double and have been set in the event handlers for the two text boxes. It is assumed that UpdateSum is being called from those handlers.
Private Sub UpdateSum()
Dim result As Double
result = _addend1 + _addend2
txtMonResult.Text = result.ToString()
End Sub

Related

All elements of an array cannot be accessed from a for loop in monthCalendar Control - Visual Basic

I have a bit of an issue here with the monthCalendar Control in Visual Basic. What is happening in the program is I have a calendar and a text box. The user selects a date on the calendar, then types in information into the textbox component, then presses the add button. From there the add button sets up a parallel array that contains the selected date and the added string from the text box as a way to add planner information for each date. Then the date is bolded for each time an addition like this has happened.
From there you are suppose to be able to click each bolded date and view the information you added. The issue is this only works on the last added date. For some reason it will only read the last element added to the array. I have the for loop down below. I do not understand why I cannot select any bolded date and view the text added. I have option strict on so I know no data loss is occurring. Also I know the date is being selected and placed into the string each time a date is selected on the calendar because I am displaying it onto a label each time. What is the problem. Is it some logical error I am not catching or is it something else?
Public Sub MonthCalendar1_DateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MonthCalendar1.DateChanged
For index As Integer = 0 To intSizeOfSelectedDatesAry - 1
strSelectedDay = CStr(MonthCalendar1.SelectionRange.Start) ' place the selected date into a string
lblError.Text = strSelectedDay
lblError.Visible = True
Dim strAryDate As String = arySelectedDates(index)
If (arySelectedDates(index) = strSelectedDay) Then
lblInstructions.Text = strSelectedDay
lblSelectedDate.Text = aryTextForDates(index)
index = intSizeOfSelectedDatesAry - 1
End If
Next
End Sub

Populating a textbox, if another textbox is populated

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.

VB.Net - How do you "dynamically" select an object?

I'm not sure if the question properly asks what I want, because I'm a bit new, but what I am trying to do is write a sub or function that I can run when a label is clicked, and I want to to be reusable on multiple labels. There will be 12 in total, and I don't wish to write the same thing over and over, with slightly different characters. A programmer never wants to write the same thing twice, right? Also, something else is making it a necessity to do it dynamically.
So, how I was trying to accomplish that was by using a string, and adding the name of the label to the string on click.
click1 = "Label1"
As it turns out, you can't just say click1.Text and return the text of Label1. The reason it's important to do it implicitly is because I need to somehow remember the one I clicked before, to compare the first click and second click, and if they match, do A, and if they don't match, do B.
The first parameter (it is called sender) to the event handler your wrote to respond to the click event is the object which sent the event.
If you assign the same routine to respond to on click of all your labels, it will be called for every one of them, but the sender parameter will point to the actual label which was clicked
HTH
mfeingold is correct, if you're unsure of the syntax:
Private Sub LabelClicked (ByVal sender As Object, ByVal e As EventArgs) Handles Label1.Click, Label2.Click, Label3.Click
sender.Text = "I've been clicked."
End Sub

Dynamic Textboxes

I have a gridview - GridView1.
I am adding rows to this Gridview1 dyanamically through code.
One of the columns is a dropdownlist that reads from a sqldatasource.
The textboxes, dropdownlists & sql datasources are all arrays.
If I change the value of the dropdownlist, it maintains its state even after the page reloads on any button click event.
This is ok.
However, the values of the textboxes are not maintained.
Say I enter "Hello World" in the textbox & click "Add" button, I want to collect the text value in dropdownlist(which I can read) & the value in textbox(which returns blank.)
Please suggest a method so that on add button click I can retrive the value I had typed in the textbox.
Each textbox has a unique id & I tried using the ID to get its value
eg
protected Sub Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) handles add.click
{
Dim valueinText = gettext(1).text
}
now if I type "Hello World" in textbox: gettext(1),
Reults:
valueinText = ""
Thanks in advance
You will have to reset the values for unbound textboxes that get their value from code when you post back. In your Page Load Event...
If Page.IsPostback Then
'Code that uses dropdownlistAtoZ.SelectedValue
'to fill in correct value for Textboxes.
End If
I was trying to create the same textboxes on every page_load event. But this just erased their values & their client_ID kept changing.
Instead, I tried making all textboxes Shared and created them only once on add click event, added them to a Row (which is again shared) and on page_load if page.postback = true I just added the row to the table again. If you don't do so, the row will not be shown on reload.
This solved my problem(now the values entered in Textbox were not cleared like before).
I accessed the value by classname.textboxname(i).text.
Now the solution seems obvious, but I spent a few days trying to solve this.
Thanks!

finding a solution for find string in listbox

I has 4 item on my form.........
tow listbox ,one button and one text box
I has a listbox 'A' with many items.....
i need a item in a listbox 'B' from listbox 'A'
steps are as follow.....that i like to performe...........
1)enter a word or character in a textbox
2)press a button
3)the list appear in listbox 'B'.......that is character or string start in listbox 'A' that is we write in textbox (for matching)
i need a help in which a item that can be in listbox 'B' is getting for listbox 'A'
that is Starting string or character we enter in the text box.
please try to solve me out..........
Not quite sure I follow. Using the text box' Changed event would be a good trigger instead of a button. Just iterate the list items and check for a match with String.StartsWith. For example:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
ListBox2.Items.Clear()
If TextBox1.Text.Length > 0 Then
For index As Integer = 0 To ListBox1.Items.Count - 1
Dim txt = ListBox1.Items(index).ToString()
If txt.StartsWith(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase) Then
ListBox2.Items.Add(txt)
End If
Next
End If
End Sub
I don't have an IDE in front of me, and it's been a while since I've done WinForms development, so I may not have the exact event name or other stuff, but you get the idea. This also means my code will be in C# since I'm more familiar with that, but you should be able to find the VB equivalent.
You'll want to bind to the proper event on the text box first. Maybe the KeyPress or KeyUp event? Or TextChanged? You want one that fires any time text changes in the text box. In that event, you'll loop through the items in listbox A and compare their values to the text in the text box. Basic string comparison is all that's needed, if there's a .StartsWith() or something of that nature, otherwise some basic use of .Substring() will do fine (based on the length of the string in the text box).
The loop would likely be something along the lines of:
listboxA.Items.ForEach(i =>
{if (i.StartsWith(textboxA.Text)) listboxB.Items.Add(i);});
Or...
foreach (var i in listboxA.Items)
if (i.StartsWith(textBoxA.Text))
listboxB.Items.Add(i);
Like I said, this is all off the top of my head, so the code may not be exact. But hopefully you get the idea.