ComboBox Exception: “Too many items in the combo box”

1 Comment »

The .NET ComboBox may throw a cryptic OutOfMemoryException with the following message:

Too many items in the combo box.

This poorly-worded exception results when you Add an object to the ComboBox whose ToString() method returns a null or empty string. 

To fix this error, make sure that for every object that you add to the ComboBox, the ToString() method returns a non-empty string.

How to Launch a Process Synchronously

1 Comment »

Most developers use the Process.Start static method to run an external application from within C# code.  The Start method launches the external process asynchronously, meaning that your C# code continues executing and does not wait for the process to finish.

But occasionally you may wish to halt your program and wait for the external process to finish.  So to launch a process synchronously from a C# application, the key is to create a Process object and call the WaitForExit method after you start the process.  Be sure to finish with a call to the process Close method.  Here is some sample code:

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo( "notepad.exe" );
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
process.Close();
MessageBox.Show( "Process Complete" );

Where to Find SN.exe

1 Comment »

SN.exe is a Strong Name tool that can sign assemblies, manage strong name keys, and generate and verify signatures.  You will typically find it here:

C:Program Files (x86)Microsoft Visual Studio 8SDKv2.0Bin

If you cannot find it there, check “C:Program Files” on 32-bit systems.  Also check the folders corresponding to other versions of Microsoft Visual Studio, such as “Microsoft Visual Studio 9.0” etc.  If you still cannot find it, run a search on your C: drive. 

If SN.exe is not installed on your hard drive, you can download it here:

.NET Framework 2.0 Software Development Kit (SDK)
x86   x64

According to Microsoft, since .NET Framework versions 3.0 and 3.5 are built incrementally on the .NET Framework version 2.0, many of the tools included in the .NET Framework 2.0 SDK are the latest versions available.  But just in case, you can download the newest version of .NET:

.NET Framework 4 redistributable package

Display Icons in a ListBox

No Comments »

The standard Windows Forms ListBox control is not designed to display an icon with each item.  You can modify the ListBox to handle the DrawItem event and manually draw the items and their associated icons, as shown here and here

However, if you were hoping to use a standard off-the-shelf control with full icon support, your best bet is to use the ListView control.

Where to Find MSBuild.exe

No Comments »

MSBuild is the build tool for Microsoft Visual Studio.  Developers use MSBuild to build a Visual Studio project or solution file from a command line, batch file, build tool, or automated script. 

A new version of MSBuild.exe is included with each version of the .NET Framework stored in “%WinDir%Microsoft.NETFramework” on your Windows PC.  The MSBuild.exe path depends on the .NET Framework version used to build your project.  Typically you will want to call the most recent version of MSBuild.exe installed on your computer:

.NET v2.0:
%WinDir%Microsoft.NETFrameworkv2.0.50727MSBuild.exe

.NET v3.5:
%WinDir%Microsoft.NETFrameworkv3.5MSBuild.exe

.NET v4.0:
%WinDir%Microsoft.NETFrameworkv4.0.30319MSBuild.exe

MSBuild Command Line Reference

C# TextBox Scroll to Cursor

No Comments »

To scroll a C# TextBox to the cursor/caret, it’s important that the TextBox is both visible and focused, then call the ScrollToCaret method:

textBox.Focus();
textBox.ScrollToCaret();

To scroll to the bottom/end of a TextBox, set the SelectionLength to 0 to remove any selection, then set SelectionStart to the end of the text in the TextBox:

textBox.SelectionLength = 0;
textBox.SelectionStart = textBox.Text.Length;
textBox.Focus();
textBox.ScrollToCaret();

Mono 2.8 Released with C# 4.0 Support

No Comments »

Mono.  Trademark by Novell.

Mono is an open source implementation of Microsoft’s .NET Framework based on the ECMA standards for C# and the Common Language Runtime.  Mono enables developers to build Linux and cross-platform applications with improved productivity.

Sponsored by Novell, Mono has released version 2.8, which includes full support for C# v4.0, improvements to the optional LLVM-based Mono backend, and more efficient garbage collection.

Download Mono 2.8

 

Get More .NET News

Are you interested in .NET development news like this without having to wade through pages of code and crap?  Then check out Dot-Net-News.com for the latest news and information about the Microsoft .NET Framework and development ecosystem, including C#, Visual Basic and Visual Studio.  No fluff, no spam, just the facts, man.

C# Code Converter

No Comments »

DeveloperFusion offers a free .NET code converter.  Simply paste your C# or VB.NET code into this web-based tool, then select your target language: C#, VB.NET, Python or Ruby.  Supports syntax up to .NET 3.5.

Code Converter

Follow C# 411 on Twitter

No Comments »

Tired of RSS feeds?  Now you can follow C# 411 on Twitter!

C# 411 on Twitter

Cool Twitter logo from here

Type Name “UITypeEditor” Not Found

1 Comment »

This is one of those “D’oh!” moments.  You’re creating your own UITypeEditor.  You know the UITypeEditor class is located in the System.Drawing.Design namespace.  So naturally you want to add to your Visual Studio project a reference to the System.Drawing.Design.dll, right?  Wrong!  When you compile your project, the following error may appear:

The type or namespace name ‘UITypeEditor’ could not be found (are you missing a using directive or an assembly reference?)

It turns out that UITypeEditor is actually defined in System.Drawing.dll, even though it’s located in the System.Drawing.Design namespace.  See the disconnect?  But you can easily solve this problem by adding to your Visual Studio project a reference to System.Drawing.dll

« go backkeep looking »