Encoding type : To add new Encoding we have to add new member in here,with the CodePage value as an value of the new encoding and have to modify FileHandler.DetectEncoding function.
You can pass the path of the file and this method returns the encoding format of type which has been defined in enum EncodingType.You just modify the enum with your encoding and code page value and get the encoding format of any file.
private Encoding DetectEncoding(string fileName) { byte[] data = new byte[4]; Encoding encoding = null; StreamReader streamReader = new StreamReader(fileName); streamReader.BaseStream.Read(data, 0, data.Length);
[StructLayout(LayoutKind.Sequential)] public struct SHFILEINFO { public IntPtr hIcon; public IntPtr iIcon; public uint dwAttributes; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szDisplayName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] public string szTypeName; };
class Win32 { public const uint SHGFI_ICON = 0x100; public const uint SHGFI_LARGEICON = 0x0; // 'Large icon public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
//the handle to the system image list IntPtr hImgSmall;
//the handle to the system image list //IntPtr hImgLarge;
//Create an object of SHFILEINFO shinfo = new SHFILEINFO();
try { //Use this to get the small Icon hImgSmall = Win32.SHGetFileInfo( Path of file, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON ); Icon myIcon =Icon.FromHandle(shinfo.hIcon);
//This return the index on=f system image list int iconIndex = shinfo.iIcon;
//Add the icon to imagelist or where ever you want to use you can imageList.Images.Add(myIcon); } catch (Exception) {
If you have closed your appplication but still it runs in task manager then you can use this method to kill the thread.It happens only when some thread of your application is not closed.
Code:-
public const int SC_CLOSE = 0xF060; public const int WM_SYSCOMMAND = 0x0112;
//Override method when You will close the window protected override void WndProc(ref System.Windows.Forms.Message m) { if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
//close the application MyClose(); // your method that cleans everything up and then runs // System.Environment.Exit(0) which WILL close the threads forcefully if needed
using System; // this is COM component that can be found under the name "Microsoft Shell Controls And Automation" // this must be added to project references
using Shell32;
Code:
static void Main() { //Creating an object of shell to access all the control panel items. Shell shell = new Shell();
Controls:----------- Most controls in .NET derive from the System.Windows.Forms.Control class. This class defines the basic functionality of the controls, which is why many properties and events in the controls we'll see are identical. Many of these classes are themselves base classes for other controls, as is the case with the Label and TextBoxBase classes:--
Some controls, named custom or user controls, derive from another class: System.Windows.Forms.UserControl. This class is itself derived from the Control class and provides the functionality we need to create controls ourselves. We'll cover this class in Chapter 14. Incidentally, controls used for designing Web user interfaces derive from yet another class, System.Web.UI.Control.
Properties
All controls have a number of properties that are used to manipulate the behavior of the control. The base class of most controls, Control, has a number of properties that other controls either inherit directly or override to provide some kind of custom behavior.
The table below shows some of the most common properties of the Control class. These properties will be present in most of the controls we'll visit in this chapter, and they will therefore, not be explained in detail again, unless the behavior of the properties is changed for the control in question. Note that this table is not meant to be exhaustive; if you want to see all f the properties in the class, please refer to the MSDN library:
Name Availability Description
Anchor Read/Write Using this property, you can specify how the control
behaves when its container is resized. See below for a detailed explanation of this property.
BackColor Read/Write The background color of a control.
Bottom Read/Write By setting this property, you specify the distance
from the top of the window to the bottom of the control. This is not the same as specifying the height of the control.
Dock Read/Write Allows you to make a control dock to the edges of a
window. See below for a more detailed explanation of this property.
Enabled Read/Write Setting Enabled to true usually means that the control
can receive input from the user. Setting Enabled to false usually means that it cannot.
ForeColor Read/Write The foreground color of the control.
Height Read/Write The distance from the top to the bottom of the control.
Left Read/Write The left edge of the control relative to the left
edge of the window.
Name Read/Write The name of the control. This name can be used to reference the control in code.
Parent Read/Write The parent of the control.
Right Read/Write The right edge of the control relative to the left
edge of the window.
TabIndex Read/Write The number the control has in the tab order of its container.
TabStop Read/Write Specifies whether the control can be accessed by the Tab key.
Tag Read/Write This value is usually not used by the control itself,
and is there for you to store information about the control on the control itself. When this property is assigned a value through the Windows Form designer, you can only assign a string to it.
Top Read/Write The top edge of the control relative to the top of the window.
Visible Read/Write Specifies whether or not the control is visible at runtime.
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms;
namespace MatrixControl { public partial class M42CheckBoxControl : UserControl { private static int index = 0;
public M42CheckBoxControl() { InitializeComponent(); } # region Properties /// /// These are the properties value contains the text string seperated by the seperator /// private string _value; public string Value { set { _value = value; } } /// /// Seperator is also a string by which we will parse the text string. /// private string _seperator; public string Seperator {
set { _seperator = value; } } /// /// This is default value means which checkbox will be checked by default. /// private string _default; public string Default { set { _default = value; } }
# endregion
#region private
/// /// This event will fire when checkBox will checked or unchecked. /// /// /// private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e) { //Creates an array of strings upto the number of check boxes avaliable in check box list.
string[] checkedItems = new string[M42CheckedListBox.CheckedItems.Count];
foreach (object objchecked in M42CheckedListBox.CheckedItems) { checkedItems[M42CheckedListBox.CheckedItems.IndexOf(objchecked)] = objchecked.ToString(); } //This join method string.Join(_seperator, collectionOfCheckedItems) will give the selected items. this.Description.Text= string.Join(_seperator, checkedItems); }
# endregion
#region public /// /// This check box option is firstly parse all the values and defaults then store iot in check /// list boxes and text. ///
public void CheckBoxOption() { index = 0; //This is to split through seperator string[] parseString = _value.Split(_seperator.ToCharArray()); //thisd is to split the defailt value through seperator string[] defaultString = _default.Split(_seperator.ToCharArray()); //this default description string array is just to store all the default values. string[] defaultDescription = new string[defaultString.Length];
//This for loop is just to find the index of default value and create the check boxes. if (_value != string.Empty) { foreach (string value in parseString) { if (M42CheckedListBox.Items.Contains(value)) { } else { M42CheckedListBox.Items.Add(value); } } foreach (string defaultValue in defaultString) { if (M42CheckedListBox.Items.Contains(defaultValue)) { M42CheckedListBox.SetItemCheckState(M42CheckedListBox.Items.IndexOf(defaultValue), CheckState.Checked); defaultDescription[index++] = (defaultValue); } this.Description.Text = string.Join(_seperator, defaultDescription); } } }
This is an example how to create Dynamic Check Boxes and how to communicate betwwen two user controls on a sigle form using Events and Delegates. In this application I have to send some data from control2 to control1 on a single button click.So for this i have used event and Delegates.
Have A look. //Control2:---- namespace ParserF { public partial class Control2 : UserControl { public ArrayList CheckBoxName = new ArrayList(); public char[] seperator = new char[5]; static int count = 0, indexing=0; public delegate void PassSelectedValues(ArrayList CheckBoxName, int countDefault); public event PassSelectedValues DataSelected; public int Flag = 0;
public Control2() { InitializeComponent(); }
# region Properties /// /// These are the properties value contains the text string seperated by the seperator /// private string _Value; public string Value { get { return _Value; } set { _Value = value; } } /// /// Seperator is also a string by which we will parse the text string. /// private string _Seperator; public string Seperator { get { return _Seperator; } set { _Seperator = value; }
} /// /// This is default value means which checkbox will be checked by default. /// private string _Default; public string Default { get { return _Default; } set { _Default = value; } }
# endregion
# region private
/// /// This function is just when we will click on Ok button then all the data will be sent to control1 /// by delegate public delegate void PassSelectedValues(ArrayList CheckBoxName, int ind); /// and event public event PassSelectedValues DataSelected; In this function i m taking a string Builder /// Temporary String in that we will store the string after parsing through seperator and then store it in a arraylist. /// /// /// /// private void OkButton_Click(object sender, EventArgs e) { StringBuilder TemporaryString = new StringBuilder();
InitializaValues(); //This if codition will check that value (Text String) contains default or not .If yes the continue otherwise will make a messagebox. if (Value.Contains(Default)) { for (int index = 0; index < Seperator.Length; index++) { seperator[index] = Seperator[index]; } //This for loop is just to extract strings upto the length of the value for (; (indexing < Value.Length); indexing++) { if (Value[indexing] != seperator[0]) { TemporaryString.Append(Value[indexing]); } else { if (Flag == 0) { count++; } String str = TemporaryString.ToString(); //This if condition is just to comapare default one and store the index. if (!System.Convert.ToBoolean((str.CompareTo(Default)))) { Flag = count; count = 0; }
//This will add the string to the array list CheckBoxName.Add(str); //This will replace the string Builder Temporary String to Empty One. TemporaryString.Replace(str, String.Empty); } } if ((indexing == Value.Length)&&(Flag==0)) { Flag = (count+1); } String str1 = TemporaryString.ToString(); CheckBoxName.Add(str1); //DataSelected is the event which will fire by this we are sending the arraylist and the index for defaultCheck DataSelected(CheckBoxName, Flag-1); } else { MessageBox.Show("No focus set due to No string matches from TextString"); } } /// /// This is just to Inotialize values to the text boxes. /// private void InitializaValues() { Value = ValueTextBox.Text; Seperator = SeperatorTextBox.Text; Default = DefaultTextBox.Text; }
# endregion } }
//Control1:---
namespace ParserF { public partial class Control1 : UserControl {
public Control1() { InitializeComponent(); } public int flag1; ArrayList Array1 = new ArrayList(); static int index2 = 0;
public ArrayList SelectedCheckBox {
set { Array1 = value; }
} public int indexing {
set { flag1 = value; }
}
public void AddCheckBox() { for (int index = 0; index < Array1.Count; index++) { CheckBox checkBox = new CheckBox(); checkBox.AutoSize = true; checkBox.Location = new Point((28), (27 + index2));
checkBox.Size = new System.Drawing.Size(80, 17); checkBox.Text = Array1[index].ToString(); if (index == flag1) { checkBox.Checked = true; } if (checkBox.Checked) { textBox1.Text = checkBox.Text; } checkBox.UseVisualStyleBackColor = true; index2 += 20; Controls.Add(checkBox); checkBox.CheckedChanged += new EventHandler(checkBox_CheckedChanged);
} } /// /// This is an event which will fire when a user will check the check Box /// /// /// private void checkBox_CheckedChanged(object sender, System.EventArgs e) { if (((CheckBox)sender).Checked == true) { if (textBox1.Text == string.Empty) { textBox1.Text = ((CheckBox)sender).Text; } else { textBox1.AppendText(","); textBox1.AppendText(((CheckBox)sender).Text); } } else if (textBox1.Text == ((CheckBox)sender).Text) { textBox1.Text = string.Empty; } else { int FirstIndex=textBox1.Text.IndexOf(((CheckBox)sender).Text); int length=((CheckBox)sender).Text.Length; string str1=textBox1.Text.Remove(FirstIndex, length); textBox1.Text = str1; } }
This is my personal blog and i write articles on .Net, WPF, C#, OOPS, Threading and other .Net technologies. This is not related to any of my employer and organizations. This is the result of my personal interest.