Type converters let you convert one type to another type. Each type that you declare can optionally have a TypeConverter associated with it using the TypeConverterAttribute. If you do not specify one the class will inherit a TypeConverter from its base class.
The following methods deal with the type conversion work that is performed by TypeConverters.
// for conversion from another type to the type that this type converter is associated withpublicoverrideboolCanConvertFrom(ITypeDescriptorContext context, Type sourceType);
publicoverrideobjectConvertFrom(ITypeDescriptorContext context, CultureInfo culture, objectvalue);
// for conversion of this type to some other typepublicoverrideboolCanConvertTo(ITypeDescriptorContext context, Type destinationType);
publicoverrideobjectConvertTo(ITypeDescriptorContext context, CultureInfo culture, objectvalue, Type destinationType);
The above functions provide the core of TypeConverters. One reason for the confusion surrounding TypeConverters is the fact that they have other functions that are unrelated to the primary type conversion function. Methods such as public PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value) do not perform type conversion. They are present for another purpose.
Typically when we require access to the properties, events and methods we use a TypeDescriptor. However there are .NET framework elements such as the PropertyGrid that first access the TypeConverter and then query it for a list of Properties (it returns true when public bool GetPropertiesSupported(); is called). If the TypeConverter returns false from public bool GetPropertiesSupported(), then the TypeDescriptor is queried for such information. Deriving a type converter is an easy way to present properties, events etc in any manner that you want especially with the property grid. In your own code also when you access Property information it is good practice to query the TypeConverter first. So, presentation of object properties, methods and events is also a function of the TypeConverter. Unfortunately, the name ’TypeConverter’ does not hint at this function.
Enclosed is a small sample that illustrates the use of a custom Type Converter for conversion. Download TypeConverter.zip. Try the sample and it will be clear what Type Converters primarily do.
The CollectionEditor allows adding and removing items from a collection at design time. If the items in this collection implement IComponent or if they are derived from Component, the items in your collection can be persisted in code.
2) Next implement your Collection. You have to implement the Ilist interface. The CollectionEditor will determine the type of instances to be added to your collection using reflection inspecting the return type of the Item property (Indexer).
[
Description(''The set of properties to be edited with CollectionEditor.''),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Editor(typeof(System.ComponentModel.Design.CollectionEditor),
typeof(System.Drawing.Design.UITypeEditor))
]
public SomeItemCollection SomeItems
{
get
{
if (someItemCollection == null)
{
someItemCollection = CreateItemCollection();
}
return someItemCollection;
}
}
protected SomeItemCollection CreateItemCollection()
{
returnnew SomeItemCollection(this);
}
publicclassSomeItemCollection : IList
{
// Fieldsprivate SomeItemDisplayer owner;
publicevent EventHandler Changed;
// ConstructorspublicSomeItemCollection(SomeItemDisplayer owner)
{
this.owner = owner;
}
internal ArrayList InnerArray
{
get
{
return owner.someItems;
}
}
publicvoidOnChanged()
{
if (this.Changed != null)
this.Changed(this, EventArgs.Empty);
}
////// The CollectionEditor will determine the type of objects to be created by/// looking at the property type of the following method. CollectionEditor/// internally uses reflection to get the PropertyInfo for the ''Item'' property. /// This method must be public.///public SomeItem this[int index]
{
set
{
if (value == null)
thrownew ArgumentNullException(''value'');
if (index < 0 || index >= this.InnerArray.Count)
thrownew ArgumentOutOfRangeException(String.Format(''Invalid Argument {0}: {1}'', ''index'', index.ToString()));
this.InnerArray[index] = value;
OnChanged();
}
get
{
if (index < 0 || index >= this.InnerArray.Count)
thrownew ArgumentOutOfRangeException(''Invalid Argument {0}: {1}'', ''index'', index.ToString());
return (SomeItem) this.InnerArray[index];
}
}
publicvoidAddRange(object[] items)
{
InnerArray.AddRange(items);
OnChanged();
}
////// This implementation for the Item property for the IList interface. It’s/// property type is object.///object IList.this[int index]
{
set
{
// forward call to public indexer this[index] = (SomeItem) value;
}
get
{
// forward call to public indexerreturnthis[index];
}
}
public/*IEnumerable*/IEnumerator GetEnumerator()
{
return InnerArray.GetEnumerator();
}
public/*ICollection*/int Count
{
get
{
return InnerArray.Count;
}
}
public/*IList*/voidRemoveAt(int index)
{
if (index < 0 || index >= this.InnerArray.Count)
thrownew ArgumentOutOfRangeException(String.Format(''Invalid Argument {0}: {1}'', ''index'', index.ToString()));
this.InnerArray.RemoveAt(index);
OnChanged();
}
public/*IList*/voidRemove(objectvalue)
{
int n = this.InnerArray.IndexOf(value,0);
if (n != -1)
this.RemoveAt(n);
}
public/*IList*/voidInsert(int index, object item)
{
if (item == null)
thrownew ArgumentNullException(''item'');
if (index < 0 || index > this.InnerArray.Count)
thrownew ArgumentOutOfRangeException(String.Format(''Invalid Argument {0}: {1}'',''index'', index.ToString()));
this.InnerArray.Insert(index,item);
OnChanged();
}
public/*IList*/intIndexOf(objectvalue)
{
if (value == null)
thrownew ArgumentNullException(String.Format(''Invalid Argument {0}: {1}'',''value'', ''null''));
returnthis.InnerArray.IndexOf(value,0);
}
public/*IList*/bool IsReadOnly
{
get
{
returnfalse;
}
}
public/*IList*/voidClear()
{
InnerArray.Clear();
this.owner.Invalidate();
}
public/*IList*/boolContains(objectvalue)
{
returnthis.IndexOf(value) != -1;
}
void System.Collections.ICollection.CopyTo(Array dest, int index)
{
int count = this.InnerArray.Count;
for (int n1 = 0; n1 < count; n1++)
dest.SetValue(this.InnerArray[n1], (n1 + index));
}
int System.Collections.IList.Add(object item)
{
int n = this.InnerArray.Add(item);
OnChanged();
return n;
}
bool System.Collections.IList.IsFixedSize
{
get
{
returnfalse;
}
}
bool System.Collections.ICollection.IsSynchronized
{
get
{
returnfalse;
}
}
object System.Collections.ICollection.SyncRoot
{
get
{
returnthis;
}
}
}
3) Reference this collection in your control or component that should be designable. You need to supply a DesignerSerializationVisibility and an Editor attribute:
private SomeItemCollection someItemCollection = null;
ArrayList someItems = new ArrayList();
[
Description(''The set of properties to be edited with CollectionEditor.''),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Editor(typeof(System.ComponentModel.Design.CollectionEditor),
typeof(System.Drawing.Design.UITypeEditor))
]
public SomeItemCollection SomeItems
{
get
{
if (someItemCollection == null)
{
someItemCollection = CreateItemCollection();
}
return someItemCollection;
}
}
protected SomeItemCollection CreateItemCollection()
{
returnnew SomeItemCollection(this);
}
3) Using implicit or explicit operators defined in the source or destination
type. This will allow you to perform implicit or explicit casts from the
source type to the destination type.
SizeF sizeF = size; // Possible because type Size has an implicit type conversion operator that converts a Size to SizeF
PointF pointF = (PointF)sizeF; // Possible because type SizeF has an explicit type conversion operator that converts a SizeF to PointF.
There is usually a corresponding ToXXX method that does the same conversion. You can use either this or the ToXXX methods in C#.
4) Using TypeConverters. Some types come with TypeConverters that allow you to convert to or convert
from another type. This conversion is usually not documented and can be
discovered only by writing some test code.
For example, the System.Drawing.Icon type’s TypeConverter converts the Icon
into a byte[]. You can use this functionality in your code as follows:
It’s usually time consuming to figure out whether a TypeConverter’s
ConvertTo or ConvertFrom method can perform the necessary conversion.
The attached TypeConverterLookup tool lets you figure that out easily on any type declared in any assembly available in the GAC or available in the same directory as the tool’s exe. If you have types in custom assemblies, then just copy over that assembly to the same directory as the tool, you can then specify the type in the tool.
Follow these steps to introduce Windows XP visual styles into your Windows application.
1. Create a new Windows Application and add some controls to the default form.
2. For every control you place on the form that has a FlatStyle property, set the property to System.
3. Compile your application.
4. Build a manifest file in the application directory. NOTE: This manifest file must be located in the same directory as the executable. Open Notepad and place the code shown below in the file.
<?xml version='1.0' encoding='UTF-8' standalone='yes'?><assemblyxmlns='urn:schemas-microsoft-com:asm.v1'manifestVersion='1.0'><description>.NET control deployment tool</description><dependency>
dependentAssembly>
<assemblyIdentitytype='win32'name='Microsoft.Windows.Common-Controls'version='6.0.0.0'processorArchitecture='X86'publicKeyToken='6595b64144ccf1df'language='*'
/></dependentAssembly></dependency></assembly>
Save the file in the application directory with the name of the application and an extension of exe.manifest. For instance, if your application name is ‘MyApp’, you will name the manifest file as MyApp.exe.manifest.
5. When your application runs, Windows XP looks at the application manifest which tells the operating system to use common controls library version 6.0.
This process is discussed and a sample given in an article by the David C. Brown on the Windows Forms team at MSDN.
The .manifest file is not required if you are using .NET FrameWork 1.1. You can now use the application’s EnableVisualStyles() method which should called before creating any controls. You also need to ensure that the FlatStyle property of the control is changed to System instead of the default value of Standard.
Erick Ellis steps you through creating a Windows Form and then adding an object tag to an HTML page in his article Using Windows Forms Controls in IE on gotnetdot.com.
From within the designer: 1) From the ToolBox, drop a ToolTip control to your form. 2) Check the properties of this toolTip1 object to make sure Active is set to true. 3) Click on your button, and in its Property page, set the ToolTip on toolTip1 entry.
From code:
private System.Windows.Forms.ToolTip toolTip1;
......
......
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.toolTip1.Active = true;
......
......
this.toolTip1.SetToolTip(this.button1, 'Press for information...');
Be careful when using the Timer from System.Timers from within a Windows.Form application. Most methods in Windows Forms are not thread safe and can produce indeterminate results when called from another thread. In this case, System.Timers.Timer is much higher resolution and less affected by how busy your application is (since it doesn’t use the message pump) but it’s Tick event is fired from another thread. In order to be sure this works correctly, you’ll have to write the proper Control.Invoke code to marshal over to the thread the control was created on (the main thread) before you call methods on the control. So, in cases where you don’t need super-high-resolution and regular events to be fired, you’re much better off using System.Windows.Forms.Timer. It’s message pump is based (WM_TIMER) and will fire on the UI thread.
(from sburke_online@microsoft..nospam..com on microsoft.public.dotnet.framework.windowsforms)
You can add a web browser to a form in a straight forward manner. 1) Right-click your toolbox and select Customize Toolbox. Then add the Com Component ’Microsoft Web Browser’. 2) Drag the just added Explorer tool and drop it on your form to position it where you want it. 3) Set the initial display page using code such as:
publicForm1()
{
//// Required for Windows Form Designer support//
InitializeComponent();
object a = 0;
object b = 0;
object c = 0;
object d = 0;
axWebBrowser1.Navigate('www.syncfusion.com', ref a, ref b, ref c, ref d);
//// TODO: Add any constructor code after InitializeComponent call//
}
You can provide Intellisense support to your type and it’s members by providing xml comments in code as follows:
/// <summary>/// Summary description for Form3./// </summary>publicclassForm3 : System.Windows.Forms.Form
{
/// <summary>/// Clean up any resources being used./// </summary>protectedoverridevoidDispose( bool disposing ){
}
/// <summary>/// Summary of my property/// </summary>publicbool MyProperty
{
get{..}
set{..}
}
}
Search for the ‘Tags for Documentation Comments’ topic in MSDN for all the available documentation tags.
Then in your project, go to the Project Properties dialog, to the Configuration Properties/Build tab and specify a file for the XML Documentation File property. This will generate a file by that name when you compile your assembly. Place this xml file beside your dll. This will provide Intellisense support for your types in that assembly.
To provide Description support for your properties in the property grid in the designer, add the DescriptionAttribute attribute to your properties in code.
////// Summary of my property///
[Description('description of the property')]
publicbool MyProperty
{
get{..}
set{..}
}
If you add a BMP file to your solution using the File|Add Existing Item… Menu Item, then change the Build Action property of this BMP file to Embedded Resource, you can then access this resource with code similar to:
// WindowsApplication6 corresponds to Default Namespace in your project settings.// subfolders should be the folder names if any, inside which the bmp is added. If the bmp was added to the top level, you don’t have to specify anything here.string bmpName = 'WindowsApplication6.subfolders.sync.bmp';
System.IO.Stream strm = null;
try
{
strm = this.GetType().Assembly.GetManifestResourceStream(bmpName);
pictureBox1.Image = new Bitmap(strm);
// Do the same for Icons // Icon icon1 = new Icon(strm);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
if(strm != null)
strm.Close();
}
Parse through all the Controls in the designer and call TypeDescriptor.Refresh() on them.
// From inside your custom IDesigner implementation: privatevoidUpdateExtendedProperties()
{
IDesignerHost idh = this.GetService(typeof(IDesignerHost)) as IDesignerHost;
foreach (Component comp in idh.Container.Components)
{
// Ignoring the Formif ((comp is Control) && ((comp is Form) == false))
{
Control ctrl = comp as Control;
TypeDescriptor.Refresh(ctrl);
}
}
}
You have to set DesignerSerializationVisibility attribute on the property whose type is a strongly-typed collection to Content. You have to ensure the collection is created at startup or on demand. In order to support serialization of items that do not implement IComponent into your code you have to write a TypeConverter for that class that can convert to InstanceDescriptor. See the documentation on InstanceDescriptor for an example.
The CLR is catching an Access Violation that’s being thrown from unmanaged code, and that is propagating up as a NullReferenceException. I’ve seen this happen with certain common control library windows types if an application such as spy++ is running, and I see this is the TreeView control that is having troubles with a mouse down. Have you done any modification to the control through P/Invoke methods?
(from sburke_online@microsoft..nospam..com on microsoft.public.dotnet.framework.windowsforms)
publicclassMyClass
{public ArrayList myArrayList = new ArrayList();
publicMyClass(){}
publicoverridevoidMyOverride(){
// When this gets called by the base class constructor, myArrayList will be initialized.
}
}
You could use the DateTime.Ticks property to record the time taken for a long operation, as follows:
[C#]
privatevoidMyLongOp()
{
long startTicks = DateTime.Now.Ticks;
// Perform a long op:long endTicks = DateTime.Now.Ticks;
long delta = endTicks - startTicks;
MessageBox.Show('Time taken in Ticks: ' + delta.ToString());
delta = delta/(long)10000000; // A tick is 100 nanoseconds
MessageBox.Show('Time taken in seconds: ' + delta.ToString());
}
[VB.Net]
Private Sub MyLongOp()
Dim startTicks As Long = DateTime.Now.Ticks
’ Perform a long op:
Dim endTicks As Long = DateTime.Now.Ticks
Dim delta As Long = endTicks - startTicks
MessageBox.Show('Time taken in Ticks: ' + delta.ToString())
delta = delta/(Long)10000000 ’ A tick is100 nanoseconds
MessageBox.Show('Time taken in seconds: ' + delta.ToString())
End Sub
An easy way to keep the program from appearing in the Alt+Tab list is to set the Form’s FormBorderStyle property to be a ToolWindow (fixed or sizable).
One caveat to this is that if you display another Form (that is not a ToolWindow) through a call in your application, the main Form will again appear in the Alt+Tab listing. To get around this, you can make all of the Forms in your Tray application have a FormBorderStyle of ToolWindow.
You need to go to Tools->Options->Text Editor->All Languages->General->Word Wrap and check/uncheck the checkbox or you could press Ctrl+R Ctrl+R to toggle this property.
Yes, in Code View you can select and drag code to aTab (General Tab) in the ToolBox and then you can drag the code from the Tab to the desired location.
In the Solution Explorer right click on the project that should be the start up project for your application and choose the Set As Start Up Project option.
You can set the build order of the projects by right clicking on the Solution and choosing Project Build Order and adjusting the dependencies on the Dependencies tab in the Project Dependencies window that pops up.
To get the default namespace of a C# project developed using VS.NET, you need to right-click on the project in the Solution Explorer and then choose Properties->Common Properties->General->Default Namespace
However, this would require all of the systems that build the application to have the exact same directory structure locally. There are often times when this is not wanted (or needed), such as having a script on a build machine.
In this type of situation, you can use the DEVPATH environment variable, or make use of the Public Assembly folder functionality
To use the DEVPATH environment variable, you would simply add the folder that contains the assembly (e.g. ‘c:\work\samples\assemblies’) to the list of directories. Then, you will need to specify the <developmentMode> element in the machine configuration file:
This will instruct the CLR to locate the assemblies based on the DEVPATH environment variable.
A less restrictive way to specify the assembly locations is by making use of the Public Assembly Folder functionality. This is done by adding a path to the PublicAssemblies registry key.
Open up the registry, and locate the ‘HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.0\AssemblyFolders’ key (for VS.Net 2003, you would look under the ‘…\VisualStudio\7.1\AssembliesFolders’ key). You will see a subkey named ‘PublicAssemblies’. This is where you will want to add additional keys to create your own Public folders. To add your own folder, create a key (e.g. MyAssemblies), and set the default value to be the desired path (e.g. ‘c:\work\samples\assemblies’).
The option you choose will simply depend on your needs.
But, note that when in Visual Inheritance mode (designing a derived form), your Control’s DesignMode property will be true when the base form’s constructor gets executed in the design-time.
To workaround this, you could check if the app in which your control is running is not devenv.exe, as follows:
string exePath = Application.ExecutablePath;
exePath = exePath.ToLower();
if(Application.ExecutablePath.ToLower().IndexOf('devenv.exe') > -1)
{
// Then you are running in vs.net.
}
There are two ways in which this can be done. The first way is to use the provided Public assembly folder that is installed with VS.Net.
This folder is: ‘C:\Program Files\Microsoft Visual Studio .NET\Common7\IDE\PublicAssemblies’
All assemblies in this folder will be picked up by the ‘Add Reference’ dialog under the ‘.NET’ tab, and the ‘Customize Toolbox’ under the ‘.NET Framework Components’ tab.
Now, you are not limited to using this folder. You can specify your own public assembly folder by adding a key to the registry.
If you look at the following key: ‘HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.0\AssemblyFolders’, you will see a subkey named ‘PublicAssemblies’. This is where the path above is specified as a public assembly folder. To add your own folder, create a key (e.g. MyAssemblies), and set the default value to be the desired path.
You need to use a second instance of VS.NET to debug the one that’s running the code.
Put your control on a from in VS.NET Start a 2nd Vs.Net Choose the Debug menu >> Processes … Double click ‘devenv.exe’ and choose ‘Common Language Runtime’ as the types of debugging Open your code file, set your breakpoint, and you’re debugging.
Posted by Shawn Burke of MSFT in microsoft.public.dotnet.framework.windowsforms.
Set the Designer attribute for your custom dialog to be ComponentDesigner (instead of the default ControlDesigner). That should make it droppable in the design surface as a component.
You cannot move it via drag-and-drop. But you change it’s position or dock order by selecting the ‘Bring To Front’ or ‘Send To Back’ verbs in it’s context menu. ‘Bring To Front’ will move it to the top of the children list and ‘Send To Back’ will move it to the last of the children list. This is possible because the docking logic will be applied on the children in the order in which they appear in the child controls list.
In Visual Studio, you can store commonly used code snippets on a tab (other than the Clipboard ring) in your Toolbox. You use drag and drop techniques to move the snippets to the Toolbox. To later use a snippet in your code, you drag it from the Toolbox and drop it into your code.
In design mode, you drop a NotifyIcon object on your form. You can then drop a ContextMenu on your form and add this menu to the NotifyIcon’s ContextMenu property. This menu will be seen when the user rightclicks the try icon. You can add a handler for the NotifyIcon’s Click event to catch the action of a user clicking the icon.
From code, you create an instance of NotifyIcon, set properties, hook any handlers you want, and then make it visible. Here are some VB snippets.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim trayItem As NotifyIcon = New NotifyIcon()
trayItem.Icon = SystemIcons.Question
trayItem.Visible = True
AddHandler trayItem.Click, New EventHandler(AddressOf HandleTrayClick)
End Sub
Private Sub HandleTrayClick(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show('Tray item clicked')
End Sub
This behavior has nothing to do with .Net and is expected Windows behavior beginning with Win2000. Windows will only show accelerator keys after the alt is pressed. There is a control panel setting that you can use to change this behavior. Look on the Effects tab under Control Panel|Display to see a checkbox labeled ‘Hide keyboard navigation indicators until I use the Alt key’.
Its looks like you cannot change this behavior of MessageBox. One solution is to derive your own MessageForm class from Form to display your message. Then call its ShowDialog method to show it after you set its Size, Location and StartPosition properties. (If you don’t set the StartPosition , then the Location is ignored.) One of the values for StartPosition is CenterParent which would center your new MessageForm.
While MFC programmer’s have had this feature available in earlier versions of Visual Studio, VB programmers have not. To display tab orders on the active design mode Form, select the View | Tab Order menu item. Then click the numbers on each control to reset their tab order.
You have to implement a TypeConverter for your class and override the GetStandardValues and GetStandardValuesSupported method. In your override of GetStandardValuesSupported you have to return true. In your override of GetStandardValues you should return the list of values.
Optionally you can override GetStandardValuesExclusive and allow the user to specify values that are not in the value list.
Note: The standard values collection can be initialized at runtime depending on the context of the instance object.
publicclassGridCellValueTypeConverter: TypeConverter
{
publicoverrideboolCanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(System.String))
returntrue;
returnbase.CanConvertFrom(context,sourceType);
}
publicoverrideobjectConvertFrom(ITypeDescriptorContext context, CultureInfo culture, objectvalue)
{
if (valueis System.String)
{
if (((string) value) != '')
return Type.GetType((string) value);
elsereturnnull;
}
returnbase.ConvertFrom(context,culture,value);
}
// no string conversionpublicoverrideobjectConvertTo(ITypeDescriptorContext context, CultureInfo culture, objectvalue, Type destinationType)
{
if (destinationType == typeof(String))
{
Type type = (Type) value;
if (type == null)
return String.Empty;
elseif (type.Namespace == 'System')
return type.ToString();
else
{
return String.Concat(type.FullName, ',', type.AssemblyQualifiedName.Split(’,’)[1]);
}
}
returnbase.ConvertFrom(context,culture,value);
}
publicoverride System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return svc;
}
publicoverrideboolGetStandardValuesExclusive(ITypeDescriptorContext context)
{
returnfalse;
}
publicoverrideboolGetStandardValuesSupported(ITypeDescriptorContext context)
{
returntrue;
}
// FieldsstaticGridCellValueTypeConverter()
{
values = newstring[]
{
'System.String',
'System.Double',
'System.Int32',
'System.Boolean',
'System.DateTime',
'System.Int16',
'System.Int64',
'System.Single',
'System.Byte',
'System.Char',
'System.Decimal',
'System.UInt16',
'System.UInt32',
'System.UInt64',
};
Array.Sort(values);
Type[] types = new Type[values.Length];
for (int i = 0; i < values.Length; i++)
types[i] = Type.GetType(values[i]);
svc = new TypeConverter.StandardValuesCollection(types);
}
privatestaticstring[] values;
privatestatic TypeConverter.StandardValuesCollection svc;
}
When you add an existing item from with the Visual Studio.NET IDE, click the small arrow on the ’Open’ button in the dialog that is presented. You will see a list of options. One of these is to ’Link file’. If you select this option then you will not get a local copy and any changes that you make to the linked file will be in the original file.
Another way to get the same effect is to share the files using Visual Source Safe. You can simply drag and drop the files between projects in VSS and they will also be in sync.
The Resize event is getting raised before the constructor completes because the form is being resized in the constructor. If you are using VS.NET to create your project, then in your constructor there is a call to InitializeComponent. In this method there is code to resize the form. You could instantiate your child form in your constructor *before* InitializeComponent is called, then when the form gets resized for the first time you already have an instance of your child form.
Click ‘Project | Properties’ from the menus. Select ‘Configuration Properties’ folder and the ‘Build’ item under that. Switch ‘Allow unsafe code blocks’ from ‘False’ to ‘True’.
(from Ryan LaNeve on microsoft.public.dotnet.framework.windowsforms)
That marks the thread as being ‘Single Thread Apartment’ which means any multiple threaded calls need to be marshaled over to that thread before they are called. That’s there because Windows Forms uses some OLE calls (Clipboard for example), which must be made from the thread that initialized OLE.
(from sburke_online@microsoft..nospam..com on microsoft.public.dotnet.framework.windowsforms)
The SetDataObject and GetDataObject methods in the Clipboard class found in the System.Windows.Forms namespace allows you to access the clipboard. Here is some code.
string text = 'Some text for the clipboard';
Clipboard.SetDataObject(text); //clipboard now has 'Some text for the clipboard'
text = ''; //zap text so it can be reset...
IDataObject data = Clipboard.GetDataObject();
if(data.GetDataPresent(DataFormats.Text))
{
text = (String)data.GetData(DataFormats.Text);
//text is now back to 'Some text for the clipboard'
}
Simply type devenv.exe from the command line. If you get a message like this, then you do not have devenv.exe in your path. >>> ’devenv.exe’ is not recognized as an internal or external command, operable program or batch file. >>> To fix this simply run the batch file, vsvars32.bat that comes with Visual Studio.NET from the command line in the working folder. After you run this batch file devenv.exe will be available from the command line in that folder.
Subscribe to IDesignerHost.LoadComplete event. Make any changes to the designerhost (like adding/deleting component, etc.) only after this event, or else the design document will not be made ‘dirty’. (IdesignerSerializationManager.SerializationComplete will tell you when Code is deserialized into design time components).
In the Solutions Explorer window, right-click the exe project and then select Properties in the popup. Then change the Output type from Windows Application to Class Library. You may also want to comment out the Main method in your code as it is not needed in the library project.
To convert from a DLL to and exe, reverse the process.