Friday, March 19, 2010

Problem with Creating Activex Controls in Visual Studio 2008



When we add a control (ex. ) to an ascx or aspx
file, visual studio is supposed to automatically add the definition for that
control to the designer.cs file (associated with that ascx or aspx). But it
is not added or updated properly in VS2008

It seems most of the developers are facing this issue.

We need to regenerate the designer class to solve this issue

To regenerate the .designer.cs file for a ASPX/ASCX, first delete the
.designer.cs file, then right click on the ASPX/ASCX and click "Convert to
Web Application". This will probably give you an error message that will
help you find the root of the problem. I noticed that there's a difference
in the error message you might get here versus the file that is being
opened in the Design mode in Visual Studio.

For example, we had a .ASPX file where the .designer.cs file was not being
generated when we modified it. We could not figure out what the error was
until we discovered this tip because Visual Studio did not give us any
feedback. Once I deleted the .designer.cs and ran "Convert to Web
Application" on the file, I was prompted with an error message that informed
me that we had a <%@ Register /> declaration in our ASPX that was also
declared in our web.config file (and this was creating a conflict even if
they both pointed to the same location). Visual Studio could benefit from
better error reporting for this particular scenario.

Thursday, November 26, 2009

Clear Console Window using C#

This step-by-step article demonstrates how to clear the Console window programmatically by using Visual C#.

Sample Program

  1. Start Microsoft Visual Studio.
  2. Create a new Visual C# Console Application project.
  3. Paste the following using statement at the top of the default class:
using nsClearConsole;

4. Paste the following code in the Main procedure of the Console application:

static void Main(string[] args)
{
ClearConsole ClearMyConsole = new ClearConsole();
Console.WriteLine("THIS IS FIRST LINE"); // Some text
Console.WriteLine("THIS IS SECOND LINE"); // Some text
Console.WriteLine("THIS IS THIRD LINE"); // Some text
Console.WriteLine("THIS IS FOURTH LINE"); // Some text
Console.WriteLine("THIS IS FIFTH LINE"); // Some text
Console.WriteLine("Hit Enter to Clear"); // Some text
Console.ReadLine(); // Wait for user input
ClearMyConsole.Clear(); // Clear the screen
Console.WriteLine("THE CONSOLE WAS CLEARED"); // Some text to clear console
Console.WriteLine("Hit Enter to Terminate"); //Some text
Console.ReadLine(); // Wait for user input
}

5. On the Project menu, click Add Class.
6.
In the Add New Item dialog box, select Class, and then click Add.
7.
Replace all of the existing code in the new class with the following code:


using System;
using System.Runtime.InteropServices;

namespace nsClearConsole
{
///
/// Summary description for ClearConsole.
///


public class ClearConsole
{
private const int STD_OUTPUT_HANDLE = -11;
private const byte EMPTY = 32;

[StructLayout(LayoutKind.Sequential)]
struct COORD
{
public short x;
public short y;
}

[StructLayout(LayoutKind.Sequential)]
struct SMALL_RECT
{
public short Left;
public short Top;
public short Right;
public short Bottom;
}

[StructLayout(LayoutKind.Sequential)]
struct CONSOLE_SCREEN_BUFFER_INFO
{
public COORD dwSize;
public COORD dwCursorPosition;
public int wAttributes;
public SMALL_RECT srWindow;
public COORD dwMaximumWindowSize;
}

[DllImport("kernel32.dll", EntryPoint="GetStdHandle", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
private static extern int GetStdHandle(int nStdHandle);

[DllImport("kernel32.dll", EntryPoint="FillConsoleOutputCharacter", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
private static extern int FillConsoleOutputCharacter(int hConsoleOutput, byte cCharacter, int nLength, COORD dwWriteCoord, ref int lpNumberOfCharsWritten);

[DllImport("kernel32.dll", EntryPoint="GetConsoleScreenBufferInfo", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);

[DllImport("kernel32.dll", EntryPoint="SetConsoleCursorPosition", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
private static extern int SetConsoleCursorPosition(int hConsoleOutput, COORD dwCursorPosition);

private int hConsoleHandle;

public ClearConsole()
{
//
// TODO: Add constructor logic here.
//
hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
}

public void Clear()
{
int hWrittenChars = 0;
CONSOLE_SCREEN_BUFFER_INFO strConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO();
COORD Home;
Home.x = Home.y = 0;
GetConsoleScreenBufferInfo(hConsoleHandle, ref strConsoleInfo);
FillConsoleOutputCharacter(hConsoleHandle, EMPTY, strConsoleInfo.dwSize.x * strConsoleInfo.dwSize.y, Home, ref hWrittenChars);
SetConsoleCursorPosition(hConsoleHandle, Home);
}
}
}



8. Press F5 to run the application. Note that the text in the Console window is cleared.