Pokazywanie postów oznaczonych etykietą c#. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą c#. Pokaż wszystkie posty

poniedziałek, 2 lipca 2012

Walidacja numeru Dowodu Osobistego w c#

Funkcja walidująca numer dowodu osobistego w c# wraz z formatowaniem.
Funkcja oparta o sumę kontrolną

/// <summary>
/// Weryfikacja Nr Dowowdu Osobistego
/// </summary>
/// <param name="szNR">Nr Dowodu</param>
/// <param name="retFormated">Czy zwracać numer w postaci sformatowanej</param>
/// <returns>true/false</returns>
public static bool ValidateDO(ref string szNR, bool retFormated = true)
{
    byte[] tab = new byte[9] { 7, 3, 1, 9, 7, 3, 1, 7, 3 };
    byte[] tablicz = new byte[] { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 };
          
    bool bResult = false;
    int sumcontrol = 0;
    int sum = 0;

    szNR = szNR.Trim().Replace(" ", "");
           
    if (szNR.Length == 9)
    {               
        byte b;
               
        for (int i = 0; i < 3; i++)
        {
            b = Convert.ToByte(szNR[i]);
            if (b < 65 || b > 90) return false;
        }
        for (int i = 3; i < 9; i++)
        {
            b = Convert.ToByte(szNR[i]);
            if (b < 48 || b > 57) return false;
        }
               
        sumcontrol = Convert.ToInt32(szNR[3].ToString());

        for (int i = 0; i < 9; i++)
        {
            if (i < 3)
            {
                sum += (Convert.ToByte(szNR[i]) - 55) * tab[i];
            }
            else
            {
                sum += Convert.ToInt32(szNR[i].ToString()) * tab[i];
            }
        }

        bResult = ((sum % 10) == 0);

    }
    else return false;

    if (bResult && retFormated)
    {
        szNR = szNR.Insert(3, " ");                               
    }

    return bResult;
}

Walidacja numeru konta bankowego w c#

Poniżej przedstawiam funkcję walidującą numer konta bankowego zarówno przyjętego w Polsce 26 i 28 znakowy.
Funkcja może zwrócić podany numer w postaci sformatowanej tj ze spacjami.

/// <summary>
/// Walidacja numeru bankowego
/// </summary>
/// <param name="szNR">weryfikowany numer</param>
/// <param name="retFormated">Czy zwracać numer w postaci sformatowanej</param>
/// <returns>true/false</returns>
public static bool ValidateIBAN_NRB(ref string szNR, bool retFormated = true)
{
    bool bResult = false;

    szNR = szNR.Trim()
                .Replace(" ", "")
                .Replace("-", "");
          
    if (szNR.Length == 26 || szNR.Length == 28)
    {
        string nr = szNR;
        if (nr.Length == 26)
        {
            nr = (nr +"PL"+ nr.Substring(0, 2)).Remove(0, 2);
        }
        else
        {
            nr = (nr + nr.Substring(0, 4)).Remove(0, 4);
        }

        nr = nr.Replace("P", "25").Replace("L", "21");

        String nr6 = nr.Substring(0, 6);
        String nr12 = nr.Substring(6, 6);
        String nr18 = nr.Substring(12, 6);
        String nr24 = nr.Substring(18, 6);
        String nr30 = nr.Substring(24);

        int r = Convert.ToInt32(nr6) % 97;
        nr12 = (r > 0 ? r.ToString() : "") + nr12;
        r = Convert.ToInt32(nr12) % 97;
        nr18 = (r > 0 ? r.ToString() : "") + nr18;
        r = Convert.ToInt32(nr18) % 97;
        nr24 = (r > 0 ? r.ToString() : "") + nr24;
        r = Convert.ToInt32(nr24) % 97;
        nr30 = (r > 0 ? r.ToString() : "") + nr30;               

        bResult = (Convert.ToInt32(nr30) % 97 == 1);
    }
    else return false;

    if (bResult && retFormated)
    {
        if (szNR.Length==26)
        {
            szNR = szNR.Insert(22, " ").Insert(18, " ").Insert(14, " ").Insert(10, " ").Insert(6, " ").Insert(2, " ");
        }
        else
        {
            szNR = szNR.Insert(24, " ").Insert(20, " ").Insert(16, " ").Insert(12, " ").Insert(8, " ").Insert(4, " ");
        }
    }

    return bResult;
}

Walidacja numeru Regon w c#

Funkcja walidująca numer Regon w c# zarówno 9-cio jak i 14 cyfrowy.
Walidacja na podstawie sumy kontrolnej jak oraz długości ciągu cyfr.
Funkcja zwrac true w przypadku poprawnego numeru

public static bool ValidateRegon(ref string szRegon)
{
    byte[] tab8 = new byte[8] { 8, 9, 2, 3, 4, 5, 6, 7 };
    byte[] tab13 = new byte[13] { 2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8 };
    byte[] tablicz = new byte[] { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 };
    bool bResult = false;
    int suma = 0;
    int sumcontrol = 0;

    szRegon = szRegon.Trim();

    if ((szRegon.Length == 9)||(szRegon.Length==14))
    {
        foreach (char l in szRegon)
        {
            byte b = Convert.ToByte(l);
            if (Array.IndexOf(tablicz, Convert.ToByte(l)) == -1) return false;
        }

        sumcontrol = Convert.ToInt32(szRegon[(szRegon.Length==9)?8:13].ToString());

        for (int i = 0; i < ((szRegon.Length==9)?8:13); i++)
        {
            suma += ((szRegon.Length==9)?tab8[i]:tab13[i]) * Convert.ToInt32(szRegon[i].ToString());
        }

        bResult = ( (((suma % 11)!=10)?(suma % 11):0) == sumcontrol);
    }
    else return false;
          
    return bResult;
}

Walidacja numeru NIP w c#

Funkcja walidująca numer NIP w c# wraz z formatowaniem.

/// <summary>
/// Walidacja NIP
/// </summary>
/// <param name="szNIP"></param>
/// <param name="retFormated"> Zwracany NIP jest w postaci sformatowanej</param>
/// <returns>zwraca true/false</returns>


public static bool ValidateNIP(ref string szNIP, bool retFormated = true)
{
    byte[] tab = new byte[9] { 6, 5, 7, 2, 3, 4, 5, 6, 7 };
    byte[] tablicz = new byte[] { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 };
    bool bResult = false;
    int suma = 0;
    int sumcontrol = 0;

    szNIP = szNIP.Replace("-", "");
    szNIP = szNIP.Trim();

    if (szNIP.Length == 10)
    {
        foreach (char l in szNIP)
        {
            byte b = Convert.ToByte(l);
            if (Array.IndexOf(tablicz, Convert.ToByte(l)) == -1) return false;
        }

        sumcontrol = Convert.ToInt32(szNIP[9].ToString());

        for (int i = 0; i < 9; i++)
        {
            suma += tab[i] * Convert.ToInt32(szNIP[i].ToString());
        }

        bResult = ((suma % 11) == sumcontrol);
    }
    else return false;

    if (bResult && retFormated)
    {
        szNIP = szNIP.Insert(8, "-")
                        .Insert(6, "-")
                        .Insert(3, "-");              
    }

    return bResult;
}

Walidacja numeru Pesel w C#

Przedstawiam funkcje walidującą numer PESEL.
Funkcja sprawdza sumę kontrolną numeru, długość oraz logiczność.
Zakres sprawdzanych numerów dat w Peselu od 1800 do 2200 (w przybliżeniu)
Zwraca true - w przypadku poprawnego numeru.

public static bool ValidatePesel(ref string szPesel)
{           
byte[] tab = new byte[10] { 9, 7, 3, 1, 9, 7, 3, 1, 9, 7 };
byte[] tablicz = new byte[] {48,49,50,51,52,53,54,55,56,57};
bool bResult = false;
int suma = 0;
int sumcontrol = 0;
               
szPesel = szPesel.Trim();

if (szPesel.Length == 11)
{
    foreach (char l in szPesel)
    {
        byte b = Convert.ToByte(l);
        if (Array.IndexOf(tablicz,Convert.ToByte(l)) == -1) return false;
    }
              
    sumcontrol = Convert.ToInt32(szPesel[10].ToString());

    for (int i = 0; i < 10; i++)
    {
        suma += tab[i] * Convert.ToInt32(szPesel[i].ToString());
    }

    bResult = ((suma % 10) == sumcontrol); 
               
    if (bResult)
    {
        int rok = 0;
        int mies = 0;
        int dzien = Convert.ToInt32(szPesel[4].ToString()) * 10 + Convert.ToInt32(szPesel[5].ToString());

        if (szPesel[2] == '0' || szPesel[2] == '1')
        {
            rok = 1900;
            mies = Convert.ToInt32(szPesel[2].ToString()) * 10 + Convert.ToInt32(szPesel[3].ToString());
        }
        else if (szPesel[2] == '2' || szPesel[2] == '3')
        {
            rok = 2000;
            mies = (Convert.ToInt32(szPesel[2].ToString()) * 10 + Convert.ToInt32(szPesel[3].ToString()) - 20);
        }
        else if (szPesel[2] == '4' || szPesel[2] == '5')
        {
            rok = 2100;
            mies = (Convert.ToInt32(szPesel[2].ToString()) * 10 + Convert.ToInt32(szPesel[3].ToString()) - 40);
        }
        else if (szPesel[2] == '6' || szPesel[2] == '7')
        {
            rok = 2200;
            mies = (Convert.ToInt32(szPesel[2].ToString()) * 10 + Convert.ToInt32(szPesel[3].ToString())-60);
        }
        else if (szPesel[2] == '8' || szPesel[2] == '9')
        {
            rok = 1800;
            mies = (Convert.ToInt32(szPesel[2].ToString()) * 10 + Convert.ToInt32(szPesel[3].ToString())-80);
        }
        rok += Convert.ToInt32(szPesel[0].ToString()) * 10 + Convert.ToInt32(szPesel[1].ToString());
        String szDate = rok.ToString() + "-" + (mies < 10 ? "0" + mies.ToString() : mies.ToString()) + "-" + (dzien < 10 ? "0" + dzien.ToString() : dzien.ToString());
        DateTime dt;
        bResult = DateTime.TryParse(szDate, out dt);
    }
}
else return false;
           
return bResult;
}

czwartek, 16 lutego 2012

Konwersja IList do DataSet - IList to DataSet

Poniżej funkcja konwertująca Ilist-ę do DataSet-a
Param: IList
Return: DataSet

public static DataSet ConvertToDataSet<T>(IList<T> list)
{
  if (list == null || list.Count <= 0)
  {
   return null;
  }
  DataSet ds = new DataSet();
  DataTable dt = new DataTable(typeof(T).Name);
  DataColumn column;
  DataRow row;

  System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

  foreach (T t in list)
  {
   if (t == null)
   {
     continue;
   }

   row = dt.NewRow();
   for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
   {
    System.Reflection.PropertyInfo pInfo = myPropertyInfo[i];
    string name = pInfo.Name;
    if (dt.Columns[name] == null)
    {
     try
    {
     column = new DataColumn(name, pInfo.PropertyType);
    }
    catch (NotSupportedException)
    {
     column = new DataColumn(name, Nullable.GetUnderlyingType(pInfo.PropertyType));
    }
    catch (Exception)
    {
     column = new DataColumn(name, typeof(object));
    }

    dt.Columns.Add(column);
    }
    row[name] = pInfo.GetValue(t, null);
   }
   dt.Rows.Add(row);
  }
 ds.Tables.Add(dt);
 return ds;
}

czwartek, 28 kwietnia 2011

Richtextbox problem różnych czcionek w c# Net

Problem występuje w sytuacji, gdy zaczytujesz z pliku lub wklejasz ze schowka zaznaczony fragment tekstu.
W richtextbox-ie wyświetlane są różne czcionki o różnych wielkościach znaków, szczególnie przy polskich znaków.
Aby temu zapobiec należy bezpośrednio w designerze dopisać linijkę przed ustawieniem fontów.


this.richTextBox1.LanguageOption = System.Windows.Forms.RichTextBoxLanguageOptions.AutoKeyboard;

i problem znika

piątek, 15 kwietnia 2011

Problem z toolStrip i ImageList w .Net c#

Problem jest następujący. We właściwościach komponentu toolStrip brakuje właściwości ImageList. Jednakże właściwość ta wciąż dostępna jest z poziomu kodu. Wystarczy najlepiej zaraz po inicjacji formy w konstruktorze dodać powiązanie np:
this.toolStrip1.ImageList = imageList1;

Teraz można przy pomocy indeksów ustawiać odpowiednie ikony np:
this.toolStripSplitButton1.ImageIndex = 0; gdzie 0 to index z ImageList

Inny problem jest to, że ustawienie indeksu dla pod menu "ToolStripMenuItem" nie powoduje wyświetlenie ikony. Można zastosować przypisanie do właściwości Image odpowiednią ikonę z ImageList np:
this.ekportToolStripMenuItem.Image = imageList1.Images[0];

poniedziałek, 17 stycznia 2011

Pobranie numeru Pesel z tekstu

Prosta funkcja wybierająca nr Pesel z tekstu (sText) i zwracająca w svPesel numer Pesel. Funkcja zwraca true w przypadku poprawnego znalezienia numeru Pesel.

public bool GetPeselFromText(ref String svPesel, String sText)
        {
            bool bResult = false;
            svPesel = "";
            string sPesel = "";
            if (sText.Length < 11) return false;

            for (int i = 0; i < sText.Length; i++)
            {
                if (Convert.ToInt32(sText[i]) >= 48 && Convert.ToInt32(sText[i]) <= 58)
                {
                    sPesel += sText[i].ToString();
                }
                else
                {
                    if (sPesel.Length == 11)
                    {
                        bResult = true;
                        break;
                    }
                    else
                    {
                        sPesel = "";
                    }
                }
            }
            if (sPesel.Length == 11) bResult = true;
            if (bResult) svPesel = sPesel;

            return bResult;
        }

środa, 23 czerwca 2010

Usunięcie elementu z tablicy Array

 public static Array RemoveAt(Array source, int index)
    {
        if (source == null)
            throw new ArgumentNullException("source");

        if (0 > index || index >= source.Length)
            throw new ArgumentOutOfRangeException("index", index, "index is outside the bounds of source array");

        Array dest = Array.CreateInstance(source.GetType().GetElementType(), source.Length - 1);
        Array.Copy(source, 0, dest, 0, index);
        Array.Copy(source, index + 1, dest, index, source.Length - index - 1);

        return dest;
    }