|
Okay guys so I am allowing a user to enter in values into a text box and then click a button to have them display
in a listbox. My array for saving the input from the textbox into the list box is working perfectly.
But the problem I am having is that I know my code for displaying the array's indexices(elements) is not correct at all
I know there has to be a smarter way of doing this but I am lost, PLEASE HELP!!!!!! I COMMENT THE PART I NEED HELP WITH
Private Sub btnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCal.Click
'Variables
Dim StrArr() As String
Dim Str As String
Dim count As Integer
Dim lblStack() As Label
Dim i As Integer
Str = txtInput.Text 'store string into variable
StrArr = Str.Split(" ") 'use split to seperate words by space
For count = 0 To StrArr.Length - 1
'add user entered values to list box
lstDisplay.Items.Add(StrArr(count))
Next 'end for
count = 0 'set counter back to 0
For i = 0 To StrArr.Length - 1
lblDis1.Text = StrArr(0) 'gets the first value from the array
lblDis2.Text = StrArr(1) 'gets the second value from the array
lblDis3.Text = StrArr(2) ' and so on and so on...........
lblDis4.Text = StrArr(3)
lblDis5.Text = StrArr(4)
Next 'OKAY THIS IS WHERE I KNOW IF THE USER ENTER ONLY 3 VALUES IN THE TEXT BOX MY PROGRAM WOULD CRASH
'BECAUSE THE ARRAY ONLY HAS 3 ITEMS BEING STORED IN (0)(1)AND (2) SO WHAT IS A BETTER WAY OF
'DOING THIS LOOP AND SENDING OUT EACH ELEMENT TO THE LABELS.
End Sub
|
|
|
|
|
Your basic issue is that you have a dynamic array and a static number of labels. This means you have an upper limit to the number of items you can accept, which you do not enforce. Creating the labels dynamically might make more sense.
Your for loop is ridiculous. You don't use i anywhere in the loop, it's just a waste of time. You COULD built an array of label controls, so that you can step through the correct number of strings, so long as it's not more than the number of labels, and use a loop that way. You could even write some code that goes through the controls collection of the form and dynamically builds a list of how ever many labels you happen to put on there, if you like.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
PAguilar09 wrote: For i = 0 To StrArr.Length - 1 lblDis1.Text = StrArr(0) 'gets the first value from the array lblDis2.Text = StrArr(1) 'gets the second value from the array lblDis3.Text = StrArr(2) ' and so on and so on........... lblDis4.Text = StrArr(3) lblDis5.Text = StrArr(4) Next
PAguilar09 wrote: For count = 0 To StrArr.Length - 1 'add user entered values to list box lstDisplay.Items.Add(StrArr(count)) Next
1. Why are you displaying same thing twice in the UI? Even if you need to, why not have a single loop instead of two.
2. How did you decided the number of labels when at design time you don't know how many will be required?
3. Why do you bother to set count as 0 after the loop? I don't see you using it anymore. Declare that inside for loop itself.
4. Why do you need variable Str when you can directly split the textbox text?
5. In the second loop, you are not using "i" anywhere.
For adding text to the listbox, I would use following code (c#):
listBox.Items.AddRange(this.textBox1.Text.Split(new char[1] { ' ' }));
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
|
|
|
|
|
Ok let me explain again what I am trying to figure out how to do.
I know my code that I have for a label array is incorrect. I am trying to find out how i can
write code for this:
for i = 0 to array.getupperbound(0)
'then for each element in the array i want to push it out into a label that will display
'that single value, then get the next element in the array push it out into another label
'to display and so on and son until it has reach the end of the array
next
|
|
|
|
|
You still have the problem that the number of items will not match the number of labels and will crash at some point because of that. This is why controls such as the listbox exist.
Why bother populating labels when you already have the data in a listbox?
If you have knowledge, let others light their candles at it.
Margaret Fuller (1810 - 1850)
www.JacksonSoft.co.uk
|
|
|
|
|
Its because it is a program for a programming course, I need to show a visual representation of stacks, thus why I thought i could just pop each element(indexice) from the array into a label, but i had heard of some type of label array that allows you to populate each individual label with the values from the array, without doing it the way i was passing the data to each label individualy.
|
|
|
|
|
Why not add a label at runtime?
Private Sub btnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCal.Click
Dim StrArr() As String
Dim Str As String
Dim i As Integer
Str = txtInput.Text
StrArr = Str.Split(" ")
For i = 0 To StrArr.Length - 1
'Add a label at runtime
Dim MyLabel As New Label
Me.Controls.Add(MyLabel)
With MyLabel
.Left = 100
.Top = 50 + i * 20
.Visible = True
End With
MyLabel.Text = StrArr(i) 'gets the first, second etc value from the array
Next
End Sub
Sigurd Johansen
|
|
|
|
|
Hey Sigurd Johansen, THANKS the code worked perfectly! I have one question what would i need to change to display the labels like this:
So user enters into a textbox this: 1 2 3 4 5 6 then he hits the calculate button
once it is clicked display the labels at runtime like this:
6
5
4
3
2
1
Thanks for the help
|
|
|
|
|
I'm glad I was abel to help you, but I can't be your coder. You can use the same code as last time but change the For - next to this: For i = StrArr.Length - 1 To 0 Step -1 and also change the Top code to this: .Top = 170 - i * 20
Sigurd Johansen
|
|
|
|
|
Thanks for all your help. I did try to figure it out on my own b4 asking again its just that I was leaving out STEP -1 so it wasn't working the correct way. Sorry
|
|
|
|
|
If it is for a programming course, then the answer will be almost certainly be in the course notes, just probably written is a slightly different way.
|
|
|
|
|
Hello Everybody
I am creating an outlook add-in in vb.net. I want to get the path of contact picture. I have successfully add contact picture in outlook programmatic ally but i want to check the path programmatic ally also, where picture is added(saved) in outlook and want to show this path on a label control.
I am using vb.net 2005 and outlook 2003.
Thanks and Regards
Dharmchand Dhingra
|
|
|
|
|
I have this program where I need to use multiple forms. Which hasn't been a problem. The program also needs to include a module in order to create a procedure to keep a running total of a number which is added in two different forms. It then needs to be displayed on the third form.
I am having problems making this number calculate on my display. Can anyone help?
Here is the module
'This is the Standard Module'
Option Strict On
Module Module1
Public Sub AddToOrder(ByVal intTotalSales As Integer, ByRef dblSubtotal As Double)
dblSubtotal = dblSubtotal + intTotalSales
End Sub
Public Function Taxes() As Double
Dim dbltaxRate As Double = 0.07
Dim dblSubtotal As Double
Return CDbl(dblSubtotal * dbltaxRate)
End Function
End Module
Here is the Display Form:
Option Strict On
Public Class frmDisplay
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
'Closes the Bill
Me.Close()
End Sub
Private Sub frmDisplay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'When the form is loaded, the date is going to appear as well as the total amount due for all coffee ordered'
Dim todayDate As Date = Today
txtBoxDate.Text = todayDate.ToString
'declare'
Dim decSalesTax As Decimal
Dim decSubtotal As Decimal
Dim dblSubtotal As Double
Dim intTotalSales As Integer
'calculate'
decSalesTax = CDec(Taxes())
decSubtotal = AddToOrder(intTotalSales, dblSubtotal)
'display'
txtBoxSubtotal.Text = decSubtotal.ToString
txtBoxTax.Text = decSalesTax.ToString
txtBoxTotal.Text = txtBoxSubtotal.Text + txtBoxTax.Text
End Sub
End Class
|
|
|
|
|
bphoopstar wrote: Public Sub AddToOrder(ByVal intTotalSales As Integer, ByRef dblSubtotal As Double)
This seems like such a waste of time to me. Why not just place delegates between the forms to communicate between them. Passing two values, one by ref, so they can be added together makes no sense at all.
bphoopstar wrote: Dim intTotalSales As Integer
'calculate'
decSalesTax = CDec(Taxes())
decSubtotal = AddToOrder(intTotalSales, dblSubtotal)
intTotalSales is obviously 0 at this point. Therefore, I don't see how this can do anything useful.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
hello ! can you tell how to read data from store procedure in Dataset that have read from DATABASE by wizard ? i have a dataset that have bound data from SQL server by wizard. so this Dataset have all tables and views n store procedure, but i can't read data from it. speceilly i don kno how to get data from store procedure in that dataset . so please tell how to do ...?
hortpitou
|
|
|
|
|
What have you tried ?
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
i have a store procedure in sql server. and i created a dataset by wizard, so i can see in my dataset has Store procedure. but i don kno how to get data from that store procedure. so can u tell me how can i do ... ?
hortpitou
|
|
|
|
|
Pitou HORT wrote: and i created a dataset by wizard,
Perhaps if you wrote code, you'd have some idea of what you're doing. Why on earth do you need a wizard to create a dataset, if that wizard hasn't filled it from a data source ?
What have you tried ? Did you read any books ? Did you google for info and examples ? Where did this stored proc come from ? Surely you didn't write it ?
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
because of i have some reason have to use dataset by wizard. so can u show me or give me some guidline ? i have searched in google ready. but didn't find any idea.
my store procedure is get from sqlserver to dataset. please help me.....
hortpitou
|
|
|
|
|
Can I append the menustrip items from one form to another forms?
|
|
|
|
|
I suspect you'd need to clone them b/c the current ones are attached to a different form.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
If you attach the exact same toolstrip items to another toolstrip they will vanish from the original toolstrip.
You will definately have to clone them.
If you have knowledge, let others light their candles at it.
Margaret Fuller (1810 - 1850)
www.JacksonSoft.co.uk
|
|
|
|
|
I'm doing a program that runs a math algorithm, and so should be very accurate. However, I'm facing this problem: 0.2+0.1<>0.3!
The code:
For i = 0 To 100 Step 0.1
Stop
Next
After the 4th stop i should be 0.3, but it's 0.30000000000000004!
Of course the difference is not big, but my program should be as accurate as possible.
I have no idea what can cause this.
Any explanation would be welcome.
The more you know the more you know how little you know, you know?
|
|
|
|
|
The real $M@ wrote: Any explanation would be welcome.
Short Version: decimal numbers <> binary numbers
Longer Version: Wikipedia page on floating point numbers
This is a common type of problem that is well known and documented all over the web. In your case, you may be able to iterate from 0 to 1000 instead and divide by 10 inside the loop.
|
|
|
|
|
Computers represent everything in binary, not decimal, and everything has to be crammed into 32, 64, or 128 bits (Depending on the precision).
In binary, the digits after the decimal correspond to 1/2, 1/4, 1/8, 1/16, etc., instead of 1/10, 1/100, 1/1000.
So representing 0.3 in binary is kind of like, say, representing 10/3 or 22/7 in decimal... You can write it out to a lot of digits, but you can't get it exactly right in that form. Normally, this approximation gets hidden, but when you do arithmetic, these errors get compounded until it starts to become visible.
There are thousands of articles on the web about this... Here's one: http://docs.sun.com/source/806-3568/ncg_goldberg.html[^]
|
|
|
|