segunda-feira, 16 de novembro de 2009

C# Componentes de ajuda

Ajuda com seu Sistema..
Cls COm Componentes que podem te facilitar a vida.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.IO;


namespace xxx
{
class ClsComponentes:clBanco
{
DataSet ds = new DataSet();
public string Codigos;

public ClsComponentes()
{
this.Codigos = string.Empty;
}

public void LimparTxt(Control controles)
{
foreach (Control ctl in controles.Controls)
{
if (ctl is TextBox)
{
ctl.Text = string.Empty;
this.FormatarDecimal(ctl);
}
if (ctl is MaskedTextBox) ctl.Text = string.Empty;
}
}

public void apagarControles(Form form)
{
for (int i = 0; i <= form.Controls.Count - 1; i++) { if (form.Controls[i] is TextBox) { (form.Controls[i] as TextBox).Text = string.Empty; } if (form.Controls[i] is MaskedTextBox) { (form.Controls[i] as MaskedTextBox).Text = string.Empty; } if (form.Controls[i] is ComboBox) { (form.Controls[i] as ComboBox).SelectedIndex = -1; } } } public void PreparaDw(string Csql, DataGridView dtg) { if (Csql != null ) { DataSet ds = new DataSet(); ds = RetornarDataSet(Csql); dtg.DataSource = ds.Tables[0]; dtg.Columns[0].HeaderText = "Código"; dtg.Columns[0].Width = 50; dtg.Columns[1].HeaderText = "Descrição"; dtg.Columns[1].Width = 150; dtg.Columns[2].HeaderText = "Obs."; dtg.Columns[2].Width = 230; } } public void FechaForm(Form sender) { for (double i = 1; i > 0; i -= 0.05)
{
sender.Opacity = i;
sender.Refresh();
}
}

public void FormatarDecimal(Control controles)
{
foreach (Control ctl in controles.Controls)
{
if (ctl is TextBox && ctl.Tag == "Decimal")
{
if (ctl.Text == "0,00" || ctl.Text == string.Empty)
{
ctl.Text = string.Format("{0:####0,00}", "0,00");
}
else
{
ctl.Text = string.Format("{0:####0.00}", Convert.ToDecimal(ctl.Text));
}
}
}
}
public void ValidaDecimal(TextBox txt)
{
if (txt.Text == "")
{
//Decimal dc = Convert.ToDecimal("0");
txt.Text = string.Format("{0:####0.00}","0,00");
}
else
{
txt.Text = string.Format("{0:####0.00}", Convert.ToDecimal(txt.Text));
}
}
public bool ValidaData(Control data)
{
DateTime result;

if (DateTime.TryParse(data.Text,out result))
return true;
else
return false;
}


public bool ValidaCpf(string cpf)
{
try
{
int[] multiplicador1 = new int[9] { 10, 9, 8, 7, 6, 5, 4, 3, 2 };
int[] multiplicador2 = new int[10] { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 };
string tempCpf;
string digito;
int soma;
int resto;

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

if (cpf.Length != 11)
return false;

tempCpf = cpf.Substring(0, 9);
soma = 0;
for (int i = 0; i < 9; i++)
soma += int.Parse(tempCpf[i].ToString()) * multiplicador1[i];

resto = soma % 11;
if (resto < 2)
resto = 0;
else
resto = 11 - resto;

digito = resto.ToString();

tempCpf = tempCpf + digito;

soma = 0;
for (int i = 0; i < 10; i++)
soma += int.Parse(tempCpf[i].ToString()) * multiplicador2[i];

resto = soma % 11;
if (resto < 2)
resto = 0;
else
resto = 11 - resto;

digito = digito + resto.ToString();

return cpf.EndsWith(digito);
}
catch
{
return false;
}
}

public bool ValidaCnpj(string cnpj)
{
try
{
int[] multiplicador1 = new int[12] { 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };
int[] multiplicador2 = new int[13] { 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };
int soma;
int resto;
string digito;
string tempCnpj;

cnpj = cnpj.Trim();
cnpj = cnpj.Replace(".", "").Replace("-", "").Replace("/", "");

if (cnpj.Length != 14)
return false;

tempCnpj = cnpj.Substring(0, 12);

soma = 0;
for (int i = 0; i < 12; i++)
soma += int.Parse(tempCnpj[i].ToString()) * multiplicador1[i];

resto = (soma % 11);
if (resto < 2)
resto = 0;
else
resto = 11 - resto;

digito = resto.ToString();

tempCnpj = tempCnpj + digito;
soma = 0;
for (int i = 0; i < 13; i++)
soma += int.Parse(tempCnpj[i].ToString()) * multiplicador2[i];

resto = (soma % 11);
if (resto < 2)
resto = 0;
else
resto = 11 - resto;

digito = digito + resto.ToString();

return cnpj.EndsWith(digito);
}
catch
{
return false;
}
}

public string FormatarData(DateTime Data)
{
return Data.ToString("yyyy/MM/dd");
}

public string FormatarData2(DateTime Data)
{
return Data.ToString("yyyy/MM/dd 00:00:000");
}

public void Ajuda(Form form)
{
string strReportPath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
strReportPath = strReportPath.Replace("\\Pasta", "") + "\\NOme do arquivo.pdf";
Help.ShowHelp(form, strReportPath);
}
}
}