|
I don't know if 2008 would make a difference or not, sorry.
Jacob Dixon wrote: a save before
No, you have to do the save after.
This is an existing user that's been saved to the store, I'm assuming.
BDF
People don't mind being mean; but they never want to be ridiculous.
-- Moliere
|
|
|
|
|
I have a program that contains a “start time” and “end time” to equal TempHours
I would like dHours to be the rounded figure.
I am trying to round the number up to next tenth of an hour
(IE 4.11 hours is 4.2 hours but 4.10 hours stays at 4.1 hours)
What do I need to add to the below statement to get it working
decimal dHours = Math.Round(decimal.Parse(TempHours.ToString()),3);
Thanks
|
|
|
|
|
ok,
Two questions for you
1) What does not working mean?
2) What type is the TempHours variable?
Alan.
|
|
|
|
|
Why are you changing it to a string?
If TempHours is already a numeric type (single, double, decimal, etc), just do this:
decimal dHours = Math.Round(TempHours, 1, MidpointRounding.AwayFromZero);
Second parameter is the number of decimals. 1 = To the nearest tenth. Third parameter tells it you always want to round up to the next value, never down.
|
|
|
|
|
Hi,
why would you call ToString() and then Parse() to turn a number into another number? This is nonsense, as it wastes lots of CPU cycles: numbers are stored in binary for a reason, it takes a lot of divide-by-10s to perform a ToString() on a number.
Math.Round does rounding in the regular way, i.e. 1-4 go down, 5-9 go up.
However you want 1-9 to go up, hence add 0.04 to the original number, then use Math.Round
Warning: you did not define how it should work for negative numbers; whatever straight code you come up with, there is a 50% chance it won't do what you want for negative numbers. If so, test for negative, and provide two different formulas.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Thank you all for your replies. Great info! Although I didn't write the orig code, I'm learning quite a bit in trying to get it to work.
|
|
|
|
|
protected void Page_Init(object sender, EventArgs e)
{
RadioButtonList rbl = new RadioButtonList();
rbl.ID = "rbl1";
rbl.Items.Add("a");
rbl.Items.Add("b");
PlaceHolder1.Controls.Add(rbl);
rbl.SelectedIndexChanged += new EventHandler(rbl_SelectedIndexChanged);
Button btn = new Button();
btn.ID = "btn1";
btn.Text = "Button";
PlaceHolder2.Controls.Add(btn);
btn.Click += new EventHandler(btn_Click);
}
protected void Page_Load(object sender, EventArgs e)
{
}
private void btn_Click(object s, EventArgs e)
{
Label2.Text = "buttons click event fired";
}
private void rbl_SelectedIndexChanged(object s, EventArgs e)
{
Label1.Text="radiobuttonlist control selectedindexchanged event fired";
//RadioButtonList rbl2=(RadioButtonList)s;
//Label1.Text = rbl2.SelectedValue;
}
Jyoti Goel
|
|
|
|
|
there is no working SelectedIndexChanged in RB, use CheckedChanged instead
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
|
Have some patience, it is considered bad form to repost.[^]
hmmm pie
|
|
|
|
|
Another deleted post???
...No, just someone else posting the contents of thier brain
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Had posted the post in ASP.NET, but didn't know where my post will receive more traffic, since there were C# issues involved. So I double posted! Anyway I've resolved the issue. The original post is in the ASP.NET corner.
Thanks
|
|
|
|
|
The point is that you post it where it's most applicable, NOT where you think it will get the most attention.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Hi guys
I’m at my wits end with TreeNodes.
What i'm trying to do, to me is very simple. But for some reason I just can't get my head around it!
I’m currently working on a project where basically I’m splitting a Url address into a string array and creating each node and child node from the url segments (just like the Windows Explorer.)
\\ this is the plan
1) step through each url
string url1 = @"www.somedomain.com\home\about_us.aspx";
string url2 = @"www.somedomain.com\home\contact_us.aspx";
string url3 = @"www.somedomain.com\home\main.aspx";
2) split the current url into sections
3) process the url sections and create new nodes and child nodes or merge nodes if needed
4) output results
the final output I'm trying to get...
+ www.somedomain.com
_+ home
___+ about_us.aspx
___+ contact_us.aspx
___+ main.aspx
but in short i'm getting something like...
+ www.somedomain.com
_+ home
___+ about_us.aspx
___+ contact_us.aspx
___+ main.aspx
_+ home
___+ about_us.aspx
___+ contact_us.aspx
___+ main.aspx
Looking at the code below, Is there another way around this problem or am I just being stupid???
Any help would be very much appreciated. Please feel free to rip the hell out of my coding as I've kind of lost the plot!
Thanks again!
<pre>private void Form1_Load(object sender, EventArgs e)
{
TreeNode treeNode = new TreeNode();
treeView1.Nodes.Add(treeNode);
treeView1.PathSeparator = "\\"
// Add first url address
BuildNodes(treeNode,@"1\2\3\4\5\6\7");
// Add the next url address merging duplicate folders together...
BuildNodes(treeNode, @"1\2\4\5\6");
}</pre>
<pre>private void BuildNodes(TreeNode treeNode, string address)
{
string[] add = SplitNodes(address);
int max = add.Length;
string key = String.Empty;
TreeNode[] treeNodes = new TreeNode[max];
key = add[0];
treeNodes[0] = treeNode;
treeNodes[0].Text = add[0];
treeNodes[0].Name = key;
for (int i = 1; i < max; i++)
{
key = GetKey(key, add[i]);
treeNodes[i] = new TreeNode();
treeNodes[i].Text = add[i];
treeNodes[i].Name = key;
TreeNode[] tns = treeNodes[i - 1].Nodes.Find(treeNodes[i].Name, true);
// if node exists skip it else add the new node
if (tns.Length == 0)//if
{
treeNodes[i - 1].Nodes.Add(treeNodes[i]);
}
}
}</pre>
<pre>private string GetKey(string key, string value)
{
string delimiter = treeView1.PathSeparator;
if (key == String.Empty)
return value;
else
return String.Format(@"{0}{1}{2}", key, delimiter, value);
}</pre>
<pre>private string[] SplitNodes(string nodeString)
{
char[] delimiter = treeView1.PathSeparator.ToCharArray();
return nodeString.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
}
</pre>
<div class="ForumMod">modified on Friday, March 27, 2009 12:01 PM</div>
|
|
|
|
|
heres an idea... should do the job anyway...
void CreateNodes(string[] urls)
{
foreach(string url in urls)
{
string[] splits = url.Split('\\');
TreeNode temp = GetNode(splits[0], TreeView.Nodes);
if(temp == null)
temp = TreeView.Nodes.Add(splits[0]);
for(int i = 1; i < splits.Length; i++)
{
temp = GetNode(splits[i], temp.Nodes);
if(temp == null)
temp = temp.Nodes.Add(splits[i]);
}
}
}
TreeNode GetNode(string text, TreeNodeCollections nodes)
{
foreach(TreeNode n in nodes)
if(n.Text == text)
return n;
return null;
}
Life goes very fast. Tomorrow, today is already yesterday.
modified on Friday, March 27, 2009 12:47 PM
|
|
|
|
|
Thank you so much musefan, I'll give it a go!
|
|
|
|
|
Looked very promising, but the code you sent was creating each url folder as a root node.
It wasn’t breaking them down into child or grandchild nodes.
ps i'm using c#.net framework 3.5
here's the code you sent... I had to modify it slightly.
private void Form1_Load(object sender, EventArgs e)
{
TreeNode tn = new TreeNode();
treeView1.Nodes.Add(tn);
string[] urls = new string[2];
urls[0] = "a\\b\\c\\d";
urls[1] = "a\\b\\e\\f";
CreateNodes(urls);
}
void CreateNodes(string[] urls)
{
foreach (string url in urls)
{
string[] splits = url.Split('\\');
TreeNode temp = GetNode(splits[0], treeView1.Nodes);
if (temp == null)
temp = treeView1.Nodes.Add(splits[0]);
for (int i = 1; i < splits.Length; i++)
{
TreeNode temp2 = GetNode(splits[i], temp.Nodes);
if (temp2 == null)
temp2 = treeView1.Nodes.Add(splits[i]);
}
}
}
TreeNode GetNode(string text, TreeNodeCollection nodes)
{
TreeNode result = null;
foreach (TreeNode n in nodes)
if (n.Text == text)
{
result = n;
break;
}
return result;
}
|
|
|
|
|
where you have put temp2 is wrong it was not meant to be another instance. see my other post for edited code
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Sorry mate, just noticed that!
I've corrected temp2 as your right, it shouldn't be a new instance. I've re-tested the code but for some reason it's still populating the root nodes only???
I think i'll give it a rest for a bit and take another look later (with a fresh head); cos the code
i'm looking at, to me should work.
void CreateNodes(string[] urls)
{
foreach (string url in urls)
{
string[] splits = url.Split('\\');
TreeNode temp = GetNode(splits[0], treeView1.Nodes);
if (temp == null)
temp = treeView1.Nodes.Add(splits[0]);
for (int i = 1; i < splits.Length; i++)
{
temp = GetNode(splits[i], temp.Nodes);
if (temp == null)
temp = treeView1.Nodes.Add(splits[i]);
}
}
|
|
|
|
|
check out new edit lol
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
lol
My heads gone! I can't believe I've been beaten by the humble TreeView!
And yet my web crawler is a hell of a lot more complex than that...
I guess i'll take the walk of shame!! lol
i'll look at it again later
Cheers again
|
|
|
|
|
I edited your code slightly.
There was on error on the line:
temp = temp.Nodes.Add(splits[i]);
As temp was null so temp.Nodes was throwing an error.
private void createNodes(string[] urls)
{
foreach (string url in urls)
{
string[] splits = url.Split('\\');
if (splits.Length > 0)
{
TreeNode temp;
if ((temp = GetNode(splits[0], this.treeView1.Nodes) == null)
temp = this.treeView1.Nodes.Add(splits[0]);
for (int i = 1; i < splits.Length; i++)
{
TreeNode temp2;
if ((temp2 = GetNode(splits[i], temp.Nodes) == null)
temp = temp.Nodes.Add(splits[i]);
else
temp = temp2;
}
}
}
}
hmmm pie
modified on Tuesday, April 21, 2009 8:18 AM
|
|
|
|
|
Given the nature of trees in general, you might want to simplify things and go for a recursive approach. Something like this (pseudo-code):
InsertURL(TreeNode root, string url)
{
InsertURL(root, SplitNodes(url), 0)
}
InsertURL(TreeNode parent, string[] urlParts, int index)
{
TreeNode nextChild = parent.Find(urlParts[index])
If nextChild is not valid
{
Create a new node under parent corresponding to urlParts[index]
nextChild = The new node
}
if ++index < urlParts.Length
{
InsertURL(nextChild, urlParts, index)
}
}
(Yes, I know the syntax isn't proper C#. That's intentional, so ya don't try to copy-paste and compile it )
Notes:
1) The array gets passed as a reference, so it's faster to just send the whole array to each recursive call instead of stripping out each part as we place it.
2) Note the ++ in the last comparison. Increment it, THEN see if the new value is in-bounds, then recurse if valid.
|
|
|
|
|
Don’t worry about the syntax Ian. Your pseudo-code’s pretty clear and straight forward.
I’m just coding the methods out now. Fingers cross this might be the one!
I don’t know if anyone else has had this problem? I think I’d better post it up once we’ve got it working.
Thanks so much for your help, really appreciate it
|
|
|
|
|
Big, big thank you musefan and Ian! I’ve now got it working brilliantly.
Based on the pseudo-code Ian posted (which worked great after tweeking!) I’ve decided to post the working code incase anyone else out there is experiencing TreeNode’s from hell lol.
"ps, i'm not great with codeproject, so if you know of a better place for me to post this answer, please let me know."
Well here’s the working code…
Add a TreeView control to Form1 and name it treeView1. and then add this code..
<code>
private void Form1_Load(object sender, EventArgs e)
{
TreeNode tn = new TreeNode("Root");
// set PathSeparator to url paths
treeView1.PathSeparator = "/";
// Create any old folders and process
InsertURL(tn, @"mywebsite.com/home/about_us.aspx");
InsertURL(tn, @"mywebsite.com/home/contact_us.aspx");
InsertURL(tn, @"mywebsite.com/home/main.aspx");
InsertURL(tn, @"mywebsite.com/blogs/seo.aspx");
InsertURL(tn, @"mywebsite.com/blogs/linkchecks.aspx");
InsertURL(tn, @"mywebsite.com/blogs/howto/gettheresults.aspx");
//add nodes to treeView
treeView1.Nodes.Add(tn);
}
private void InsertURL(TreeNode root, string url)
{
InsertURL(root, SplitNodes(url), 0);
}
private string[] SplitNodes(string nodeString)
{
char[] delimiter = treeView1.PathSeparator.ToCharArray();
return nodeString.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
}
private void InsertURL(TreeNode parent, string[] urlParts, int index)
{
// Create the node if it doesn't exist.
CreateNode(parent, urlParts[index]);
// get the next urlPart
index++;
if (index < urlParts.Length)
{
InsertURL(parent.Nodes[urlParts[index-1]], urlParts, index);
}
}
private static void CreateNode(TreeNode treeNode, string name)
{
// Add the node if it don't exist
if (treeNode.Nodes[name] == null)
{
treeNode.Nodes.Add(name, name);
}
}</code>
<div class="ForumMod">modified on Saturday, March 28, 2009 5:57 AM</div>
|
|
|
|
|