245 lines
8.5 KiB
C#
245 lines
8.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace ADSUtileriasWF.Helpers
|
|
{
|
|
public static class ModExtensiones
|
|
{
|
|
|
|
public static System.Windows.Forms.DialogResult Alert(string mensage, string Titulo, System.Windows.Forms.MessageBoxButtons botones = System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon Icon = System.Windows.Forms.MessageBoxIcon.None)
|
|
{
|
|
System.Windows.Forms.DialogResult result = System.Windows.Forms.DialogResult.None;
|
|
result = System.Windows.Forms.MessageBox.Show(mensage, Titulo, botones, Icon);
|
|
//switch (button)
|
|
//{
|
|
// case MessageBoxButtons.YesNo:
|
|
// using (frmYesNo yesNo = new frmYesNo())
|
|
// {
|
|
// yesNo.Text = caption;
|
|
// yesNo.Message = message;
|
|
// result = yesNo.ShowDialog();
|
|
// }
|
|
// break;
|
|
// case MessageBoxButtons.OK:
|
|
// using (frmOK ok = new frmOK())
|
|
// {
|
|
// ok.Text = caption;
|
|
// ok.Message = message;
|
|
// result = ok.ShowDialog();
|
|
// }
|
|
// break;
|
|
//}
|
|
return result;
|
|
}
|
|
public static void OnError(string Mensaje, string Caption, Exception Exeption, System.Windows.Forms.MessageBoxIcon Icon, string InfoAdicional = "")
|
|
{
|
|
GuardaLog(string.Format("{0} DESCRIPCIÓN DETALLADA: {1} {2} {3}", Mensaje, Exeption.ToString(), "\r\n", InfoAdicional));
|
|
Alert(Mensaje, Caption, System.Windows.Forms.MessageBoxButtons.OK, Icon);
|
|
}
|
|
|
|
public static void GuardaLog(string Str)
|
|
{
|
|
string strLineaTMP;
|
|
// strLineaTMP = Chr(13) & Chr(10) & Day(Now) & "/" & Month(Now) & "/" & Year(Now) & " " & Hour(Now) & ":" & Minuto & ". " & Str & Chr(13) & Chr(10)
|
|
strLineaTMP = string.Format("{0}{1}{2: dd-MM-yyyy HH\\:mm}", "\r\n", Str, DateTime.Now);
|
|
try
|
|
{
|
|
using (System.IO.StreamWriter ArchivoW = new System.IO.StreamWriter(string.Format(@"{0}\Log.txt", AppDomain.CurrentDomain.BaseDirectory), true, System.Text.Encoding.UTF8))
|
|
{
|
|
ArchivoW.WriteLine(strLineaTMP);
|
|
ArchivoW.Close();
|
|
}
|
|
}
|
|
catch(Exception)
|
|
{
|
|
}
|
|
}
|
|
#region "String To Another type"
|
|
public static int ToEntero(this string Obj)
|
|
{
|
|
int result = 0;
|
|
// restriccion menor a 10, se quitan ceros a la izquiera primero, porque provoca desbordamiento si pasa mas de una cantidad,Holds signed 32-bit (4-byte) integers that range in value from -2,147,483,648 through 2,147,483,647.
|
|
Obj = Obj.ToString().Trim().Replace(",", "");
|
|
int.TryParse(Obj, out result);
|
|
return result;
|
|
}
|
|
public static bool ToBool(this string Obj)
|
|
{
|
|
|
|
return Obj == "1" || Obj.ToUpper() == "TRUE";
|
|
}
|
|
public static double ToDouble(this string Str, double TipoCambio = 1)
|
|
{
|
|
double result = 0;
|
|
Str = Str.Trim().Replace(",", "").Replace("$", "").Replace("%", "");
|
|
if (!string.IsNullOrEmpty(Str))
|
|
{
|
|
double.TryParse(Str, out result);
|
|
}
|
|
return result * TipoCambio;
|
|
}
|
|
public static decimal ToDecimal(this string Str, decimal TipoCambio = 1)
|
|
{
|
|
decimal result = 0;
|
|
Str = Str.Trim().Replace(",", "").Replace("$", "").Replace("%", "");
|
|
if (!string.IsNullOrEmpty(Str))
|
|
{
|
|
Decimal.TryParse(Str, out result);
|
|
}
|
|
return result * TipoCambio;
|
|
}
|
|
public static DateTime ToDateTime(this string Str)
|
|
{
|
|
return Str.ToDate();
|
|
}
|
|
public static DateTime ToDate(this string Str, IFormatProvider culture = null)
|
|
{
|
|
DateTime retorno = DateTime.MinValue;
|
|
if (Str != null && !string.IsNullOrEmpty(Str.Trim()))
|
|
{
|
|
if (culture == null)
|
|
{
|
|
DateTime.TryParse(Str, out retorno);
|
|
//retorno = Convert.ToDateTime(Str.Trim());
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
retorno = Convert.ToDateTime(Str.ToString().Trim(), culture);
|
|
|
|
}
|
|
catch (Exception) { }
|
|
|
|
}
|
|
}
|
|
return retorno;
|
|
}
|
|
#endregion
|
|
|
|
static public object GetValObjDy(this object obj, string propertyName)
|
|
{
|
|
return obj.GetType().GetProperty(propertyName).GetValue(obj, null);
|
|
}
|
|
public static string FormatoNumero(this string Str, int Decimales = 2)
|
|
{
|
|
decimal numero = 0;
|
|
decimal.TryParse(Str, out numero);
|
|
StringBuilder zeros = new StringBuilder();
|
|
for (int i = 0; i < Decimales; i++)
|
|
{
|
|
zeros.Append("0");
|
|
}
|
|
if (zeros.Length > 0)
|
|
{
|
|
zeros.Insert(0, ".");
|
|
}
|
|
string Tmp = string.Format("{0:#,0.00}", numero);
|
|
return Tmp;
|
|
}
|
|
|
|
public static bool Contiene(this int Valor, params int[] Valores)
|
|
{
|
|
return Array.IndexOf(Valores, Valor) >= 0;
|
|
}
|
|
public static bool Contiene(this string Valor, params string[] Valores)
|
|
{
|
|
return Array.IndexOf(Valores, Valor) >= 0;
|
|
}
|
|
public static string Mid(string input, int index, char newChar)
|
|
|
|
{
|
|
|
|
if (input == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("input");
|
|
|
|
}
|
|
|
|
char[] chars = input.ToCharArray();
|
|
|
|
chars[index - 1] = newChar;
|
|
|
|
return new string(chars);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// This is equivalent to Mid Function in VB6
|
|
/// </summary>
|
|
/// <param name="s"> String to Check.</param>
|
|
/// <param name="a">Position of Character</param>
|
|
/// <param name="b">Length </param>
|
|
/// <returns></returns> ]
|
|
public static string Mid(string s, int a, int b)
|
|
|
|
{
|
|
|
|
string temp = s.Substring(a - 1, b);
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
public static System.Data.DataTable FiltraParametros(this System.Data.DataTable dt, string RowFilter, string Orderby = "", string[] DistinctColumns = null)
|
|
{
|
|
System.Data.DataTable TmpDt = new System.Data.DataTable();
|
|
if (dt != null && dt.Rows.Count > 0)
|
|
{
|
|
System.Data.DataView Dv = new System.Data.DataView(dt, RowFilter, Orderby, System.Data.DataViewRowState.CurrentRows);
|
|
if (DistinctColumns != null)
|
|
TmpDt = Dv.ToTable(true, DistinctColumns);
|
|
else
|
|
try
|
|
{
|
|
TmpDt = Dv.ToTable();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
TmpDt = null;
|
|
}
|
|
|
|
Dv.Dispose();
|
|
}
|
|
else { TmpDt = dt; }
|
|
return TmpDt;
|
|
}
|
|
|
|
public static System.Data.DataRow FiltroPrimero(this System.Data.DataTable dt, string RowFilter, string Orderby = "")
|
|
{
|
|
System.Data.DataRow TmpDr = null;
|
|
if (dt != null && dt.Rows.Count > 0)
|
|
{
|
|
if (RowFilter != "")
|
|
{
|
|
System.Data.DataView Dv = new System.Data.DataView(dt, RowFilter, Orderby, System.Data.DataViewRowState.CurrentRows);
|
|
if (Dv.ToTable().Rows.Count > 0)
|
|
TmpDr = Dv.ToTable().Rows[0];
|
|
Dv.Dispose();
|
|
}
|
|
else
|
|
TmpDr = dt.Rows[0];
|
|
}
|
|
return TmpDr;
|
|
}
|
|
public static bool HasRows(this System.Data.DataTable dt)
|
|
{
|
|
if (dt != null && dt.Rows.Count > 0)
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
public static double SUM(this System.Data.DataTable dt, string Campo, string Filtro ="")
|
|
{
|
|
|
|
return dt.Compute("SUM(" + Campo + ")", Filtro).ToString().ToDouble();
|
|
}
|
|
|
|
|
|
}
|
|
}
|