Valhalla Legends Forums Archive | .NET Platform | Converting horiz scroll bar value to decimal

AuthorMessageTime
RiffRiot
I have been trying to figure out if it is possible to convert the value, minimum, and maximum properties of a Horizontal ScrollBall Control in C# to Float, Double, or Decimal.  I am making a simple feature in my application that manually inputs a "weight" into a text box via a Horizontal ScrollBar.. however, I can't seem to convert the value type of the scroll bar to decimal for floating point precision.

I have tried using Convert.ToDouble() and double.parse() and typecasting.  I am fairly new to the .NET world but I have fair experience in C++, PHP, and VB.

Basically.. I want the value of the scroll bar to be 0.000(min) to 100.000(max) and the value is displayed on a text box.  Therefor, the value type of the scroll bar needs to be a decimal not int to show precision.
December 17, 2007, 9:31 PM
DDA-TriCk-E
You could try setting it to 1000 - 100000 then dividing by 1000. For example: 1002 / 1000 == 1.002
December 18, 2007, 2:26 AM
Myndfyr
This is a good example of a situation where the adapter design pattern would be appropriate.

Consider the first object diagram in the following image:
[img]http://www.jinxbot.net/pub/adapterpattern.png[/img]

The scroll bar and your class interacting with the scroll bar interact directly.  Unfortunately, the scroll bar doesn't provide the observing class with the information that it needs.  But, with a minimal overhead, you can create a class that lives between the scroll bar and the observer class - this is the adapter class.  It encapsulates the work of converting the literal units of the scroll bar to the target units to be used by the observing class.

One other thing - if you wanted 1002 / 1000 to come out as 1.002, you'd really need to divide 1002 / 1000.0.  Otherwise, like most other languages, the compiler will infer integral division, which will result in an answer of 1.  Even if you declare the result as a double, like so:
[code]
int x = 1002, y = 1000;
double d = x / y;
[/code]
What you get from the compiler is effectively:
[code]
int x = 1002, y = 1000, temp;
temp = x / y;
double d = (double)temp;
[/code]

@RiffRiot: Both Convert.ToDouble and typecasting should have worked (double.Parse as well, if you were calling ToString() on the original number); however, since you didn't provide details on what didn't work I can't suggest a definite course of action to resolve the problem.  Are you running into a compile-time or run-time error?  If it's a compile-time error, what's the compiler saying?  If it's a run-time error, what's the debugger saying?
December 18, 2007, 5:04 PM
DDA-TriCk-E
[quote author=MyndFyre[vL] link=topic=17224.msg175422#msg175422 date=1197997453]
One other thing - if you wanted 1002 / 1000 to come out as 1.002, you'd really need to divide 1002 / 1000.0.  Otherwise, like most other languages, the compiler will infer integral division, which will result in an answer of 1. 
[/quote]

Haha you're right, my bad... Been doing some VB6 programming a bit lately, getting me back into bad habits ;\
December 19, 2007, 1:51 AM
RiffRiot
Thanks for you help on this, it has been appreciated.  However, I just decided to scrap the hassle with the scrollbar and use a input box instead to manually input weight to test.  This is only for testing purposes so it has not been a big deal.  Thanks.
January 14, 2008, 6:41 PM

Search