467,182 Members | 1,091 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,182 developers. It's quick & easy.

Vb.net Interesting Problem- Moving Average

Hi friends,
I have an interesting problem in vb.net. And I am struggling to get a solution for this..m trying for the past 3days.. Its to calculate moving average for the inputs given by the user..

The following are the steps..
1, get 2user inputs in textbox(1st input is number is periods, 2nd is moving range)
2, after getting both the user inputs, the user will click on an input button which must dynamically generate rows for the number of periods given by the user as well as a calculate button must appear dynamically.
3, the user again inputs the values for all the dynamically generated rows and when by pressing calculate button, the moving average for the given moving range must appear in another column

Im half way through and I have attached my progress here.. I would greatly appreciate if u complete it for me.. thanks in advance

Expand|Select|Wrap|Line Numbers
  1. Partial Public Class _Default
  2.     Inherits System.Web.UI.Page
  4. Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6.         Dim TxtDyn1 As TextBox()
  7.         Dim TxtDyn2 As TextBox()
  8.         Dim TxtDyn3 As TextBox()
  9.         Dim TxtDyn4 As TextBox()
  10.         Dim numofper As Integer
  12.         Try
  14.             'to check the textbox for empty
  16.             If Not TextBox1.Text Is String.Empty Then
  17.                 numofper = TextBox1.Text
  18.                 ReDim TxtDyn1(numofper)
  19.                 ReDim TxtDyn2(numofper)
  20.                 ReDim TxtDyn3(numofper)
  21.                 ReDim TxtDyn4(numofper)
  23.             Else
  24.                 Exit Sub
  25.             End If
  27.             'To create the dynamic text box and add the controls to panel
  28.             For i As Integer = 0 To numofper - 1
  29.                 TxtDyn1(i) = New TextBox
  30.                 Panel1.Controls.Add(TxtDyn1(i))
  31.             Next
  32.             For j As Integer = 0 To numofper - 1
  33.                 TxtDyn2(j) = New TextBox
  34.                 Panel2.Controls.Add(TxtDyn2(j))
  35.             Next
  36.             For k As Integer = 0 To numofper - 1
  37.                 TxtDyn3(k) = New TextBox
  38.                 Panel3.Controls.Add(TxtDyn3(k))
  39.             Next
  40.             For l As Integer = 0 To numofper - 1
  41.                 TxtDyn4(l) = New TextBox
  42.                 Panel4.Controls.Add(TxtDyn4(l))
  43.             Next
  45.             Dim mybutton As New Button     (THIS S NOT WORKING )
  46.             mybutton.Text = "button 2"
  47.             AddHandler mybutton.Click, AddressOf button2.click
  48.             Me.Controls.Add(mybutton)
  50.         Catch ex As Exception
  51.             MsgBox("Enter a valid Number")
  52.         End Try
  54.     End Sub
  57. End Class
Oct 4 '08 #1
  • viewed: 2390
5 Replies
Expert 4TB
Why not use a DataGridView?
You can tell it to have X amount of columns and Y amount of rows, as well as a button column for doing the calculate?
That would let your user enter data in the correct format?
Oct 6 '08 #2
ya i dont know how to use datagridview.. im new to vb.net.. can you please help me in finishing from this point.. because i have my submission tomorrow
Oct 6 '08 #3
Expert 4TB
Well I cannot give you answers to your homework assignments.If its due this soon, you might not want to make such a drastic switch.

What part of that button code is not working?
Oct 6 '08 #4
i have created two text boxes for user input,.. 1st box ll get the number of rows needed in the columns and 2nd box ll get the moving average range. and for example if the user enters 10 and clicks on button1, then 10 rows of text boxes will be created dynamically.. til this i have done..

Now what i want is to display another button dynamically after the rows r displayed .. button name calculate.. and after the user enters all values in column no.3 and presses calculate button, the moving average for the given range(textbox2) must be calculated in column no.4 .. [ column 1 &2 are just for misc inputs so not important]. for ex..
textbox 1 10
textbox 2 2

10 15
40 15
10 25
20 25
10 15
20 15
10 15
20 15

hope u understand my prob.. pls help me with some ideas
Oct 6 '08 #5
can you please help me correcting errors.. i was trying it for days..

Partial Public Class _Default
Inherits System.Web.UI.Page

Dim textdynamic1 As New ArrayList()
Dim textdynamic2 As New ArrayList()
Dim textdynamic3 As New ArrayList()
Dim textdynamic4 As New ArrayList()

Dim TxtDyn1 As TextBox()
Dim TxtDyn2 As TextBox()
Dim TxtDyn3 As TextBox()
Dim TxtDyn4 As TextBox()

Dim numofper As Integer
Dim nval As Integer

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


'to check the textbox for empty

If Not TextBox1.Text Is String.Empty Then
numofper = TextBox1.Text

ReDim TxtDyn1(numofper)
ReDim TxtDyn2(numofper)
ReDim TxtDyn3(numofper)
ReDim TxtDyn4(numofper)

Exit Sub
End If

'To create the dynamic text box and add the controls to panel, save them in session state

For i As Integer = 0 To numofper - 1
TxtDyn1(i) = New TextBox
Session("key1") = textdynamic1
For j As Integer = 0 To numofper - 1
TxtDyn2(j) = New TextBox
Session("key2") = textdynamic2
For k As Integer = 0 To numofper - 1
TxtDyn3(k) = New TextBox
Session("key3") = textdynamic3
For l As Integer = 0 To numofper - 1
TxtDyn4(l) = New TextBox
Session("key4") = textdynamic4
TxtDyn4(l).Enabled = False


Catch ex As Exception
MsgBox("Enter a valid Number")
End Try

End Sub

Public Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
nval = TextBox2.Text

Dim sum As Integer
Dim k As Integer
Dim l As Integer

'retrieving values from session state and calculating moving average

If Session("key3") IsNot Nothing Then
If Session("key4") IsNot Nothing Then
Dim newArr1 As ArrayList = DirectCast(Session("key3"), ArrayList)
Dim newArr2 As ArrayList = DirectCast(Session("key4"), ArrayList)

For t As Integer = (nval + 1) To numofper
sum += TxtDyn3(k).Text
k += 1

Loop While k <= nval

TxtDyn4(l).Text = sum / nval
l += 1

Loop While l <= numofper
End If
End If

End Sub

End Class
Oct 7 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

56 posts views Thread by Dave Vandervies | last post: by
23 posts views Thread by Bruno R. Dias | last post: by
1 post views Thread by Rakesh Roberts | last post: by
27 posts views Thread by Frederick Gotham | last post: by
5 posts views Thread by Will Honea | last post: by
11 posts views Thread by onkar.n.mahajan@gmail.com | last post: by
4 posts views Thread by Andrew | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.