[SOLVED] - [Problem] Converting TreeNode Tag to Decimal

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1341 Views - Last Post: 14 April 2011 - 06:00 AM Rate Topic: -----

#1 Gamma Ray  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 24-September 10

[SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 11:50 AM

Well, yes, how do I do that?
I did search, and found answers like "Decimal.Parse(StringVariable" ...

This may be a bit different though...
I have a value in a TreeNode's tag, and I want to have a decimal variable with that number.

How do I do that?

I've tried different ways to convert, but always get error.

Thanks in advance, :)

This post has been edited by Gamma Ray: 14 April 2011 - 06:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1212
  • View blog
  • Posts: 4,130
  • Joined: 27-January 10

Re: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 11:54 AM

What code are you using?

The .Tag object is an Object and as such, you need to cast it to be able to use it properly.

Convert.ToDecimal(object);


For example:

Object myTag = new object();
myTag = 10.31;

var x = Convert.ToDecimal(myTag);
Console.WriteLine(x);


This post has been edited by Sergio Tapia: 13 April 2011 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#3 Gamma Ray  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 24-September 10

Re: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 12:00 PM

I tried this:

		private void List_AfterSelect(object sender, TreeViewEventArgs e)
		{
			TreeNode theTag = List.SelectedNode.Tag;
                        Convert.ToDecimal(theTag);
		}


But the "List.SelectedNode.Tag" is underlined in red. :(

Edit:
I'll read through your update 1sec

This post has been edited by Gamma Ray: 13 April 2011 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1212
  • View blog
  • Posts: 4,130
  • Joined: 27-January 10

Re: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 12:04 PM

Change this:

private void List_AfterSelect(object sender, TreeViewEventArgs e)
{
	TreeNode theTag = List.SelectedNode.Tag;
                      Convert.ToDecimal(theTag);
}



To this:

private void List_AfterSelect(object sender, TreeViewEventArgs e)
{
    var theDecimal = List.SelectedNode.Tag;
    var convertedValue = Convert.ToDecimal(theDecimal);
}



The problem you were having is because you were trying to save .Tag (a decimal, right?) to a TreeNode object.

In the fixed code, you're getting the .Tag, then converting it.
Was This Post Helpful? 0
  • +
  • -

#5 Gamma Ray  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 24-September 10

Re: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 12:05 PM

Thanks, I'll try that :)

Edit:
Ok, I got this error when I clicked on the node with the tag:

Quote

Input string was not in a correct format.


I tried to fiddle around a bit, but didn't help :(

Edit 2:
		private void List_AfterSelect(object sender, TreeViewEventArgs e)
		{
			tInfo.Text = "" + List.SelectedNode.Tag + " x " + List.SelectedNode.ToolTipText;

			var theDecimal = List.SelectedNode.Tag;
			var convertedValue = Convert.ToDecimal(theDecimal);
			
			
		}

		private void bCal_Click(object sender, EventArgs e)
		{

	
			labCalc.Text = "" + Calculate(numAmount.Value, convertedValue);
		}

		decimal Calculate(decimal Am, decimal Pr)
		{
			

			return Am * Pr;
		}

		public decimal convertedValue { get; set; }


Here is the code I'm using at the moment.

(Sorry if I seem a bit incompetent :( )

This post has been edited by Gamma Ray: 13 April 2011 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1212
  • View blog
  • Posts: 4,130
  • Joined: 27-January 10

Re: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 12:23 PM

In the context of the posted code, what line is firing that error?
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3831
  • View blog
  • Posts: 6,476
  • Joined: 08-June 10

Re: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 12:24 PM

Put a breakpoint in to see what the actual value trying to be converted to a decimal is. You're probably trying to convert something that won't convert.
Was This Post Helpful? 0
  • +
  • -

#8 Gamma Ray  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 24-September 10

Re: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 12:31 PM

First of all, the error is here:
var convertedValue = Convert.ToDecimal(theDecimal);


Breakpoint, idk what that is, sorry... lol...
By the way; the tag is "0.0078125m" and I also tried "0.0078125"

So I don't know :/
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3831
  • View blog
  • Posts: 6,476
  • Joined: 08-June 10

Re: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 12:47 PM

Quote

Breakpoint, idk what that is, sorry... lol...


Are you not at a computer with access to the internet? When you don't know what something is, usually a quick google will solve that right up.

Anyway, here's a tutorial on debugging. Learn it, love it, live it.

http://www.dreaminco...6780-debugging/

With my testing, the second string will work, while the first will not. Make sure none of your tags actually have "m" in them.
Was This Post Helpful? 1
  • +
  • -

#10 Gamma Ray  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 24-September 10

Re: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 12:58 PM

Debugging :bananaman: xD

Thank you :)

I'll try to see what I can do.
Was This Post Helpful? 0
  • +
  • -

#11 here.to.code  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 55
  • Joined: 15-February 11

Re: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 01:47 PM

Try this and see if its the result you're looking for... I'm not exactly positive what you're asking but given a sample data (node.tag) of 10.5m your result will give 10.5

Take note of what I did and you should be able to manipulate it for your own use :)
Enjoy!

  private void List_AfterSelect(object sender, TreeViewEventArgs e)
        {
            

            if (List.SelectedNode.Tag != null)
            {
                if (List.SelectedNode.Tag.ToString() != String.Empty)
                {
                    string temp = "";
                    char[] nums = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.' };
                    foreach (char c in List.SelectedNode.Tag.ToString())
                    {
                        foreach (char n in nums)
                        {
                            if (c == n)
                                temp += c;
                        }
                        
                    }
                    tInfo.Text = "" + temp + " x " + List.SelectedNode.ToolTipText;
                    var convertedValue = Convert.ToDecimal(temp);
                }
            }
        }


This post has been edited by here.to.code: 13 April 2011 - 01:52 PM

Was This Post Helpful? 1
  • +
  • -

#12 Gamma Ray  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 24-September 10

Re: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 01:50 PM

I'll +rep you anyway :)

Thanks for your reply, I'll try it now.

Edit:
I get same error as above... :(

Quote

Input string was not in a correct format.

var convertedValue = Convert.ToDecimal(temp);


P.S.:
I don't know if this is right, but I use
public decimal convertedValue { get; set; }

to not get the variable underlined. As I use it in another method.


Thank you, and again, sorry if I seem stupid, lol :(

This post has been edited by Gamma Ray: 13 April 2011 - 02:00 PM

Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3831
  • View blog
  • Posts: 6,476
  • Joined: 08-June 10

Re: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 02:21 PM

Again, you need to know exactly what is in temp at the time it's being evaluated to see what it is trying to convert.

Put in this line:

MessageBox.Show(temp);


Immediately before the one throwing the error, and tell us exactly what pops up.

If it throws an error on the message box line, replace temp with temp.ToString()
Was This Post Helpful? 0
  • +
  • -

#14 Gamma Ray  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 24-September 10

Re: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 02:23 PM

It says the value I have in tag...
Then when I press Ok the error appears.
} MessageBox.Show(temp);

				tInfo.Text = "" + temp + " x " + List.SelectedNode.ToolTipText;
				var convertedValue = Convert.ToDecimal(temp);
			}


Placed it there.
Was This Post Helpful? 0
  • +
  • -

#15 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3831
  • View blog
  • Posts: 6,476
  • Joined: 08-June 10

Re: [SOLVED] - [Problem] Converting TreeNode Tag to Decimal

Posted 13 April 2011 - 02:31 PM

Well, of course it does. I need to see that value to tell you why it's not working!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2