Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I have button_click event which generate files. I want to close it after I click this button event. If I use close() at end of button click conditions, some of my functions isnt doing all their job. Maybe there is other way to close button event?


C#
private void button2_Click(object sender, EventArgs e)
           {
               {
                   var header = File.ReadAllText(@"C:\dir\header.tex");
                   var footer = File.ReadAllText(@"C:\dir\footer.tex");
                   var sb = new StringBuilder();

                   sb.AppendLine(header);

                   foreach (TreeNode node in treeView1.Nodes)
                   {

                       var tag = node.Tag as string;
                       sb.AppendLine(string.IsNullOrEmpty(Text) ? node.Text : tag);

                   }
                        sb.AppendLine(footer);
   File.WriteAllText(@"C: \dir\final.tex", sb.ToString());

                     //Close() I were using close here.
               }
Posted
Comments
ZurdoDev 2-Dec-15 9:27am    
Close button or close a form? What are you wanting to close?
Member 12175492 2-Dec-15 9:29am    
close form. It is closing, but Last part appenline loses tag value. Other parts are working well. without close() all parts working well
BillWoodruff 2-Dec-15 10:27am    
var tag = node.Tag as string;
sb.AppendLine(string.IsNullOrEmpty(Text) ? node.Text : tag);

that doesn't look right: do you mean:

var tag = node.Tag as string;
sb.AppendLine(string.IsNullOrEmpty(tag) ? node.Text : tag);
Member 12175492 2-Dec-15 10:32am    
It is how I wrote it. I don't why you don't like my code :) main problem for how to close form after this button click.
phil.o 2-Dec-15 17:04pm    
It's how you wrote it but it does not do what you want... So you better listen to advises that are given to you, especially when they come from someone that has proven to be a valuable asset to this site for many years. The question is not "liking your code or not", the question is "Is this code sematically correct ?". And, given the informations you provided, it is not. Especially, could you answer these few questions:
- What is the Text variable you are using?
- Where is it defined?
- Did you add it to your Form class, or do you mean to access the Text property of a given control?

1 solution

Your code is not making any sense. This line:
sb.AppendLine(string.IsNullOrEmpty(Text) ? node.Text : tag);

says if Text (whatever that is, it's not defined in the code snippet you posted) is null or String.Empty, append the node.Text content to the string builder. If Text is not null or empty, append the node.Tag contents.

What's Text, because that's what is driving what gets appended and is probably not what you expect.

Learn to the use the debugger and step through code and inspect variable contents. Without that skill, you're just guessing at what's wrong with your code.
 
Share this answer
 
Comments
BillWoodruff 2-Dec-15 11:15am    
Did you notice that I brought this line to the OP's attention in a comment twenty minutes before you posted this ? Did you see the OP's reply a few minutes later ?
Dave Kreskowiak 2-Dec-15 13:24pm    
No, I didn't. I had the window open and the message half typed when I was interrupted with actual "work stuff". I came back 20 minutes later, finished the post and clicked Submit.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900