Live Chat Icon For mobile
Live Chat Icon

WinForms FAQ

Find answers for the most frequently asked questions
Expand All Collapse All

This can be done using the Process class and specifying the verb as ‘Print’. This is the equivalent of right clicking and selecting print in the windows shell. Here is the code snippet that prints documents like MS Excel, MS Word, pdf etc..


// In C#.NET
//Print an Excel document
Process pr = new Process();
pr.StartInfo.Verb = 'Print';
pr.StartInfo.FileName = 'Sample.xls';
pr.Start();

’ In VB.NET
’Print an Excel document
Dim pr As Process =  New Process() 
pr.StartInfo.Verb = 'Print'
pr.StartInfo.FileName = 'Sample.xls'
pr.Start()
Permalink

Visual Basic has a GetType operator that can be used to obtain the System.Type object for the specified type name. This is the equivalent of the typeof keyword in C#.


’ Returns the System.RuntimeType object representing the integer type.
Dim inttype As Type = GetType(Integer)

’ Returns the System.RuntimeType object representing the Project1.Type1 class.
Dim mytype As Type = GetType(Project1.Type1) 
Permalink

.NET Reflection can be used to get hold of non-Public members of a class instance. The System.Type class provides various methods that allow you to access the fields/properties/methods for all defined types. Depending on the context, you can use either the typeof(class) operator or the Object.GetType() method to get hold of the System.Type object representing the particular type and then use that type object to access the required member information.

The following code shows how to use the Type.GetField() and FieldInfo.GetValue() methods to determine the value of the ’pasteIndex’ private field implemented by the Diagram.Controller class.


// In C#.NET
// Use Reflection to access the this.ActiveDiagram.Controller object’s pasteIndex private member 

Type typecontroller = typeof(Syncfusion.Windows.Forms.Diagram.Controller); 
System.Reflection.FieldInfo finfo = typecontroller.GetField('pasteIndex', BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.GetField); 
int npasteindex = 0; 
if(finfo != null) 
npasteindex = (int)finfo.GetValue(this.ActiveDiagram.Controller); 

’ In VB.NET
’ Use Reflection to access the Me.ActiveDiagram.Controller object’s pasteIndex private member 

Dim typecontroller As Type = GetType(Syncfusion.Windows.Forms.Diagram.Controller)
Dim finfo As FieldInfo = typecontroller.GetType().GetField('pasteIndex', BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.GetField)
Dim npasteindex As Integer = 0
If Not finfo Is Nothing Then
   npasteindex = finfo.GetValue(Me.ActiveDiagram.Controller)
End If
Permalink

For example, the ‘C:\WINNT\Microsoft.NET\Framework\v1.0.3705’ dir for the 1.0 framework version.

You can do so using PInvoke to call this native API method:


[C#]
[DllImport('mscoree.dll')] 
// Declaration
internal static extern void GetCORSystemDirectory([MarshalAs(UnmanagedType.LPTStr)]System.Text.StringBuilder Buffer,
	int BufferLength, ref int Length);
// Gets the path to the Framework directory.
System.Text.StringBuilder sb = new System.Text.StringBuilder(1024);
int size;
// returned value in size can be ignored
GetCORSystemDirectory(sb, sb.Capacity, ref size);

[VB.Net]
’ Declaration
Private Declare Function GetCORSystemDirectory Lib 'mscoree.dll' ( ByVal Buffer As System.Text.StringBuilder, ByVal BufferLength As Integer, ByRef Length As Integer) As Integer

’ Gets the path to the Framework directory.
Dim Path As New System.Text.StringBuilder(1024)
Dim Size As Integer
’ returned value in Size can be ignored
GetCORSystemDirectory(Path, Path.Capacity, Size)
Permalink

You can do so using the System.Diagnostics.Process.Start method as follows:


// Just specifying the document name will open the appropriate app based on system settings.
Process.Start('Http://msdn.microsoft.com') 
// Open a word document in Word.
Process.Start('AWordDocument.doc')
// Or open the app directly:
Process.Start() 
Permalink

The version information needs to be set with the ‘AssemblyVersion’ attribute for each project. However, this attribute does not need to be specified in the project’s AssemblyInfo file. You can create a separate file with the ‘AssemblyVersion’ attribute that is shared across projects that you wish to keep in sync. This file can either be shared via VSS, or by including the file as a link in the projects.

Permalink

Make sure to preface the method/property name with the fully qualified interface name that it belongs to, like this:


	// In C#
	// Where someType implements ISomeInterface’s SomeMethod as a private implementation.
	Type someType = someInstance.GetType();
	MethodInfo mi = someType.GetMethod('System.Windows.Forms.ISomeInterface.SomeMethod', 
			BindingFlags.NonPublic | BindingFlags.Instance);
	if(mi != null)
	{
		mi.Invoke(someInsance, new object[]{});
	}

	’ In VB.Net
	’ Where someType implements ISomeInterface’s SomeMethod as a private implementation.
	Dim someType As Type =  someInstance.GetType() 
	MethodInfo mi = someType.GetMethod('System.Windows.Forms.ISomeInterface.SomeMethod', 
			BindingFlags.NonPublic | BindingFlags.Instance)
	If Not mi Is Nothing Then
		’ Assuming no arguments for SomeMethod.
		mi.Invoke(someInsance, Nothing)
	End If

Permalink

You can handle the Application.ThreadException event. See the FrameWork class library for a more detailed sample in both VB and C#.


	[STAThread]
	public static void Main() 
	{
		Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(UnhandledExceptionCatcher);
		Application.Run(new Form1());
	}

	private static void UnhandledExceptionCatcher(object sender, System.Threading.ThreadExceptionEventArgs e)
	{
		Console.WriteLine('caught an unhandled exception.');
	}
Permalink

Share with

Share on twitter
Share on facebook
Share on linkedin

Couldn't find the FAQs you're looking for?

Please submit your question and answer.