Closing a Form Via the Red X

So someone posed this question to me. How do you cancel the close event on the form. This usually occurs when the user clicks the “red x” button on the form.
Here is what you do:
Private Sub dialog_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim dlgRes As Windows.Forms.DialogResult
dlgRes = MessageBox.Show(“Are you sure you wish to exit?”, “Closing”, MessageBoxButtons.YesNo, MessageBoxIcon.Information)
If dlgRes = Windows.Forms.DialogResult.Yes Then
Me.DialogResult = Windows.Forms.DialogResult.Yes
e.Cancel = False
Else
e.Cancel = True
End If
End Sub
Notice this is for the FormClosing event. This event uses a special set of event arguments called the FormClosingEventArgs. There is a property you can set that will cancel the close process named cancel.
The reason I am posting this is that while this seems so very obvious, it is suprisingly difficult to find this information for a new programmer so hopefully this will get tagged more appropriately in Google’s search engine.
Advertisements

Dot Net User Settings Vs Application Settings

Application and user settings in .Net are very simple to take advantage of. But how are they different?
Application settings are stored in the applications config file (XML document). Application settings cannot be changed while the program is running. So if you make a change in the config file while the program is running those changes will not be reflected until you restart the program. As an example, connection strings are a great thing to save here.
User settings are initially stored in the config file. The first time the program gets run it takes the values in the config file and loads them into a memory block for the program. Once you close the program all settings that were changed while the program was running are then saved in this block of memory. You can also call the method My.Settings.Saver() which will save the settings prior to the termination of the program. Great things to save in this are object that the user can manipulate and you want to save that data when they leave the program.
You might not believe me with the user settings but try it out yourself. Create a small project that saves some settings to a user setting. Then open the program in your favorite hex editor. Search for the setting that you saved, you will find it!

Error MSB3482: SignTool reported an error ‘Keyset does not exist’.

So I got this today: error MSB3482: SignTool reported an error ‘Keyset does not exist’. I was trying to publish a project in Visual Studio 2005. Here is the resolution:
Right click the ClickOnce key marked with the PFX extension in your projects folder, install it and try to publish again.
The cause apparently was that I changed the configuration of the compiling. Changing them back to the original state did not work because the damage was done.
Let me know if this helps!

Visual Studio 2005 Tip of the Day – Class Diagram

I just have a really brief entry for today. In my continued efforts of self-improvement, I have stumbled upon a rather interesting feature in VS that I never knew (or bothered to look at). For some developers, they need to visualize what they are doing in one way or another. Well the good people at Microsoft have developed a GUI for creating classes called Class Diagram.

Right mouse click on your solution in the Solution Explorer and select Class Diagram. In there you will discover that you will be able to create the shells of your classes very simply and quickly. You can add methods, background fields, properties to new or existing classes you have created. In each of these you can include parameters and comments.

Once you include some code in the classes, you can even test the results with Test Bench. Basically you can enter values into the parameters of functions and see the output, without even writing the old main function and running through test case after test case. You still might want to do that anyway, so you can repeat your tests.

Well that is it for now!

WinDbg – Dot Net Debugging Without Source Code

Debugging with WinDbg can be challenging but rewarding. One area where Windbg shines is in the area of debugging dot net without the source code.

I am very much a beginner on this but I will tell you some things that I have found invaluable. First one, Tess Ferrandez is a goddess! She is an ASP.Net Escallation Engineer at Microsoft. Her blog, If Broken It Is, Fix It You Should, is absolutely filled full of tips and tricks. She also has a very good video at DNR Tv.

Setting Up Your PC To Use WinDbg

If you are debugging  a Dot Net Program’s crash dump, you are going to need the following file off of that computer: mscordacwks*.dll. You are going to need the version of it that the program is using. The asterick (*) is where the versioning would be in the file name. You are going to want to place it in the same folder as WinDbg with the following syntax for the file name:
mscordacwks_x86_x86_2.0.50727.3603.dll

The x86 is obviously for the x86 system architecture, you can also use x64 or the IA64 for the Itanium line. Following that you have the version of Dot Net 2.0.50727 followed by the exact version number of the dll, in this case 3603.

You can then run the .loadby sos mscorwks

If you do not do this correctly you will see something like this trying to run one of the SOS (Dot Net, managed code commands):

Failed to load data access DLL, 0x80004005
Verify that 1) you have a recent build of the debugger (6.2.14 or newer)
2) the file mscordacwks.dll that matches your version of mscorwks.dll is
in the version directory
3) or, if you are debugging a dump file, verify that the file
mscordacwks_<arch>_<arch>_<version>.dll is on your symbol path.
4) you are debugging on the same architecture as the dump file.
For example, an IA64 dump file must be debugged on an IA64
machine.

You can also run the debugger command .cordll to control the debugger’s
load of mscordacwks.dll.  .cordll -ve -u -l will do a verbose reload.
If that succeeds, the SOS command should work on retry.

If you are debugging a minidump, you need to make sure that your executable
path is pointing to mscorwks.dll as well.

In this case change the name of the dll to the correct name and then run the .cordll -ve -u -l command.

David Douglass’ blog has a great cheat sheet for common commands in WinDbg: .Net On My Mind

I just want to take a minute to thank people following this blog, you are a few but devoted bunch! If you have any questions, comments, or topic suggestions, I will be more than happy to discuss them.

Things I have learned today – Console Apps Using a Timer and CurrentThread!!!

So today I was creating a console application using timers in Visual Basic.Net. It happened to be a really simple, very tiny program. It consisted of two timers (of the System.Timers.Timer variety). All the main function was doing was calling these two timers. One catch to this, this kind of timer is asynchronous (runs on a separate thread from the main function).
I needed the program to wait while these timers (separate threads finished). After thinking about it for a while and after some trial and error here is what I came up with:

System.Threading.CurrentThread.Join(time in milliseconds)

It completely rocks! It will not spike the CPU at all! You will spike the CPU if you were to do something like:
Dim isFinished as Boolean
timer1.Start()

While Not isFinished
End While

CurrentThread is awesome! You can quickly manipulate your program to be have in ways you couldn’t even imagine.

Enjoy!

12/27/2009 – Happy Winter Solstice! Compiled vs. Interpreted Languages

Business meeting Sorry I haven’t had any recent posts lately with all the holiday festivities, I simply didn’t have the time. One thing I did take time to think about is the current state of programming. I must say that I am rather disappointed that a lot of the people graduating college these days have not experienced the joys and sorrows of compiling and linking program.

Modern programming has changed a lot through the years. I think a great deal of changes were brought about through the creation of Java. I have to give it props, it was the first widely popular and accepted interpreted languages. I know some people might say Perl or Lisp (I myself used to be a Perl Monger) came sooner and have a large fan base. Sun’s Java paved the way for the modern programming languages.

Microsoft was quick to follow Sun with their product Dot Net. This is where I spend most of my days anymore for work. They did come up with a slightly different product, an interpretive framework on which C#, Visual Basic, and now F# are a part. Visual Basic is now the most popular language from Microsoft and C# is now a distant second.

These interpreted languages are fast to develop in but still rely on a middle layer to interpret the code you wrote into something the CPU will understand. To this end it is both a strength and a weakness. With this you can adapt your code to change at runtime, which in reality maybe five percent of developers/programmers use this on a regular basis. Another strength is that it is heavily abstracted and which makes coding faster for experienced programmers and allows the novice to do something that they otherwise wouldn’t be able to do.

Not much has changed in compiled languages recently, I think part of this could be due to the fact that Dot Net and Java are so popular. C++ is still popular and in use today. Although its market share is really dwindling. C++ is great for being able to tell the computer exactly what you want. In C++ you are some much closer to the machine code, you can practically see the registers getting loaded and the flags being set on the processor.

Of course, once a compiled program is running, nothing comes between it and the CPU (except for the OS). Lots of control comes with the compiled language as does responsibility and tedium.

A lot of really good practices, patterns, and algorithms are heading to the scrap heap if compiled languages fade into obscurity. I know people will say they will never go away and you are right, they won’t; but they just will not be used by Joe Programmer, who is just trying to pay the mortgage.

These kids today don’t know how good they have it. I still hear the tales of yore; where one man would wire the circuits to just get program to work. I fortunately am not one of them. I am sure I would have learned a lot from experience like that; perhaps the newbies coming into field would benefit from the things I have learned in my personal tales of yore. mailbox