Pages

Adjust Width CheckBoxList using C#.net

 private void checkBoxlistWidht(CheckedListBox chk)
        {
            System.Drawing.Graphics g = chk.CreateGraphics();
            float maxwidth = 0f;
            foreach (object o in chk.Items)
            {
                float w = g.MeasureString(o.ToString(), chk.Font).Width;
                if (w > maxwidth)
                    maxwidth = w;
            }
            g.Dispose();
            chk.Width = (int)maxwidth + 30;
        }

Adjust width of DateTimepicker

Today , we will discuss about How to adjust width of DateTimepicker in C#.Net.
Now, let’s start:
Create Project in VS 2008 .
Open your form
Drag and Drop DateTimePicker from Toolbar into Form
Go To Code Section .
Copy and Paste the following code in the Code section .

private void dateTimePickerwidth(DateTimePicker dtp,string format)
        {
         System.Drawing.Graphics g = dtp.CreateGraphics();
            float maxwidth = 0f;
            float w = g.MeasureString(Convert.ToString(dtp), dtp.Font).Width;
               if (w < maxwidth)
                    maxwidth = w;
             g.Dispose();
            switch (format)
            {
                case "Long":
                    dtp.Width = (int)maxwidth + 220;
                    break;
                case "Short":
                    dtp.Width = (int)maxwidth + 100;
                    break;
                case "Time":
                    dtp.Width = (int)maxwidth + 100;
                    break;
                case "Custom":
                    dtp.Width = (int)maxwidth + 105;
                    break;
         }
       }

private void Form1_Load(object sender, EventArgs e)
        {
            dateTimePickerwidth(dateTimePicker1, dateTimePicker1.Format.ToString());
        }



Run the Application and See the Result.
Happy Coding!


Get week number of the Day using C#

int noofweek = DateTime.Today.DayOfYear;
            noofweek = noofweek/7;
            MessageBox.Show(noofweek.ToString());

Display Accounting Year of India

Indian Financial Year using C#.Net .
Indian Financial Year Starts with 01/04/yyyy To 31/03//yyyy.


private static string CurrentDate;
       
        public  static string SplitDay(DateTime dtp,string Mode)
        {
            string str = Convert.ToString(dtp);
            String.Format("{0:MM/dd/yyyy}", dtp);
            string[] st = str.Split('/');
            switch (Mode)
            {
                case "Day":
                    CurrentDate = st[1];
                    break;
                case "Month":
                    CurrentDate = st[0];
                    break;
                case "Year":
                    CurrentDate = st[2];
                    string[] s = CurrentDate.Split(' ');
                    CurrentDate = s[0];
                    break;
            }
            return CurrentDate;
        }

private void lstAccountYear(int statringYear)
        {
            string Day = ClsFinancialYear.SplitDay(DateTime.Now, "Day");
            string Month = ClsFinancialYear.SplitDay(DateTime.Now, "Month");
            string Year = ClsFinancialYear.SplitDay(DateTime.Now, "Year");
            int NextYear = Convert.ToInt32(Year) + 1;
            int LastYear = Convert.ToInt32(Year) - 1;
            int CurrentYear = Convert.ToInt32(Year);
                    if (Convert.ToInt32(Month) <= 3 && Convert.ToInt32(Year) == LastYear + 1)
                    {
                        for (int stYear = statringYear; stYear < CurrentYear; stYear++)
                        {
                            int next = stYear + 1;
                            listBox1.Items.Add(stYear + "-" + next);
                            listBox1.SelectedIndex = listBox1.Items.Count - 1;
                        }
                    }
                    else
                    {
                        for (int stYear = statringYear; stYear < NextYear ; stYear++)
                        {
                            int next = stYear + 1;
                            listBox1.Items.Add(stYear + "-" + next);
                            listBox1.SelectedIndex = listBox1.Items.Count - 1;
                        }
                    }
                }

Call this function in Form_load (where you want)

lstAccountYear(2010);
CurrentYear :2013
if Current date:01/04/2013
Result  :
2010-2011
2011-2012
2012-2013
2013-2014
else Current date:31/03/2013
2010-2011
2011-2012
2012-2013

Split date from DateTime in C#.Net

private static string CurrentDate;
       
        public  static string SplitDay(DateTime dtp,string Mode)
        {
            string str = Convert.ToString(dtp);
            String.Format("{0:MM/dd/yyyy}", dtp);
            string[] st = str.Split('/');
            switch (Mode)
            {
                case "Day":
                    CurrentDate = st[1];
                    break;
                case "Month":
                    CurrentDate = st[0];
                    break;
                case "Year":
                    CurrentDate = st[2];
                    string[] s = CurrentDate.Split(' ');
                    CurrentDate = s[0];
                    break;
            }
            return CurrentDate;
        }

Compare Two ArrayList with DateTime

 private static ArrayList list1 = new ArrayList(new DateTime[] { Convert.ToDateTime("2010-12-01"), Convert.ToDateTime("2010-12-11"), Convert.ToDateTime("2010-12-21") });
        private static  ArrayList list2 = new ArrayList(new DateTime[] { Convert.ToDateTime("2010-12-01"), Convert.ToDateTime("2010-12-15") });
        private static int CountofList1 = list1.Count;
        private static int CountofList2 = list2.Count;
        private static void CompareDatewithArrayList(string Comp)
        {
            switch (Comp)
            {
                case "1":            
                    if (CountofList1 > CountofList2)
                    {
                        foreach (DateTime str in list1)
                        {
                            if (!list2.Contains(str))
                            {
                                MessageBox.Show(Convert.ToString(str));
                            }
                        }
                    }
                    else if (CountofList1 == CountofList2)
                    {
                        foreach (DateTime str in list1)
                        {
                            if (!list2.Contains(str))
                            {
                                MessageBox.Show(Convert.ToString(str));
                            }
                        }
                    }
                    else
                    {
                        foreach (DateTime str in list1)
                        {
                            if (!list2.Contains(str))
                            {
                                MessageBox.Show(Convert.ToString(str));
                            }
                        }
                    }
                    break;
                case "2":
                    if (CountofList1 > CountofList2)
                    {
                        foreach (DateTime str in list2)
                        {
                            if (!list1.Contains(str))
                            {
                                MessageBox.Show(Convert.ToString(str));
                            }
                        }
                    }
                    else if (CountofList1 == CountofList2)
                    {
                        foreach (DateTime str in list2)
                        {
                            if (!list1.Contains(str))
                            {
                                MessageBox.Show(Convert.ToString(str));
                            }
                        }
                    }
                    else
                    {
                        foreach (DateTime str in list2)
                        {
                            if (!list1.Contains(str))
                            {
                                MessageBox.Show(Convert.ToString(str));
                            }
                        }
                    }
                    break;
            }
                    }

Pass TextBox Values into another form ComboBox in C#.net

Today ,we will discuss about Pass One control Value to another Form in C#.net. · Create a Project in VS 2008. · Create Two Form .Form 1 and Form2. · Create TextBoxes and One Button on Form1. · Rename name the TextBox .Start with txt. · Create ComboBox on Form2. Now , we will go to Our scenario. Copy and Paste the following codes in Form 1 Code View Section
public static string[] textvalues; List values = new List(); private void button1_Click(object sender, EventArgs e) { foreach (Control con in this.Controls) { if (con.Name.StartsWith("txt")) { values.Add(con.Text); } } textvalues = values.ToArray(); Form2 frm = new Form2(); frm.Show(); }
Copy and Paste the following codes in Form 2 Code View Section
private void Form2_Load(object sender, EventArgs e) { comboBox1.Items.Clear(); foreach (string str in Form1.textvalues) { comboBox1.Items.Add(str); } }
Just Run the Application and Enter the values and click Button1 .

Count of Controls in Form using C#.Net

                Today , we will discuss about the Count of Controls in Form.The following are code for getting no of controls in Form.

Method for getting no of controls in Form:
  public IEnumerable<Control> GetAll(Control control, Type type)
        {
            var controls = control.Controls.Cast<Control>();
            return controls.SelectMany(ctrl => GetAll(ctrl, type))
                                      .Concat(controls)
                                      .Where(c => c.GetType() == type);
        }

 call Method in the Form Loading event(where ever you want)
var c = GetAll(this, typeof(Button));
MessageBox.Show("Total Controls: " + c.Count());

Result:
Total Controls: (no of Buttons in the form)

Enable USB Port using C#.Net

Today ,We will discuss about Enable and Disable USB Port using C Sharp .Net. First ,Create a Application for Enable and Disable USB Port using C Sharp .Net. In our Example: Two Buttons in the form. One is Enable. Another one is disable. Now, we are going to our Coding Section:
using System.Security.Principal;
public static bool IsUserAnAdministrator;
private void btn_Enable_Click(object sender, EventArgs e)
{ Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 3, Microsoft.Win32.RegistryValueKind.DWord);
}
private void btn_Disable_Click(object sender, EventArgs e)
{ Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 4, Microsoft.Win32.RegistryValueKind.DWord);
}
private void FrmEnabledisableUSBPort_Load(object sender, EventArgs e)
{
if (IsAnAdministrator())
{
btn_Disable.Enabled = true;
btn_Enable.Enabled = true;
}
else
{
btn_Disable.Enabled = false;
btn_Enable.Enabled = false;
}
}
To check User is Admin or Not.
bool IsAnAdministrator()
{
WindowsIdentity MyIdentity = WindowsIdentity.GetCurrent();
WindowsPrincipal MyPrincipal = new WindowsPrincipal(MyIdentity);
return MyPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
conclusion: Thanks for reading this Article.

Status of Service installed using C#

use this method for retrieving Service Installed or Not:
private static bool IsSerInstalled(string serviceName)
{
// get list of Windows services
System.ServiceProcess.ServiceController[] ser = ServiceController.GetServices();
// try to find service name
foreach (ServiceController service in ser)
{
if (service.ServiceName == serviceName)
return true;
}
return false;
}
Call this function/Method where do you want?
if (IsSerInstalled("AdobeFlashPlayerUpdateSvc"))
{
MessageBox.Show("Yes");
}
else
{
MessageBox.Show("No");
}
Result: "yes"