Class Problem! 
Author Message
 Class Problem!

Hi all,

I have some data controls in different user controls which participate in
calculating a certain value. I want the calculated value to change whenever
any value is changed in any of the controls residing in different user
controls(classes).
Now tell me how can it be achieved? One way is to expose the data with some
variables in every class and use it in a function defined is a separate
class(file) from where that function can be called in the text_changed event
handler of those data controls to reflect the latest change.But I guess
there can be better solutions..Do u have one???

Thanks!
Punit Singh



Fri, 28 Jan 2005 22:54:52 GMT  
 Class Problem!
What's wrong with handling events? I think the entire language paradigm
points to doing just that.

Here's how I would do it. Add textboxes TextBox1, TextBox2, TextBox3 and
txtOutput to a form and add the following code:

Private Sub Form1_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler TextBox1.TextChanged, AddressOf CalculateSum
AddHandler TextBox2.TextChanged, AddressOf CalculateSum
AddHandler TextBox3.TextChanged, AddressOf CalculateSum
End Sub
Private Sub CalculateSum(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim sum As Single
If IsNumeric(TextBox1.Text) Then sum += CSng(TextBox1.Text)
If IsNumeric(TextBox2.Text) Then sum += CSng(TextBox2.Text)
If IsNumeric(TextBox3.Text) Then sum += CSng(TextBox3.Text)
txtOutput.Text = sum
End Sub

If you're working with user controls, just make and use events and methods
equivalant to .TextChanged and .Text as seen here. Or, expose the control
contained within your user control, and use those events/methods directly.

Bob


Quote:
> Hi all,

> I have some data controls in different user controls which participate in
> calculating a certain value. I want the calculated value to change
whenever
> any value is changed in any of the controls residing in different user
> controls(classes).
> Now tell me how can it be achieved? One way is to expose the data with
some
> variables in every class and use it in a function defined is a separate
> class(file) from where that function can be called in the text_changed
event
> handler of those data controls to reflect the latest change.But I guess
> there can be better solutions..Do u have one???

> Thanks!
> Punit Singh



Sat, 29 Jan 2005 00:06:36 GMT  
 Class Problem!
Bob...thanks. This works for me, too, in a similar situation. My technical
app takes some time to recalculate, so I don't want to do it every time the
text in the box changes, so I'm using LostFocus event instead (user doesn't
have to push a "Recalculate" button). The calcs are fast enough to run using
LostFocus

The concern I've got is that the user may not leave the last text box after
entering data, so the results may not be valid (ie, may be using all correct
values except the very last one entered in a box).

This is somewhat like Excel forcing you to tab out of a cell to invoke
automatic recalculation.

Any ideas on best way to handle this?

Steve


Quote:
> What's wrong with handling events? I think the entire language paradigm
> points to doing just that.

> Here's how I would do it. Add textboxes TextBox1, TextBox2, TextBox3 and
> txtOutput to a form and add the following code:

> Private Sub Form1_Load(ByVal sender As Object, _
>     ByVal e As System.EventArgs) Handles MyBase.Load
> AddHandler TextBox1.TextChanged, AddressOf CalculateSum
> AddHandler TextBox2.TextChanged, AddressOf CalculateSum
> AddHandler TextBox3.TextChanged, AddressOf CalculateSum
> End Sub
> Private Sub CalculateSum(ByVal sender As Object, ByVal e As
> System.EventArgs)
> Dim sum As Single
> If IsNumeric(TextBox1.Text) Then sum += CSng(TextBox1.Text)
> If IsNumeric(TextBox2.Text) Then sum += CSng(TextBox2.Text)
> If IsNumeric(TextBox3.Text) Then sum += CSng(TextBox3.Text)
> txtOutput.Text = sum
> End Sub

> If you're working with user controls, just make and use events and methods
> equivalant to .TextChanged and .Text as seen here. Or, expose the control
> contained within your user control, and use those events/methods directly.

> Bob



> > Hi all,

> > I have some data controls in different user controls which participate
in
> > calculating a certain value. I want the calculated value to change
> whenever
> > any value is changed in any of the controls residing in different user
> > controls(classes).
> > Now tell me how can it be achieved? One way is to expose the data with
> some
> > variables in every class and use it in a function defined is a separate
> > class(file) from where that function can be called in the text_changed
> event
> > handler of those data controls to reflect the latest change.But I guess
> > there can be better solutions..Do u have one???

> > Thanks!
> > Punit Singh



Sat, 29 Jan 2005 05:41:22 GMT  
 Class Problem!
I would probably do one of the following...

- Grey out the calculated results if the user edits the text again. Next to
the grayed results, the user is told to press a button to recalculate. Maybe
not what you're looking for, but the simplest solution.

- Add a timer to your app that is started on the text change event. If the
timer gets through (n) seconds without restarting, do the calculation.
Calculate on both lostfocus and carriage returns, too, so the user doesn't
have to wait for the timer when moving between fields or at the last entry.
Provide gray-out feedback when the calculated results are out of sync here
as well.

BTW, you may also want to examine the benefits of using unmanaged memory to
do your calculations. I haven't done it myself, but I hear you can get a
significant performance boost.

Bob


Quote:
> Bob...thanks. This works for me, too, in a similar situation. My technical
> app takes some time to recalculate, so I don't want to do it every time
the
> text in the box changes, so I'm using LostFocus event instead (user
doesn't
> have to push a "Recalculate" button). The calcs are fast enough to run
using
> LostFocus

> The concern I've got is that the user may not leave the last text box
after
> entering data, so the results may not be valid (ie, may be using all
correct
> values except the very last one entered in a box).

> This is somewhat like Excel forcing you to tab out of a cell to invoke
> automatic recalculation.

> Any ideas on best way to handle this?

> Steve



Sat, 29 Jan 2005 22:52:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Instalation Problem + Class problem (automation) (VB+Access)

2. Image Class Problems (generic GDI + error)

3. VB Class problem

4. Using DataSet's in a class - problem

5. UDPClient Class Problem - Need HELP !!!!

6. Collections in classes problem

7. define a class problem

8. Class problems

9. Class problem, or is it me?

10. Class Problem

11. Class problem

12. Class Problem

 

 
Powered by phpBB® Forum Software