Archive for Maggio 25th, 2008

MultiBinding

CodeKeep C# Feed Maggio 25th, 2008

Description: WPF MultiBinding with Converter

Link: http://www.codekeep.net/snippets/e1d1225a-a857-452d-9136-27c47980cb5f.aspx

<Rectangle Name="rtColor" Margin="25">

  <Rectangle.Fill>

    <SolidColorBrush>

      <SolidColorBrush.Color>

        <MultiBinding Converter="{StaticResource convertColor}">

          <MultiBinding.Bindings>

            <Binding ElementName="slAlpha" Path="Value" />

            <Binding ElementName="slRed" Path="Value" />

            <Binding ElementName="slGreen" Path="Value" />

            <Binding ElementName="slBlue" Path="Value" />

          </MultiBinding.Bindings>

        </MultiBinding>

      </SolidColorBrush.Color>

    </SolidColorBrush>

  </Rectangle.Fill>

</Rectangle>

<Window.Resources>

  <local:ConvertColor x:Key="convertColor" />

  <ImageBrush x:Key="newtelligencelogo" TileMode="Tile" Viewport="0,0,240,51" ViewportUnits="Absolute" ImageSource= "newtelligencelogo.bmp"/>

</Window.Resources>

public class ConvertColor : IMultiValueConverter

{

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)

    {

        float alpha = (float)(double)(values[0]);

        float red = (float)(double)(values[1]);

        float green = (float)(double)(values[2]);

        float blue = (float)(double)(values[3]);

        return Color.FromScRgb(alpha, red, green, blue);

    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)

    {

        throw new NotSupportedException("Not supported");

    }

}

Ejecutar un programa externo

CodeKeep C# Feed Maggio 25th, 2008

Description: Ejecutar un programa externo

Link: http://www.codekeep.net/snippets/f2f35277-cff6-487a-a3fa-76ab693d9f42.aspx

class Program
  {
    static void Main(string[] args) {

    System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
    myProcess.EnableRaisingEvents = false;
    System.Diagnostics.ProcessStartInfo myStarInfo = new System.Diagnostics.ProcessStartInfo("C:\\Archivos de programa\\Notepad 2rc\\Notepad2.exe", "");
    myProcess.StartInfo = myStarInfo;
    myProcess.Start();

    }

  }

Cambiar la fecha del sistema

CodeKeep C# Feed Maggio 25th, 2008

Description: Cambiar la fecha del sistema

Link: http://www.codekeep.net/snippets/1caf5b81-b062-46ea-974f-e04c87b36c41.aspx

	class Program
	{

        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
        public struct SYSTEMTIME
        {
            public short wYear;
            public short wMonth;
            public short wDayOfWeek;
            public short wDay;
            public short wHour;
            public short wMinute;
            public short wSecond;
            public short wMilliseconds;
        }

        [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool SetLocalTime(ref SYSTEMTIME _time);

        static void Main(string[] args) {

        	SYSTEMTIME _time;

        	_time.wYear = 2000;
            _time.wMonth = 04;
            _time.wDay = 09;

            _time.wDayOfWeek = 1;

            _time.wHour = (short)DateTime.Now.Hour;
            _time.wMinute = (short)DateTime.Now.Minute;
            _time.wSecond = (short)DateTime.Now.Second;
            _time.wMilliseconds = (short)DateTime.Now.Millisecond;

            SetLocalTime(ref _time);

        }

	}

Crop Text remove HTML

CodeKeep C# Feed Maggio 25th, 2008

Description: Classe para realizar um Crop do Texto informado e remover o HTML do texto

Link: http://www.codekeep.net/snippets/e8c5e408-1060-4a06-95f5-82b6c255c8e1.aspx

namespace Crop.Retorna
{
    using System.Text.RegularExpressions;

    public class cropRetorna
    {
        public string CropSentence(string textCrop, int num, string trail)
        {
            int iMax;
            Regex rx = new Regex("<[^>]*>");

            textCrop = rx.Replace(textCrop, "");

            iMax = num - trail.Length;

            if (iMax <= 0)
            {
                return "";
            }
            else if (textCrop.Length <= num)
            {
                return textCrop;
            }
            else
            {
                #region Crop

                string sOut;
                sOut = textCrop.Substring(0, num);

                int iPos;
                iPos = sOut.LastIndexOf(" ");

                return sOut.Substring(0, iPos) + trail;

                #endregion
            }
        }
    }
}

Insert Usando LINQ

CodeKeep C# Feed Maggio 25th, 2008

Description: Insert Usando LINQ

Link: http://www.codekeep.net/snippets/eba23b71-6170-4663-9f8d-440273d8889b.aspx

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
        <SnippetType>SurroundsWith</SnippetType>
        <SnippetType>Refactoring</SnippetType>
      </SnippetTypes>
      <Title>Delete</Title>
      <Shortcut>Linq List</Shortcut>
      <Description>Delete</Description>
      <Author>Paulo Lima</Author>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>NomedaClasse</ID>
          <ToolTip>
          </ToolTip>
          <Default>NomedaClasse</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>NomedoDataContext</ID>
          <ToolTip>
          </ToolTip>
          <Default>NomedoDataContext</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>VarNome</ID>
          <ToolTip>
          </ToolTip>
          <Default>VarNome</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>Var</ID>
          <ToolTip>
          </ToolTip>
          <Default>Var</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>ID</ID>
          <ToolTip>
          </ToolTip>
          <Default>ID</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp"><![CDATA[	 [DataObjectMethod(DataObjectMethodType.Delete, false)]
        public static void Delete(int $ID$)
            {
            using ($NomedoDataContext$ pag = new $NomedoDataContext$())
                {
                var $VarNome$ = from $Var$ in pag.$NomedaClasse$
                              where $Var$.$ID$ == $ID$
                              select $Var$;
                $VarNome$.$NomedaClasse$.DeleteOnSubmit($VarNome$.SingleOrDefault());
                pag.SubmitChanges();
                }
            }]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>