By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,018 Members | 1,204 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,018 IT Pros & Developers. It's quick & easy.

Vb.net Interesting Problem- Moving Average

P: 31
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.. Itís 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
  3.  
  4. Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.  
  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
  11.  
  12.         Try
  13.  
  14.             'to check the textbox for empty
  15.  
  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)
  22.  
  23.             Else
  24.                 Exit Sub
  25.             End If
  26.  
  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
  44.  
  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)
  49.  
  50.         Catch ex As Exception
  51.             MsgBox("Enter a valid Number")
  52.         End Try
  53.  
  54.     End Sub
  55.  
  56.  
  57. End Class
Oct 4 '08 #1
Share this Question
Share on Google+
5 Replies


Plater
Expert 5K+
P: 7,872
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

P: 31
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

Plater
Expert 5K+
P: 7,872
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

P: 31
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
20
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

P: 31
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


Try

'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)

Else
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
Panel1.Controls.Add(TxtDyn1(i))
textdynamic1.Add(textdynamic1)
Session("key1") = textdynamic1
Next
For j As Integer = 0 To numofper - 1
TxtDyn2(j) = New TextBox
Panel2.Controls.Add(TxtDyn2(j))
textdynamic2.Add(textdynamic2)
Session("key2") = textdynamic2
Next
For k As Integer = 0 To numofper - 1
TxtDyn3(k) = New TextBox
Panel3.Controls.Add(TxtDyn3(k))
textdynamic3.Add(textdynamic3)
Session("key3") = textdynamic3
Next
For l As Integer = 0 To numofper - 1
TxtDyn4(l) = New TextBox
Panel4.Controls.Add(TxtDyn4(l))
textdynamic4.Add(textdynamic4)
Session("key4") = textdynamic4
TxtDyn4(l).Enabled = False

Next

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
Do
Do
sum += TxtDyn3(k).Text
k += 1

Loop While k <= nval

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

Loop While l <= numofper
Next
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.