Ask the Doc Detective
Finding what you need in the Microsoft® Visual Studio® 2008 documentation, which has over 200,000 topics, can be a daunting task. The Doc Detective is here to help, utilizing his investigative skills to probe the depths of the documentation.
Can’t find what you’re looking for? Just ask-if it’s in there, I’ll find it for you; if it isn’t, I’ll let you know that as well (and tell you where else you might go to find it).
Dear Doc Detective,
I need to prevent my users from running more than one copy of my application at a time. Is there an easy way to tell if they are starting a second copy, then stop them and display a message?
-Stuck in Stuckeyville
There is more than one way, and whether it's easy depends on the language that you are using.
If you're using Visual Basic, you can simply set the Make Single Instance Application project option as described in "How to: Specify Instancing Behavior for an Application". When the user tries to launch a second instance, the first instance will be brought to the foreground instead.
Once you've set the property, a new StartupNewInstance event is fired when the user tries to launch the application-you can add code here to display a message. This is described in the topic "My.Application.StartupNextInstance Event".
If you aren't using Visual Basic, you can still check for a running instance of the application by calling the GetProcessesByName method in the System.Diagnostics namespace-it’s just a little harder. Just check the array returned by GetProcessesByName to see if it contains your application’s name, and if so, prevent the new instance from launching.
Dear Doc Detective,
I'm running into issues with the behavior of a transparent label control on top of a picture box. When adding a transparent label control to form with a background image, the label displays correctly and shows the background.
When you add a picture box control and add another transparent label on top of said picture box, the transparent label control on top of a picture box does not display correctly-the background doesn't show through. What's up with that?
-Miffed in Mayberry
What you are seeing is a known limitation of Windows Forms. Labels are transparent to their container or parent-not to the control underneath them. Windows does not support transparency so Windows Forms fakes it by painting the parent’s background inside the bounds of the label.
Fortunately there are a couple of ways to work around this, neither of which involves a label. The first would be to draw the text yourself in the parent form’s Paint event. For information on drawing text, look for the topic "How to: Draw Text at a Specified Location".
The other solution is to remove the PictureBox control and draw its contents in the Paint event. The topic "How to: Load and Display Bitmaps" explains how to draw an image on a form.
Either solution will allow your floating label to have the correct transparent behavior.
Dear Doc Detective,
I’m having trouble printing a report based on a Grid control. I've written the logic for printing using a PrintDocument, but the one thing I can't figure out is how to wrap the text. If the text in a given cell gets too large, it's extending the LayoutRectangle, which isn't what I want.
-Sprung in Springfield
If I understand you correctly, you are trying to print the contents of a data grid (DataGridView) control and the LayoutRectangle is changing in size based on the amount of text in a given cell. If you want the LayoutRectangle size to remain constant, you can do so by changing the StringFormat settings. The article "Printing Reports in Windows Forms" describes this in detail.
Dear Doc Detective,
I have a problem with a MDI application that I'm working on. I have placed an image in a picture box on the MDI parent form, but when I add child forms at run time, they appear hidden behind the picture box.
This obviously isn't the behavior that I want. How can I make the child forms appear on top?
-Screaming in Scranton
Believe it or not, this is the way it was designed to work. Any controls that you place on a MDI container form will appear in front of the Child forms. In general you shouldn't put controls on a MDI parent form, but there are cases where it is useful-for example, adding a StatusBar to the bottom of the form.
To display a picture on the MDI parent, use the BackgroundImage property of the form itself instead of using a PictureBox control-the BackgroundImage will appear behind the Child forms. While this isn't specifically covered in the docs, you can learn more about MDI in the topic "Multiple-Document Interface (MDI) Applications". Now you can come out of hiding.
Doc’s Doc Tip of the Day
Ever notice that sometimes when you press F1 in the Code Editor you get the wrong result? A reader reported a discrepancy when trying to get help on File.Open-placing the cursor on the word Open and pressing F1 brought up the right topic, but highlighting Open and pressing F1 brought up a SQLConnection,Open topic.
While this behavior may seem odd, it's actually by design. The F1 logic looks at the current position of the cursor, uses that word and any surrounding words to establish context, and then it does the F1 lookup.
If you highlight the word, it doesn’t look for context-it just finds the first instance of that keyword and displays the topic. In this case, the SQLConnection.Open topic has a higher priority than the File.Open topic, so it was found first.
If you highlight both File and Open, it “might” have enough context to find the right topic, but if there is more than one File.Open topic you may still get the wrong one. Bottom line-don’t highlight the keyword and you’ll be fine.
Found a topic in Help that doesn't help? Tell the Visual Studio documentation team about it by clicking on the "Send feedback" link in local Help topics, or the "Click to rate and give feedback" link in online Help.