public partial class Form1 : Form {
public Form1() {
InitializeComponent();
treeView1.Nodes.Add("abc");
treeView1.Nodes.Add("bcd");
treeView1.Nodes[1].Nodes.Add("ppo");
treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
treeView1.DrawNode += treeView1_DrawNode;
}
private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) {
Font f = e.Node.NodeFont != null ? e.Node.NodeFont : e.Node.TreeView.Font;
Size sz = TextRenderer.MeasureText(e.Node.Text, f);
Rectangle rc = new Rectangle(e.Bounds.X - 1, e.Bounds.Y, sz.Width + 2, e.Bounds.Height);
Color fore = e.Node.ForeColor;
if (fore == Color.Empty) fore = e.Node.TreeView.ForeColor;
// Have to indicate focus somehow, how about yellow foreground text?
if (e.Node == e.Node.TreeView.SelectedNode) {
fore = SystemColors.HighlightText;
if ((e.State & TreeNodeStates.Focused) != 0) fore = Color.Yellow;
}
Color back = e.Node.BackColor;
if (back == Color.Empty) back = e.Node.TreeView.BackColor;
if (e.Node == e.Node.TreeView.SelectedNode) back = SystemColors.Highlight;
SolidBrush bbr = new SolidBrush(back);
e.Graphics.FillRectangle(bbr, rc);
TextRenderer.DrawText(e.Graphics, e.Node.Text, f, rc, fore, TextFormatFlags.GlyphOverhangPadding);
bbr.Dispose();
Tuesday, September 28, 2010
Wednesday, September 22, 2010
File Association
http://www.codeproject.com/KB/files/custom-file-extension.aspx
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using Microsoft.Win32;
namespace QC.QTMApplication
{
internal struct CommandArrayList
{
public ArrayList Captions;
public ArrayList Commands;
}
///Properties of the file association.
internal struct FileType
{
public CommandArrayList Commands;
public string Extension;
public string ProperName;
public string FullName;
public string ContentType;
public string IconPath;
public short IconIndex;
}
///Creates file associations for your programs.
///The following example creates a file association for the type XYZ with a non-existent program.
///
VB.NET code
///
/// Dim FA as New FileAssociation
/// FA.Extension = "xyz"
/// FA.ContentType = "application/myprogram"
/// FA.FullName = "My XYZ Files!"
/// FA.ProperName = "XYZ File"
/// FA.AddCommand("open", "C:\mydir\myprog.exe %1")
/// FA.Create
///
///
C# code
///
/// FileAssociation FA = new FileAssociation();
/// FA.Extension = "xyz";
/// FA.ContentType = "application/myprogram";
/// FA.FullName = "My XYZ Files!";
/// FA.ProperName = "XYZ File";
/// FA.AddCommand("open", "C:\\mydir\\myprog.exe %1");
/// FA.Create();
///
///
public class FileAssociation
{
///Initializes an instance of the FileAssociation class.
public FileAssociation()
{
FileInfo = new FileType();
FileInfo.Commands.Captions = new ArrayList();
FileInfo.Commands.Commands = new ArrayList();
}
///Gets or sets the proper name of the file type.
///A String representing the proper name of the file type.
public string ProperName
{
get
{
return FileInfo.ProperName;
}
set
{
FileInfo.ProperName = value;
}
}
///Gets or sets the full name of the file type.
///A String representing the full name of the file type.
public string FullName
{
get
{
return FileInfo.FullName;
}
set
{
FileInfo.FullName = value;
}
}
///Gets or sets the content type of the file type.
///A String representing the content type of the file type.
public string ContentType
{
get
{
return FileInfo.ContentType;
}
set
{
FileInfo.ContentType = value;
}
}
///Gets or sets the extension of the file type.
///A String representing the extension of the file type.
///If the extension doesn't start with a dot ("."), a dot is automatically added.
public string Extension
{
get
{
return FileInfo.Extension;
}
set
{
if (value.Substring(0, 1) != ".")
value = "." + value;
FileInfo.Extension = value;
}
}
///Gets or sets the index of the icon of the file type.
///A short representing the index of the icon of the file type.
public short IconIndex
{
get
{
return FileInfo.IconIndex;
}
set
{
FileInfo.IconIndex = value;
}
}
///Gets or sets the path of the resource that contains the icon for the file type.
///A String representing the path of the resource that contains the icon for the file type.
///This resource can be an executable or a DLL.
public string IconPath
{
get
{
return FileInfo.IconPath;
}
set
{
FileInfo.IconPath = value;
}
}
///Adds a new command to the command list.
/// The name of the command.
/// The command to execute.
///Caption -or- Command is null (VB.NET: Nothing).
public void AddCommand(string Caption, string Command)
{
if (Caption == null
Command == null)
throw new ArgumentNullException();
FileInfo.Commands.Captions.Add(Caption);
FileInfo.Commands.Commands.Add(Command);
}
///Creates the file association.
///Extension -or- ProperName is null (VB.NET: Nothing).
///Extension -or- ProperName is empty.
///The user does not have registry write access.
public void Create()
{
// remove the extension to avoid incompatibilities [such as DDE links]
try
{
Remove();
}
catch (ArgumentException) { } // the extension doesn't exist
// create the exception
if (Extension == ""
ProperName == "")
throw new ArgumentException();
int cnt;
try
{
RegistryKey RegKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(Extension);
RegKey.SetValue("", ProperName);
if (ContentType != null && ContentType != "")
RegKey.SetValue("Content Type", ContentType);
RegKey.Close();
RegKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(ProperName);
RegKey.SetValue("", FullName);
RegKey.Close();
if (IconPath != "")
{
RegKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(ProperName + "\\" + "DefaultIcon");
RegKey.SetValue("", IconPath + "," + IconIndex.ToString());
RegKey.Close();
}
for (cnt = 0; cnt < FileInfo.Commands.Captions.Count; cnt++)
{
RegKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(ProperName + "\\" + "Shell" + "\\" + (string)FileInfo.Commands.Captions[cnt]);
RegKey = RegKey.CreateSubKey("Command");
RegKey.SetValue("", FileInfo.Commands.Commands[cnt]);
RegKey.Close();
}
}
catch
{
throw new Exception();
}
}
///Removes the file association.
///Extension -or- ProperName is null (VB.NET: Nothing).
///Extension -or- ProperName is empty -or- the specified extension doesn't exist.
///The user does not have registry delete access.
public void Remove()
{
if (Extension == null
ProperName == null)
throw new ArgumentNullException();
if (Extension == ""
ProperName == "")
throw new ArgumentException();
Microsoft.Win32.Registry.ClassesRoot.DeleteSubKeyTree(Extension);
Microsoft.Win32.Registry.ClassesRoot.DeleteSubKeyTree(ProperName);
}
///Holds the properties of the file type.
private FileType FileInfo;
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using Microsoft.Win32;
namespace QC.QTMApplication
{
internal struct CommandArrayList
{
public ArrayList Captions;
public ArrayList Commands;
}
///
internal struct FileType
{
public CommandArrayList Commands;
public string Extension;
public string ProperName;
public string FullName;
public string ContentType;
public string IconPath;
public short IconIndex;
}
///
///
///
VB.NET code
///
/// Dim FA as New FileAssociation
/// FA.Extension = "xyz"
/// FA.ContentType = "application/myprogram"
/// FA.FullName = "My XYZ Files!"
/// FA.ProperName = "XYZ File"
/// FA.AddCommand("open", "C:\mydir\myprog.exe %1")
/// FA.Create
///
///
C# code
///
/// FileAssociation FA = new FileAssociation();
/// FA.Extension = "xyz";
/// FA.ContentType = "application/myprogram";
/// FA.FullName = "My XYZ Files!";
/// FA.ProperName = "XYZ File";
/// FA.AddCommand("open", "C:\\mydir\\myprog.exe %1");
/// FA.Create();
///
///
public class FileAssociation
{
///
public FileAssociation()
{
FileInfo = new FileType();
FileInfo.Commands.Captions = new ArrayList();
FileInfo.Commands.Commands = new ArrayList();
}
///
///
public string ProperName
{
get
{
return FileInfo.ProperName;
}
set
{
FileInfo.ProperName = value;
}
}
///
///
public string FullName
{
get
{
return FileInfo.FullName;
}
set
{
FileInfo.FullName = value;
}
}
///
///
public string ContentType
{
get
{
return FileInfo.ContentType;
}
set
{
FileInfo.ContentType = value;
}
}
///
///
///
public string Extension
{
get
{
return FileInfo.Extension;
}
set
{
if (value.Substring(0, 1) != ".")
value = "." + value;
FileInfo.Extension = value;
}
}
///
///
public short IconIndex
{
get
{
return FileInfo.IconIndex;
}
set
{
FileInfo.IconIndex = value;
}
}
///
///
///
public string IconPath
{
get
{
return FileInfo.IconPath;
}
set
{
FileInfo.IconPath = value;
}
}
///
/// The name of the command.
/// The command to execute.
///
public void AddCommand(string Caption, string Command)
{
if (Caption == null
Command == null)
throw new ArgumentNullException();
FileInfo.Commands.Captions.Add(Caption);
FileInfo.Commands.Commands.Add(Command);
}
///
///
///
///
public void Create()
{
// remove the extension to avoid incompatibilities [such as DDE links]
try
{
Remove();
}
catch (ArgumentException) { } // the extension doesn't exist
// create the exception
if (Extension == ""
ProperName == "")
throw new ArgumentException();
int cnt;
try
{
RegistryKey RegKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(Extension);
RegKey.SetValue("", ProperName);
if (ContentType != null && ContentType != "")
RegKey.SetValue("Content Type", ContentType);
RegKey.Close();
RegKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(ProperName);
RegKey.SetValue("", FullName);
RegKey.Close();
if (IconPath != "")
{
RegKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(ProperName + "\\" + "DefaultIcon");
RegKey.SetValue("", IconPath + "," + IconIndex.ToString());
RegKey.Close();
}
for (cnt = 0; cnt < FileInfo.Commands.Captions.Count; cnt++)
{
RegKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(ProperName + "\\" + "Shell" + "\\" + (string)FileInfo.Commands.Captions[cnt]);
RegKey = RegKey.CreateSubKey("Command");
RegKey.SetValue("", FileInfo.Commands.Commands[cnt]);
RegKey.Close();
}
}
catch
{
throw new Exception();
}
}
///
///
///
///
public void Remove()
{
if (Extension == null
ProperName == null)
throw new ArgumentNullException();
if (Extension == ""
ProperName == "")
throw new ArgumentException();
Microsoft.Win32.Registry.ClassesRoot.DeleteSubKeyTree(Extension);
Microsoft.Win32.Registry.ClassesRoot.DeleteSubKeyTree(ProperName);
}
///
private FileType FileInfo;
}
}
Wednesday, September 15, 2010
C# DataGridView
private void FormatDataGridViewForNDaySchedule(DataGridView dgvTable)
{
if (dgvTable.Rows.Count >= 0)
{
dgvTable.Columns[0].Visible = false;
dgvTable.ReadOnly = false;
foreach (DataGridViewColumn column in dgvNDaysSchedule.Columns)
{
column.ReadOnly = true;
}
dgvTable.Columns[ParameterNames.PRIORITY].ReadOnly = false;
//set width
dgvTable.Columns[ParameterNames.PRIORITY].DefaultCellStyle = 50;
dgvTable.Columns[ParameterNames.REQUEST_STATUS_NAME].Width = 50;
dgvTable.AllowUserToAddRows = false;
//set Header Text
dgvTable.Columns[ParameterNames.NEXT_STOP].HeaderText = "Due Date";
dgvTable.Columns[ParameterNames.CHAMBER].Value = DateTime.Now;
dgvTable.Columns[ParameterNames.CHAMBER].Style.Format = "d";
}
else
{
//do nothing
}
}
{
if (dgvTable.Rows.Count >= 0)
{
dgvTable.Columns[0].Visible = false;
dgvTable.ReadOnly = false;
foreach (DataGridViewColumn column in dgvNDaysSchedule.Columns)
{
column.ReadOnly = true;
}
dgvTable.Columns[ParameterNames.PRIORITY].ReadOnly = false;
//set width
dgvTable.Columns[ParameterNames.PRIORITY].DefaultCellStyle = 50;
dgvTable.Columns[ParameterNames.REQUEST_STATUS_NAME].Width = 50;
dgvTable.AllowUserToAddRows = false;
//set Header Text
dgvTable.Columns[ParameterNames.NEXT_STOP].HeaderText = "Due Date";
dgvTable.Columns[ParameterNames.CHAMBER].Value = DateTime.Now;
dgvTable.Columns[ParameterNames.CHAMBER].Style.Format = "d";
}
else
{
//do nothing
}
}
Tuesday, September 7, 2010
working with File in c#
//To replace and edit a text file
StringBuilder newFile = new StringBuilder();
string temp = "";
string[] file = File.ReadAllLines(@"C:\Documents and Settings\john.grove\Desktop\1.txt");
foreach (string line in file)
{
if (line.Contains("string"))
{
temp = line.Replace("string", "String");
newFile.Append(temp + "\r\n");
continue;
}
newFile.Append(line + "\r\n");
}
File.WriteAllText(@"C:\Documents and Settings\john.grove\Desktop\1.txt", newFile.ToString());
//if file doesn't exist, search for the path
if (!System.IO.File.Exists(testContainer.Name))
{
System.IO.FileInfo fInfo = new System.IO.FileInfo(testContainer.Name);
string dllName = fInfo.Name;
foreach (string path in DLLPaths)
{
//if dll existed in this path, update the qtm container with the new path
if (System.IO.File.Exists(System.IO.Path.Combine(path, dllName)))
{
testContainer.Name = System.IO.Path.Combine(path, dllName);
//stop searching
break;
}
}
}
StringBuilder newFile = new StringBuilder();
string temp = "";
string[] file = File.ReadAllLines(@"C:\Documents and Settings\john.grove\Desktop\1.txt");
foreach (string line in file)
{
if (line.Contains("string"))
{
temp = line.Replace("string", "String");
newFile.Append(temp + "\r\n");
continue;
}
newFile.Append(line + "\r\n");
}
File.WriteAllText(@"C:\Documents and Settings\john.grove\Desktop\1.txt", newFile.ToString());
//if file doesn't exist, search for the path
if (!System.IO.File.Exists(testContainer.Name))
{
System.IO.FileInfo fInfo = new System.IO.FileInfo(testContainer.Name);
string dllName = fInfo.Name;
foreach (string path in DLLPaths)
{
//if dll existed in this path, update the qtm container with the new path
if (System.IO.File.Exists(System.IO.Path.Combine(path, dllName)))
{
testContainer.Name = System.IO.Path.Combine(path, dllName);
//stop searching
break;
}
}
}
deepCopy C#
public static object DeepCopy(object serializableObj)
{
MemoryStream s = new MemoryStream();
BinaryFormatter f = new BinaryFormatter();
f.Serialize(s, serializableObj);
s.Position = 0;
return f.Deserialize(s);
}
{
MemoryStream s = new MemoryStream();
BinaryFormatter f = new BinaryFormatter();
f.Serialize(s, serializableObj);
s.Position = 0;
return f.Deserialize(s);
}
Subscribe to:
Posts (Atom)