How to close button_click event after clicking

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 939 Views - Last Post: 03 December 2015 - 06:34 AM Rate Topic: -----

#1 Mantas   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-November 15

How to close button_click event after clicking

Posted 02 December 2015 - 07:20 AM

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 isn't doing all their job. Maybe there is other way to close button event?

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.
                }


Is This A Good Question/Topic? 0
  • +

Replies To: How to close button_click event after clicking

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,989
  • Joined: 05-May 12

Re: How to close button_click event after clicking

Posted 02 December 2015 - 07:23 AM

Close() closes the form. What were you expecting to happen? Close, but don't really close the form?
Was This Post Helpful? 0
  • +
  • -

#3 Mantas   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-November 15

Re: How to close button_click event after clicking

Posted 02 December 2015 - 07:27 AM

It close the form. The thing that I have a lot of appendline parts in that button click, and last appenline is losing his tag value. But others appenlines works well uses both text and tag values. Without this close(), everything works perfectly(I need to click button and later quit icon on top of right). But I want to click that button and close form.

This post has been edited by Skydiver: 02 December 2015 - 07:34 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,989
  • Joined: 05-May 12

Re: How to close button_click event after clicking

Posted 02 December 2015 - 07:35 AM

There is no need to quote the post above yours. Use the big Reply button or the Fast Reply area.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,989
  • Joined: 05-May 12

Re: How to close button_click event after clicking

Posted 02 December 2015 - 07:53 AM

By any chance, did you follow bad advice somewhere on the Internet and started using Application.DoEvents() somewhere in your code?

As it currently stands with the code that you've presented in this thread and other threads, I find it really hard to believe that calling Close() on line 20 would impact appending a the tag value on line 14 because by the time line 20 is hit, the value has already been read out of the treenode and copied literally into the internal buffer of the StringBuilder. The footer then gets appended after that tag, and then all of that written out to the file prior to calling Close(). There is no way that Close() would magically go crack open your file and take out that tag that you are missing.

The reason why I asked about Application.DoEvents() is because if you are using it, then you are setting yourself up for reentrancy issues where your button click handler could be called two or more times and potentially seemingly concurrently. So if the following sequence happens:
    button2 click handler called
    close called
    treeview nodes start getting torn down (but not completely)
    button2 click handler called again Oops!
    form closes completely


Then yes, you could end up with a state where there are missing tag values.

The only true way to know what is happening is to set a breakpoint on your function. Also stop stacking calls to make your debugging task easier. The theoretical saved number of lines actually doesn't make your code run any faster. Here's a re-write of your function where you can actually watch the variables with the debugger:
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;
        var line = string.IsNullOrEmpty(Text) ? node.Text : tag;
        sb.AppendLine(line);
    }

    sb.AppendLine(footer);

    var finalVersion = sb.ToString();
    File.WriteAllText(@"C:\dir\final.tex", finalVersion);

    Close();
}



(As an aside you had an extra curly brace on line 3, and you still have that extra space in your filepath.)

This post has been edited by Skydiver: 02 December 2015 - 08:14 AM
Reason for edit:: Broke up stacked commands in foreach loop.

Was This Post Helpful? 0
  • +
  • -

#6 Mantas   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-November 15

Re: How to close button_click event after clicking

Posted 02 December 2015 - 08:00 AM

I am not using application.do . I debugged hundred times and it says it should be correct. But still when I run it, last file which is written in last append line part is filling file with .text , not with .tag.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,989
  • Joined: 05-May 12

Re: How to close button_click event after clicking

Posted 02 December 2015 - 08:17 AM

So it works correctly when stepping through the code with the debugger and the Close() call there, but it fails when you just run the code without breakpoints?
Was This Post Helpful? 0
  • +
  • -

#8 Mantas   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-November 15

Re: How to close button_click event after clicking

Posted 02 December 2015 - 08:29 AM

After debugging it look like it, jumps through my tag foreach on the last part of appendline. So in debugg and run, program fails.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,989
  • Joined: 05-May 12

Re: How to close button_click event after clicking

Posted 02 December 2015 - 08:33 AM

So how can you say in post #6 that you are getting correct values while debugging but in post #8 say that it is not giving you the correct values?
Was This Post Helpful? 0
  • +
  • -

#10 Mantas   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-November 15

Re: How to close button_click event after clicking

Posted 02 December 2015 - 08:37 AM

It gives correct values, but foreach is working in all other appendline parts, except the last one.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,989
  • Joined: 05-May 12

Re: How to close button_click event after clicking

Posted 02 December 2015 - 08:47 AM

So that then means that the issue is with your tree node values, and not because you are calling Close().

That also means that you don't have the correct values if you are expecting the last tag to be present but you say that you are not getting it. If you had the correct values, then your last tag would not be skipped.
Was This Post Helpful? 0
  • +
  • -

#12 Mantas   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-November 15

Re: How to close button_click event after clicking

Posted 02 December 2015 - 08:50 AM

So why everything is working, when I am not putting close() ? If my values are wrong?
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,989
  • Joined: 05-May 12

Re: How to close button_click event after clicking

Posted 02 December 2015 - 08:56 AM

Are you sure that it is only the single line for the Close() call that you are changing?

Any which way the real way to tell is to set a breakpoint and step through with the debugger. With the call to Close() commented out, what are the values that you pass to AppendLine()? With the call to Close() present, what are the value that you pass to AppendLine()? They should be exactly the same unless you are touching other lines than just that one line to comment out or uncomment the call to Close().
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,989
  • Joined: 05-May 12

Re: How to close button_click event after clicking

Posted 02 December 2015 - 09:02 AM

As a brute force way of debugging is to set a breakpoint right after the call to WriteAllText(). Inspect the physical file contents. Does it have the expected results? Then let the debugger continue to run. Inspect the file contents again after your program closes. Does it still have the expected results? If not, then you probably have written a form closing or form close event handler that goes and overwrites the file. But I highly doubt that is what is happening assuming that I can trust what you are saying that your foreach loop is not looping the correct number of times depending on whether you have the Close() call present or not.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,989
  • Joined: 05-May 12

Re: How to close button_click event after clicking

Posted 02 December 2015 - 12:07 PM

Just an FYI for other readers- the OP cross posted to StackOverflow. There is important context for those offering help or using this thread as a reference.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2