• Welcome to Valhalla Legends Archive.
 

Converting horiz scroll bar value to decimal

Started by RiffRiot, December 17, 2007, 03:31 PM

Previous topic - Next topic

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.

Chriso

You could try setting it to 1000 - 100000 then dividing by 1000. For example: 1002 / 1000 == 1.002

MyndFyre

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:


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:

int x = 1002, y = 1000;
double d = x / y;

What you get from the compiler is effectively:

int x = 1002, y = 1000, temp;
temp = x / y;
double d = (double)temp;


@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?
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Chriso

Quote from: MyndFyre[vL] on December 18, 2007, 11:04 AM
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. 

Haha you're right, my bad... Been doing some VB6 programming a bit lately, getting me back into bad habits ;\

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.