Common Winform Methods

Source: Internet
Author: User
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Windows. Forms;
Using System. Reflection;
Using System. Threading;

Namespace EbookToBBs
{
Public class Tool
{
Public static Type RtbType = typeof (RichTextBox );

/// <Summary>
/// Stop a thread. If no thread is stopped, the method does not return
/// </Summary>
/// <Param name = "thread"> </param>
Public static void StopThread (Thread thread)
{
If (thread! = Null & thread. ThreadState! = ThreadState. Stopped)
{
Thread. Abort ();
While (true)
{
If (thread. ThreadState = ThreadState. Stopped)
{
Return;
}
Thread. Sleep (10 );
}
}
}

/// <Summary>
/// Add content for RichTextBox
/// </Summary>
/// <Param name = "rtb"> </param>
/// <Param name = "text"> </param>
Public static void AppendText (RichTextBox rtb, string text)
{
// If rtb is not operated on the UI thread
If (rtb. InvokeRequired)
{
Rtb. Invoke (new Action <string> (delegate (string txt)
{
Rtb. AppendText (txt );
Rtb. AppendText ("\ r \ n ");
}), Text );
}
Else
{
Rtb. AppendText (text );
}
}

/// <Summary>
/// Use reflection to call the RichTextBox Method
/// </Summary>
/// <Param name = "rtb"> </param>
/// <Param name = "methodName"> </param>
Public static void Method (RichTextBox rtb, string methodName)
{
MethodInfo method = GetMethod (RtbType, methodName );
Invoke (rtb, method, null );
}

/// <Summary>
/// Use reflection to call the RichTextBox Method
/// </Summary>
/// <Param name = "rtb"> </param>
/// <Param name = "methodName"> </param>
Public static void Method (RichTextBox rtb, string methodName, Type [] typeArr, ParameterModifier [] paraModiferArr, params object [] args)
{
MethodInfo method = GetMethod (RtbType, methodName, typeArr, paraModiferArr );
If (method = null)
{
Return;
}
Invoke (rtb, method, args );
}

/// <Summary>
/// Call a method of an instance
/// </Summary>
/// <Param name = "rtb"> instance </param>
/// <Param name = "method"> method </param>
/// <Param name = "args"> parameter </param>
Public static void Invoke (RichTextBox rtb, MethodInfo method, params object [] args)
{
// If rtb is not operated on the UI thread
If (rtb. InvokeRequired)
{
Rtb. Invoke (new Action <object []> (delegate (object [] objArr)
{
Method. Invoke (rtb, objArr );
}), New object [] {args });
}
Else
{
Method. Invoke (rtb, args );
}
}

# Region method for obtaining a type
/// <Summary>
/// Obtain a method without Parameters
/// </Summary>
/// <Param name = "type"> </param>
/// <Param name = "name"> </param>
/// <Returns> </returns>
Public static MethodInfo GetMethod (Type type, string name)
{
MethodInfo method = type. GetMethod (name, BindingFlags. Public | BindingFlags. IgnoreCase | BindingFlags. Instance );
Return method;
}

/// <Summary>
/// Obtain a method with Parameters
/// </Summary>
/// <Param name = "type"> </param>
/// <Param name = "name"> </param>
/// <Param name = "typeArr"> </param>
/// <Param name = "paraModiferArr"> </param>
/// <Returns> </returns>
Public static MethodInfo GetMethod (Type type, string name, Type [] typeArr, ParameterModifier [] paraModiferArr)
{
MethodInfo method = type. GetMethod (name, BindingFlags. Public | BindingFlags. IgnoreCase | BindingFlags. Instance, null, typeArr, paraModiferArr );
Return method;
}
# Endregion
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.