Archive

Posts Tagged ‘C#’

How to find current active control in a Form

October 27th, 2009 wiley No comments

If you are working within a form you can use this to find it:


if (this.ActiveControl.Equals(testControl)

{

//......

}

Unique visitors to post: 6

Categories: C#, utils Tags: , ,

dot net panel component

July 15th, 2009 wiley No comments

Formed dot net panel component, with several additional options.
The component code is C#.
Additional options are:
1. Rounded corners of the panel.
2. Add frame and manipulation of it color
3. Transparency

Here is the example:

Maxtrade panel

Maxtrade panel

The name of component is: MaxtradePanel
You can download component from here:

http://blog.avalonbg.com/wp-content/plugins/downloads-manager/img/icons/winzip.gif download: MaxtradePanel v.1.0.0.0 (71.87KB)
added: 14/07/2009
clicks: 96
description: MaxtradePanel v.1.0.0.0

The component is freeware.


Unique visitors to post: 2

Categories: Dot net components Tags: , ,

How to obtain current directory

June 5th, 2009 wiley No comments

C# how to obtain current direntory of the executing process:

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
[/c-sharp]


Unique visitors to post: 0

Categories: C#, utils Tags: ,

Change cell formatting on DataGridView

June 4th, 2009 wiley No comments

How to change the visual presentation of the scope of DataGridView. If in a particular field of the table in the database have such a unix timestamp value like visualizing it as a standard date and time. Add EventHandler CellFormating for the DataGridView.

private void dgvActiveUsers_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
  try
  {
    if (dgvActiveUsers.Columns[e.ColumnIndex].Name.Equals("logindate"))
    {
      DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
      e.Value = origin.AddSeconds(double.Parse(e.Value.ToString())).ToShortDateString() + " - " + origin.AddSeconds(double.Parse(e.Value.ToString())).ToShortTimeString();
     }
   }
   catch (Exception)
   {
   }
}

This method can give an idea and many other types of formatting values.


Unique visitors to post: 6

Categories: C# Tags: , ,

Programaticaly opening folder

June 3rd, 2009 wiley No comments

A method to programaticaly opening folder with explorer:

string folderToOpen = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\archive";
string windir = Environment.GetEnvironmentVariable("SystemRoot");
System.Diagnostics.Process proccess = new System.Diagnostics.Process();
proccess.StartInfo.FileName = windir + @"\explorer.exe";
proccess.StartInfo.Arguments = folderToOpen;
proccess.Start();
[/c-sharp]

String variable folderToOpen contains path to “archive” folder under this folder in which the program works.
proccess.StartInfo.FileName contains path to “windows explorep”.


Unique visitors to post: 0

Categories: C#, utils Tags: , ,

Create ZIP file

May 29th, 2009 wiley No comments

A class to create ZIP file from another file.
This class uses the free C# ZIP library (http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx) is great for reading and creating ZIP files.
The parameter source is full path to source file.
The parameter destination is full path to destination result zip file.

using System;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;

namespace Tools
{
  public class Zip
  {
   public static void create(string source, string destination, string comment)
    {
      Crc32 crc = new Crc32();
      ZipOutputStream s = new ZipOutputStream(File.Create(@destination));
      s.SetLevel(9); // 0 - store only to 9 - means best compression
      FileStream fs = File.OpenRead(@source);
      byte[] buffer = new byte[fs.Length];
      fs.Read(buffer, 0, buffer.Length);
      ZipEntry entry = new ZipEntry(ZipEntry.CleanName(@source));
      entry.DateTime = DateTime.Now;
      entry.Comment = comment;
      entry.ZipFileIndex = 1;
      entry.Size = fs.Length;
      fs.Close();
      crc.Reset();
      crc.Update(buffer);
      entry.Crc = crc.Value;
      s.PutNextEntry(entry);
      s.Write(buffer, 0, buffer.Length);
      s.Finish();
      s.Close();
    }
  }
}

Unique visitors to post: 4

Categories: C#, utils Tags: ,

Execute batch file

May 29th, 2009 wiley No comments

A function to executing the batch file from C#
The value of the first argument [batchFileName] is full path to batch file.
The value of the second argument [argumentsToBatchFile] is array of argument if exist, sending to batch file.

protected bool ExecuteBatchFile(string batchFileName, string[] argumentsToBatchFile)
{
string argumentsString = string.Empty;
try
{
if (argumentsToBatchFile != null)
{
for (int count = 0; count < argumentsToBatchFile.Length; count++)
{
argumentsString += " ";
argumentsString += argumentsToBatchFile[count];
}
}

System.Diagnostics.ProcessStartInfo DBProcessStartInfo = new  System.Diagnostics.ProcessStartInfo(batchFileName, argumentsString);

DBProcessStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
DBProcessStartInfo.UseShellExecute = true;

System.Diagnostics.Process dbProcess;
dbProcess = System.Diagnostics.Process.Start(DBProcessStartInfo);

while (!dbProcess.HasExited)
dbProcess.WaitForExit(2000);

return true;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}

Unique visitors to post: 2

Categories: C#, utils Tags: , , ,

C# – get ip address and host name of a machine

May 26th, 2009 wiley 1 comment

How to find IP address and Host name of computer running a program of C #.
Get Host name:

String strHostName ="";
// Get the host name of local machine.
strHostName = Dns.GetHostName();

Get IP addresses:

IPHostEntry ipEntry = DNS.GetHostByName(strHostName);
IPAddress [] addr = ipEntry.AddressList;
for (int i = 0; i > addr.Length; i++)
{
   MessageBox.show("IP Address " + i.ToString() + ": " + addr[i].ToString());
}

Unique visitors to post: 222

Categories: C#, utils Tags: , ,

C# construction switch

May 25th, 2009 wiley No comments

A simple example of the structure switch, it still can not remember it:

switch (caseSwitch)
{
    case 1:
        makeEver1();
        break;
    case 2:
        makeEver2();
        break;
    default:
        makeEverDefault();
        break;
}

Unique visitors to post: 0

Categories: C# Tags: ,

Load DataSet from xml file.

May 20th, 2009 wiley No comments

Load DataSet from xml file:

The file is called users.xml. The example structure of xml file is:

<users>
  <user>
    <name>My Name</name>
  </user>
</users>

Code to load xml file into DataSet:

DataSet ds = new DataSet("user");
...
string filePath = "users.xml";
ds.ReadXml(filePath);

The value of the filePath is the full path to the file users.xml.


Unique visitors to post: 33

Categories: C#, XML Tags: , ,
Google Analytics integration offered by Wordpress Google Analytics Plugin