Posts

Showing posts with the label C# Tips and Tricks

Simple User Settings in a C# Windows Forms Application

There is a lot of confusion and long programming blog posts on application settings for a Windows Forms Application in C#.  I needed a quick and simple way to save a few user specific settings such as com port settings and names of labels on a customizable GUI. I have used the Python config package written by Vinay Sajip in a previous project.  The nice thing about the package was that it opened a config file and made an object which could be accessed as config.ports.baudrate .  Since Python is not typed the config file, which is simply Python code without any punctuation marks, is flexible, fast and easy to create. I had hoped that C# would have an easy way to build something similar.  I found an post early on suggesting using app.config.  After reading MANY posts on configuration and trying a lot of code that would not work on Visual Studio 2011 I had about given up and started writing my own. Luckily I found a simple solution. A settings file is already included in a Windows

C# Textbox - Enter to go to next control

I was building a Windows Form Application in Visual C# Express 2010 and had a problem that needed a lot of searching to figure out.   I could not get a value to "enter" and tab to the next textbox when I hit the  Enter key. After much trial and error (most of which would work but I still got a bell when I hit Enter ) I found this. Enter this as the KeyDown Event. Note that KeyPress uses KeyChar return types. private void textbox1_KeyDown( object sender, KeyEventArgs e) {       //checks for Enter, suppresses it and sends a tab       if (e.KeyCode == Keys .Enter && e.Modifiers == Keys .None)     {         e.SuppressKeyPress = true ;  //eliminates the enter key *BELL*         SendKeys .Send( "{TAB}" ); //changes focus to the next control     } }