elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Como proteger una cartera - billetera de Bitcoin


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  Librería de Snippets para VB.NET !! (Compartan aquí sus snippets)
0 Usuarios y 2 Visitantes están viendo este tema.
Páginas: 1 ... 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [25] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 ... 58 Ir Abajo Respuesta Imprimir
Autor Tema: Librería de Snippets para VB.NET !! (Compartan aquí sus snippets)  (Leído 480,112 veces)
Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.788



Ver Perfil
Re: Librería de Snippets !! (Posteen aquí sus snippets)
« Respuesta #240 en: 4 Julio 2013, 11:24 am »

Devolvuelve la Key equivalente de un Value de un dictionary:

Código
  1.    Public Function FindKeyByValue(Of TKey, TValue)(dictionary As Dictionary(Of TKey, TValue), value As TValue) As TKey
  2.  
  3.        For Each pair As KeyValuePair(Of TKey, TValue) In dictionary
  4.            If value.Equals(pair.Value) Then Return pair.Key
  5.        Next
  6.  
  7.        ' Throw New Exception("The value is not found in the dictionary.")
  8.        Return Nothing
  9.    End Function


En línea

Novlucker
Ninja y
Colaborador
***
Desconectado Desconectado

Mensajes: 10.683

Yo que tu lo pienso dos veces


Ver Perfil
Re: Librería de Snippets !! (Posteen aquí sus snippets)
« Respuesta #241 en: 4 Julio 2013, 16:39 pm »

Algo como esto en C#, aunque como digo, me resulta tan corto que no me gusta ponerlo en funciones/métodos :xD

Código
  1. public K FindKeyByValue<K, V>(Dictionary<K, V> dictionary, V value)
  2. {
  3.    return dictionary.FirstOrDefault(k => k.Value.Equals(value)).Key;
  4. }

Saludos


En línea

Contribuye con la limpieza del foro, reporta los "casos perdidos" a un MOD XD
"Hay dos cosas infinitas: el Universo y la estupidez  humana. Y de la primera no estoy muy seguro."
Albert Einstein
z3nth10n


Desconectado Desconectado

Mensajes: 1.583


"Jack of all trades, master of none." - Zenthion


Ver Perfil WWW
Re: Librería de Snippets !! (Posteen aquí sus snippets)
« Respuesta #242 en: 4 Julio 2013, 23:10 pm »

Perdón por desvirtuar,

http://foro.elhacker.net/net/libreria_de_snippets_posteen_aqui_sus_snippets-t378770.0.html;msg1857514#msg1857514

Siempre me salta la Excepción de Could not set keyboard hook

Que puedo hacer? :S
« Última modificación: 4 Julio 2013, 23:26 pm por Ikillnukes » En línea


Interesados hablad por Discord.
Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.788



Ver Perfil
Re: Librería de Snippets !! (Posteen aquí sus snippets)
« Respuesta #243 en: 5 Julio 2013, 05:06 am »

Algo como esto en C#

Muy bueno Nov!, gracias, la verdad es que necesitaba simplificar esa función y eres el único de todo stackoverflow que ha llegado a conseguirlo xD.

Lo mismo pero en VB:

Código
  1.    Public Function Find_Dictionary_Key_By_Value(Of K, V)(Dictionary As Dictionary(Of K, V), Value As V) As K
  2.  
  3.        Dim Key = Dictionary.FirstOrDefault(Function(x) x.Value.Equals(Value)).Key
  4.  
  5.        If Key Is Nothing Then
  6.            Throw New Exception("The value is not found in the dictionary.")
  7.        End If
  8.  
  9.        Return Key
  10.  
  11.    End Function





http://foro.elhacker.net/net/libreria_de_snippets_posteen_aqui_sus_snippets-t378770.0.html;msg1857514#msg1857514

Siempre me salta la Excepción de Could not set keyboard hook

Que puedo hacer? :S

Se me olvidó mencionar este detalle:

Citar
Project -> Properties -> Debug -> Uncheck “Enable the Visual Studio hosting process”

Saludos!
« Última modificación: 5 Julio 2013, 05:23 am por EleKtro H@cker » En línea

Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.788



Ver Perfil
Re: Librería de Snippets !! (Posteen aquí sus snippets)
« Respuesta #244 en: 5 Julio 2013, 05:31 am »

Modifica el color de un Bitmap

Código
  1. #Region " Fill Bitmap Color "
  2.  
  3.    ' [ Fill Bitmap Color Function ]
  4.    '
  5.    ' Examples :
  6.    '
  7.    ' IMPORTANT: use ARGB colors as the parameter.
  8.    ' PictureBox1.BackgroundImage = Fill_Bitmap_Color(bmp, Color.FromArgb(255, 255, 255, 255), Color.Red)
  9.  
  10.    Private Function Fill_Bitmap_Color(ByVal Image As Bitmap, ByVal FromColor As Color, ByVal ToColor As Color)
  11.  
  12.        Dim bmp As New Bitmap(Image)
  13.  
  14.        Dim x As Integer = 0, y As Integer = 0
  15.  
  16.        While x < bmp.Width
  17.            y = 0
  18.            While y < bmp.Height
  19.                If Image.GetPixel(x, y) = FromColor Then bmp.SetPixel(x, y, ToColor)
  20.                Math.Max(Threading.Interlocked.Increment(y), y - 1)
  21.            End While
  22.            Math.Max(Threading.Interlocked.Increment(x), x - 1)
  23.        End While
  24.  
  25.        Return bmp
  26.  
  27.    End Function
  28.  
  29. #End Region





Mueve el slider de un "GTrackBar" de forma progresiva al mantener presionada una tecla de dirección.

Se necesita el control extendido GTrackBar: http://www.codeproject.com/Articles/35104/gTrackBar-A-Custom-TrackBar-UserControl-VB-NET

Código
  1. ' By Elektro H@cker
  2. #Region " [GTrackBar] Progressive Scroll "
  3.  
  4.    Dim TrackBar_SmallChange As Int32 = 5
  5.    Dim TrackBar_LargeChange As Int32 = 10
  6.  
  7.    ' GTrackBar [KeyDown]
  8.    Private Sub GTrackBar_KeyDown(sender As Object, e As KeyEventArgs) Handles GTrackBar1.KeyDown
  9.  
  10.        sender.ChangeSmall = 0
  11.        sender.ChangeLarge = 0
  12.  
  13.        Select Case e.KeyCode
  14.            Case Keys.Left, Keys.Right, Keys.Up, Keys.Down
  15.                MakeScroll_TrackBar(sender, e.KeyCode)
  16.        End Select
  17.  
  18.    End Sub
  19.  
  20.    ' GTrackBar [KeyUp]
  21.    Private Sub GTrackBar_KeyUp(sender As Object, e As KeyEventArgs) Handles GTrackBar1.KeyUp
  22.        ' Set the values on KeyUp event because the Trackbar Scroll event.
  23.        sender.ChangeSmall = TrackBar_SmallChange
  24.        sender.ChangeLarge = TrackBar_LargeChange
  25.    End Sub
  26.  
  27.    ' MakeScroll TrackBar
  28.    Private Sub MakeScroll_TrackBar(ByVal GTrackBar As gTrackBar.gTrackBar, key As Keys)
  29.  
  30.        Select Case key
  31.            Case Keys.Left
  32.                GTrackBar.Value -= TrackBar_SmallChange
  33.            Case Keys.Right
  34.                GTrackBar.Value += TrackBar_SmallChange
  35.            Case Keys.Up
  36.                GTrackBar.Value += TrackBar_LargeChange
  37.            Case Keys.Down
  38.                GTrackBar.Value -= TrackBar_LargeChange
  39.        End Select
  40.  
  41.    End Sub
  42.  
  43. #End Region

...Lo mismo pero si tenemos múltiples GTrackbars:

Código
  1. ' By Elektro H@cker
  2. #Region " [GTrackBar] Progressive Scroll MultiTrackbars "
  3.  
  4.    Dim TrackBar1_SmallChange As Int32 = 2
  5.    Dim TrackBar1_LargeChange As Int32 = 5
  6.  
  7.    Dim TrackBar2_SmallChange As Int32 = 5
  8.    Dim TrackBar2_LargeChange As Int32 = 10
  9.  
  10.    ' GTrackBar [KeyDown]
  11.    Private Sub GTrackBars_KeyDown(sender As Object, e As KeyEventArgs) Handles GTrackBar1.KeyDown, GTrackBar2.KeyDown
  12.  
  13.        sender.ChangeSmall = 0
  14.        sender.ChangeLarge = 0
  15.  
  16.        Select Case e.KeyCode
  17.            Case Keys.Left, Keys.Right, Keys.Up, Keys.Down
  18.                MakeScroll_TrackBar(sender, e.KeyCode)
  19.        End Select
  20.  
  21.    End Sub
  22.  
  23.    ' GTrackBar [KeyUp]
  24.    Private Sub GTrackBars_KeyUp(sender As Object, e As KeyEventArgs) Handles GTrackBar1.KeyUp, GTrackBar2.KeyUp
  25.  
  26.        ' Set the values on KeyUp event because the Trackbar Scroll event.
  27.  
  28.        Select Case sender.Name
  29.            Case "GTrackBar1"
  30.                sender.ChangeSmall = TrackBar1_SmallChange
  31.                sender.ChangeLarge = TrackBar1_LargeChange
  32.            Case "GTrackBar_2"
  33.                sender.ChangeSmall = TrackBar2_SmallChange
  34.                sender.ChangeLarge = TrackBar2_LargeChange
  35.        End Select
  36.  
  37.    End Sub
  38.  
  39.    ' MakeScroll TrackBar
  40.    Private Sub MakeScroll_TrackBar(ByVal GTrackBar As gTrackBar.gTrackBar, key As Keys)
  41.  
  42.        Dim SmallChange As Int32 = 0, Largechange As Int32 = 0
  43.  
  44.        Select Case GTrackBar.Name
  45.            Case "GTrackBar1"
  46.                SmallChange = TrackBar1_SmallChange
  47.                Largechange = TrackBar1_LargeChange
  48.            Case "GTrackBar2"
  49.                SmallChange = TrackBar2_SmallChange
  50.                Largechange = TrackBar2_LargeChange
  51.        End Select
  52.  
  53.        Select Case key
  54.            Case Keys.Left
  55.                GTrackBar.Value -= SmallChange
  56.            Case Keys.Right
  57.                GTrackBar.Value += SmallChange
  58.            Case Keys.Up
  59.                GTrackBar.Value += Largechange
  60.            Case Keys.Down
  61.                GTrackBar.Value -= Largechange
  62.        End Select
  63.  
  64.    End Sub
  65.  
  66. #End Region
En línea

Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.788



Ver Perfil
Re: Librería de Snippets !! (Posteen aquí sus snippets)
« Respuesta #245 en: 5 Julio 2013, 07:10 am »

[ComboBoxTooltip] Show tooltip when text exceeds ComboBox width

(Muestra un tooltip cuando el tamaño del Item supera el tamaño del ComboBox.)



Código
  1.    Dim LastSelectedItem As Int32 = -1
  2.  
  3.    Private Sub ComboBoxTooltip_DropdownItemSelected(sender As Object, e As ComboBoxTooltip.DropdownItemSelectedEventArgs) _
  4.    Handles ComboBoxTooltip1.DropdownItemSelected
  5.  
  6.        Dim SelectedItem As Int32 = e.SelectedItem
  7.  
  8.        If SelectedItem <> LastSelectedItem Then
  9.            ToolTip1.Hide(sender)
  10.            LastSelectedItem = -1
  11.        End If
  12.  
  13.        If SelectedItem < 0 OrElse e.Scrolled Then
  14.            ToolTip1.Hide(sender)
  15.            LastSelectedItem = -1
  16.        Else
  17.            If sender.Items(e.SelectedItem).Length > CInt(sender.CreateGraphics.MeasureString(0, sender.Font).Width) + 8 Then
  18.                LastSelectedItem = SelectedItem
  19.                ToolTip1.Show(sender.Items(SelectedItem).ToString(), sender, e.Bounds.Location)
  20.            End If
  21.        End If
  22.  
  23.    End Sub

Es necesario este usercontrol:

Código
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Runtime.InteropServices;
  5.  
  6. public class ComboBoxTooltip : ComboBox
  7. {
  8.    private DropdownWindow mDropdown;
  9.    public delegate void DropdownItemSelectedEventHandler(object sender, DropdownItemSelectedEventArgs e);
  10.    public event DropdownItemSelectedEventHandler DropdownItemSelected;
  11.  
  12.    protected override void OnDropDown(EventArgs e)
  13.    {
  14.        // Install wrapper
  15.        base.OnDropDown(e);
  16.        // Retrieve handle to dropdown list
  17.        COMBOBOXINFO info = new COMBOBOXINFO();
  18.        info.cbSize = Marshal.SizeOf(info);
  19.        SendMessageCb(this.Handle, 0x164, IntPtr.Zero, out info);
  20.        mDropdown = new DropdownWindow(this);
  21.        mDropdown.AssignHandle(info.hwndList);
  22.    }
  23.    protected override void OnDropDownClosed(EventArgs e)
  24.    {
  25.        // Remove wrapper
  26.        mDropdown.ReleaseHandle();
  27.        mDropdown = null;
  28.        base.OnDropDownClosed(e);
  29.        OnSelect(-1, Rectangle.Empty, true);
  30.    }
  31.    internal void OnSelect(int item, Rectangle pos, bool scroll)
  32.    {
  33.        if (this.DropdownItemSelected != null)
  34.        {
  35.            pos = this.RectangleToClient(pos);
  36.            DropdownItemSelected(this, new DropdownItemSelectedEventArgs(item, pos, scroll));
  37.        }
  38.    }
  39.    // Event handler arguments
  40.    public class DropdownItemSelectedEventArgs : EventArgs
  41.    {
  42.        private int mItem;
  43.        private Rectangle mPos;
  44.        private bool mScroll;
  45.        public DropdownItemSelectedEventArgs(int item, Rectangle pos, bool scroll) { mItem = item; mPos = pos; mScroll = scroll; }
  46.        public int SelectedItem { get { return mItem; } }
  47.        public Rectangle Bounds { get { return mPos; } }
  48.        public bool Scrolled { get { return mScroll; } }
  49.    }
  50.  
  51.    // Wrapper for combobox dropdown list
  52.    private class DropdownWindow : NativeWindow
  53.    {
  54.        private ComboBoxTooltip mParent;
  55.        private int mItem;
  56.        public DropdownWindow(ComboBoxTooltip parent)
  57.        {
  58.            mParent = parent;
  59.            mItem = -1;
  60.        }
  61.        protected override void WndProc(ref Message m)
  62.        {
  63.            // All we're getting here is WM_MOUSEMOVE, ask list for current selection for LB_GETCURSEL
  64.            Console.WriteLine(m.ToString());
  65.            base.WndProc(ref m);
  66.            if (m.Msg == 0x200)
  67.            {
  68.                int item = (int)SendMessage(this.Handle, 0x188, IntPtr.Zero, IntPtr.Zero);
  69.                if (item != mItem)
  70.                {
  71.                    mItem = item;
  72.                    OnSelect(false);
  73.                }
  74.            }
  75.            if (m.Msg == 0x115)
  76.            {
  77.                // List scrolled, item position would change
  78.                OnSelect(true);
  79.            }
  80.        }
  81.        private void OnSelect(bool scroll)
  82.        {
  83.            RECT rc = new RECT();
  84.            SendMessageRc(this.Handle, 0x198, (IntPtr)mItem, out rc);
  85.            MapWindowPoints(this.Handle, IntPtr.Zero, ref rc, 2);
  86.            mParent.OnSelect(mItem, Rectangle.FromLTRB(rc.Left, rc.Top, rc.Right, rc.Bottom), scroll);
  87.        }
  88.    }
  89.    // P/Invoke declarations
  90.    private struct COMBOBOXINFO
  91.    {
  92.        public Int32 cbSize;
  93.        public RECT rcItem;
  94.        public RECT rcButton;
  95.        public int buttonState;
  96.        public IntPtr hwndCombo;
  97.        public IntPtr hwndEdit;
  98.        public IntPtr hwndList;
  99.    }
  100.    [StructLayout(LayoutKind.Sequential)]
  101.    private struct RECT
  102.    {
  103.        public int Left;
  104.        public int Top;
  105.        public int Right;
  106.        public int Bottom;
  107.    }
  108.    [DllImport("user32.dll", EntryPoint = "SendMessageW", CharSet = CharSet.Unicode)]
  109.    private static extern IntPtr SendMessageCb(IntPtr hWnd, int msg, IntPtr wp, out COMBOBOXINFO lp);
  110.    [DllImport("user32.dll", EntryPoint = "SendMessageW", CharSet = CharSet.Unicode)]
  111.    private static extern IntPtr SendMessageRc(IntPtr hWnd, int msg, IntPtr wp, out RECT lp);
  112.    [DllImport("user32.dll")]
  113.    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
  114.    [DllImport("user32.dll")]
  115.    private static extern int MapWindowPoints(IntPtr hWndFrom, IntPtr hWndTo, [In, Out] ref RECT rc, int points);
  116. }
  117.  
En línea

z3nth10n


Desconectado Desconectado

Mensajes: 1.583


"Jack of all trades, master of none." - Zenthion


Ver Perfil WWW
Re: Librería de Snippets !! (Posteen aquí sus snippets)
« Respuesta #246 en: 5 Julio 2013, 12:43 pm »

Añadir difentes estilos a un "Label" (en realidad se usa un RichTextBox >:D)

Código
  1. 'Ejemplos:
  2.  
  3.        'RichTextLabel.AddTextWithFont("algo de texto con Arial al 12", New Font("Arial", 12, FontStyle.Bold), RichTextBox1)
  4.        'RichTextLabel.AddTextWithColor("ROOOJOOORL xD", Color.Red, RichTextBox1)
  5.        'RichTextLabel.AddTextWithColor(vbCrLf & "nueva linea y algo de texto", Color.Black, RichTextBox1)
  6.  
  7.  
  8. Public Class RichTextLabel
  9.  
  10.    Public Shared Sub AddTextWithFont(ByVal sText As String, ByVal oFont As Font, ByVal rtb As RichTextBox)
  11.  
  12.        Dim index As Integer
  13.        index = rtb.TextLength
  14.        rtb.AppendText(sText)
  15.        rtb.SelectionStart = index
  16.        rtb.SelectionLength = rtb.TextLength - index
  17.        rtb.SelectionFont = oFont
  18.        rtb.BorderStyle = System.Windows.Forms.BorderStyle.None
  19.        rtb.ReadOnly = True
  20.        rtb.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None
  21.  
  22.    End Sub
  23.  
  24.    Public Shared Sub AddTextWithColor(ByVal sText As String, ByVal oColor As Color, ByVal rtb As RichTextBox)
  25.  
  26.        Dim index As Integer
  27.        index = rtb.TextLength
  28.        rtb.AppendText(sText)
  29.        rtb.SelectionStart = index
  30.        rtb.SelectionLength = rtb.TextLength - index
  31.        rtb.SelectionColor = oColor
  32.        rtb.BorderStyle = System.Windows.Forms.BorderStyle.None
  33.        rtb.ReadOnly = True
  34.        rtb.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None
  35.  
  36.    End Sub
  37.  
  38. End Class
  39.  

Un saludo. >:D
En línea


Interesados hablad por Discord.
Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.788



Ver Perfil
Re: Librería de Snippets !! (Posteen aquí sus snippets)
« Respuesta #247 en: 5 Julio 2013, 13:20 pm »

Añadir difentes estilos a un "Label" (en realidad se usa un RichTextBox >:D)

Se puede mejorar muy mucho, para evitar todas las cosas que dije... aquí tienes:

Código
  1. Add_Text_With_Color(RichTextBox1, "algo de texto con Arial al 12", RichTextBox1.ForeColor, New Font("Arial", 12, FontStyle.Bold))
  2. Add_Text_With_Color(RichTextBox1, " ROOOJOOORL xD", Color.Red)
  3. Add_Text_With_Color(RichTextBox1, Environment.NewLine & "nueva linea y algo de texto", Color.Black)

Código
  1.    Public Sub Add_Text_With_Color(ByVal richTextBox As RichTextBox, _
  2.                                          ByVal text As String, _
  3.                                          ByVal color As Color, _
  4.                                          Optional ByVal font As Font = Nothing)
  5.  
  6.        richTextBox.Enabled = False
  7.        richTextBox.BorderStyle = BorderStyle.None
  8.        richTextBox.ScrollBars = RichTextBoxScrollBars.None
  9.  
  10.        Dim index As Int32 = richTextBox.TextLength
  11.        richTextBox.AppendText(text)
  12.        richTextBox.SelectionStart = index
  13.        richTextBox.SelectionLength = richTextBox.TextLength - index
  14.        richTextBox.SelectionColor = color
  15.        If font IsNot Nothing Then richTextBox.SelectionFont = font
  16.  
  17.    End Sub
  18.  

Saludos
« Última modificación: 5 Julio 2013, 13:43 pm por EleKtro H@cker » En línea

z3nth10n


Desconectado Desconectado

Mensajes: 1.583


"Jack of all trades, master of none." - Zenthion


Ver Perfil WWW
Re: Librería de Snippets !! (Posteen aquí sus snippets)
« Respuesta #248 en: 5 Julio 2013, 13:34 pm »

Tás colao, necesitas poner un Public Shared Sub y no un Public Sub na más. >:D
Por cierto, muchas gracias, como siempre mejorando mi Snippets... A ver si algún día es de al revés. ;) :laugh:
En línea


Interesados hablad por Discord.
Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.788



Ver Perfil
Re: Librería de Snippets !! (Posteen aquí sus snippets)
« Respuesta #249 en: 5 Julio 2013, 13:47 pm »

Tás colao, necesitas poner un Public Shared Sub y no un Public Sub na más. >:D

No me he colado Ikillnukes, el shared no es obligatorio, eso depende de las necesidades. En el snippet original hay una Class para meter dos mini procedimientos, en mi snippet como ves no hay ninguna Class externa y los dos procedimientos están simplificados en sólo uno, si necesitas sharearla pues hazlo.

Si lo quieres llamar desde otra class:
Código
  1. Form1.Add_Text_With_Color(Form1.RichTextBox1, "lo que sea", Color.AliceBlue)

Saludos
« Última modificación: 5 Julio 2013, 13:53 pm por EleKtro H@cker » En línea

Páginas: 1 ... 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [25] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 ... 58 Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines