el Guille, la Web del Visual Basic, C#, .NET y más...

Código fuente de LanConfig 2008

El formulario de configuración (fLanCfg_config)

 
Publicado el 12/Ene/2008
Actualizado el 12/Ene/2008
Autor: Guillermo 'guille' Som

Este es el código fuente (en línea) de la utilidad LanConfig 2008. En esta página está el del formulario de configuración (fLanCfg_config).



 

Introducción:

Pues eso... aunque puedes bajarte el código fuente del proyecto para Visual Basic 2008 desde la página principal de la utilidad LanConfig 2008, (que como sabes es una utilidad para manejar las configuraciones de redes que tienes instalada en tu quipo), me he decidido a publicar el código fuente de forma independiente y en línea, con idea de que puedas ver algunas de las cosas que hace la aplicación sin tener que bajarte el proyecto y verlo de forma desconectada.

En los temas relacionados te pongo los links a las diferentes partes (formularios y clases) que utiliza este proyecto.

 

'------------------------------------------------------------------------------
' fLanCfg_config                                                    (25/Dic/07)
' Para configurar LanConfig
'
' ©Guillermo 'guille' Som, 2007
'------------------------------------------------------------------------------
Option Strict On

Imports Microsoft.VisualBasic
Imports vb = Microsoft.VisualBasic
Imports System
Imports System.Windows.Forms
Imports System.Drawing

Imports System.Collections.Generic

Public Class fLanCfg_config

    Private deshaciendo As Boolean = False

    Private expanded As New List(Of Boolean)
    Private grSize As New List(Of Size)

    Public Sub New()
        deshaciendo = True

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        deshaciendo = False

        ' Add any initialization after the InitializeComponent() call.

        ' Líneas 3D
        Dim ColorOscuro As System.Drawing.Color = Color.FromKnownColor(KnownColor.ControlDark)
        Dim ColorClaro As System.Drawing.Color = Color.FromKnownColor(KnownColor.ControlLightLight)
        Const indentValue As Integer = 4
        Dim mWidth As Integer = Me.DisplayRectangle.Width - (indentValue * 2)

        With linea1
            .BackColor = ColorOscuro
            .Height = 1
            .Left = indentValue
            .Width = mWidth
        End With
        With linea2
            .BackColor = ColorClaro
            .Height = 1
            .Top = linea1.Top + 1
            .Left = indentValue
            .Width = mWidth
        End With

        ' Crear las listas usando los valores indicados
        expanded = crearLista(True, True, True)
        grSize = crearLista(GroupBox1.Size, GroupBox2.Size, GroupBox3.Size)

        toolStripFichero.Top = 12
        toolStripFichero.Left = 6
        toolStripAyuda.Top = 42
        toolStripAyuda.Left = 6

        lvConfigs.Columns(0).Width = lvConfigs.ClientSize.Width + 200

        ' Habilitar el botón solo si es administrador
        Me.btnObtenerCfg.Enabled = LanCfg.EsAdministrador

        btnDeshacer_Click()

    End Sub


    Private Function crearLista(Of T)(ByVal ParamArray valores() As T) As List(Of T)
        Dim lista As New List(Of T)
        lista.AddRange(valores)

        Return lista
    End Function

    Private Sub pic1_Click(ByVal sender As Object, _
                           ByVal e As EventArgs) _
                           Handles pic1.Click, pic2.Click, pic3.Click
        ' El picture en el que se ha hecho click
        Dim pic As PictureBox = TryCast(sender, PictureBox)
        If pic Is Nothing Then Exit Sub

        ' El panel en el que está este picture
        Dim expanderPanel As Panel = TryCast(pic.Parent, Panel)
        If expanderPanel Is Nothing Then Exit Sub

        ' El GroupBox en el que está el picture
        Dim expanderGroup As GroupBox = TryCast(expanderPanel.Parent, GroupBox)
        If expanderGroup Is Nothing Then Exit Sub

        ' El índice será el final del nombre menos uno
        ' (los pictures deben llamarse nnnn1, nnnn2, etc.)
        Dim index As Integer = CInt(vb.Right(pic.Name, 1)) - 1

        Dim expanderExpanded As Boolean = expanded(index)
        Dim expanderSize As Size = grSize(index)

        expanderExpanded = Not expanderExpanded

        If expanderExpanded Then
            pic.Image = My.Resources.ExpanderUp
            expanderGroup.Size = expanderSize
            'toolTip1.SetToolTip(pic, " Ocultar las opciones ")
        Else
            pic.Image = My.Resources.ExpanderDown
            expanderGroup.Size = expanderPanel.Size
            'toolTip1.SetToolTip(pic, " Mostrar las opciones ")
        End If

        expanded(index) = expanderExpanded

        ' Ajustar el tamaño del formulario
        Me.Height = 117 + GroupBox1.Height + GroupBox2.Height + GroupBox3.Height

    End Sub


    Private Sub btnCancelar_Click() Handles btnCancelar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub btnAceptar_Click() Handles btnAceptar.Click
        directorioCfg()

        With My.Settings
            .mostrarImagenesMenus = Me.chkMostrarImagenes.Checked
            .mostrarImagenesTodos = Me.chkMostrarImagenesTodos.Checked
            .usarMisDocumentos = Me.optDirDocumentos.Checked
            .usarDirPrograma = Me.optDirProg.Checked
            .usarDirPersonalizado = Me.optDirPersonalizado.Checked
            If .usarDirPersonalizado Then
                .dirConfig = Me.txtDirPersonal.Text
            End If
            If Me.optImagenesJuansa.Checked Then
                .tipoImagenesMenus = 0
            ElseIf Me.optImagenesGuille.Checked Then
                .tipoImagenesMenus = 1
            ElseIf Me.optImagenesPersonalizado.Checked Then
                .tipoImagenesMenus = 2
            End If
            .imgAcerca = CInt(opcAcerca.Tag)
            .imgAplicar = CInt(opcAplicar.Tag)
            .imgAyuda = CInt(opcAyuda.Tag)
            .imgCerrar = CInt(opcCerrar.Tag)
            .imgFichero = CInt(opcFichero.Tag)
            .imgGeneral = CInt(opcGeneral.Tag)
            .imgIdioma = CInt(opcIdioma.Tag)
            .imgPersonalizar = CInt(opcPersonalizar.Tag)
        End With
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnDeshacer_Click() Handles btnDeshacer.Click
        ' para que no se ejecute el código de datosCambiados()
        deshaciendo = True

        ' Dejar los valores iniciales
        With My.Settings

            Me.chkMostrarImagenes.Checked = .mostrarImagenesMenus
            Me.chkMostrarImagenesTodos.Checked = .mostrarImagenesTodos
            Me.optDirDocumentos.Checked = .usarMisDocumentos
            Me.optDirPersonalizado.Checked = .usarDirPersonalizado
            Me.optDirProg.Checked = .usarDirPrograma
            Me.optDirPersonalizado.Checked = .usarDirPersonalizado

            Me.txtDirPersonal.Text = .dirConfig
            optDirPersonalizado_CheckedChanged(Nothing, Nothing)

            Select Case .tipoImagenesMenus
                Case 1
                    Me.optImagenesGuille.Checked = True
                    .imgAcerca = 1
                    .imgAplicar = 1
                    .imgAyuda = 1
                    .imgCerrar = 1
                    .imgFichero = 1
                    .imgGeneral = 1
                    .imgIdioma = 1
                    .imgPersonalizar = 1
                Case 2
                    Me.optImagenesPersonalizado.Checked = True
                Case Else ' 0
                    Me.optImagenesJuansa.Checked = True
                    .imgAcerca = 0
                    .imgAplicar = 0
                    .imgAyuda = 0
                    .imgCerrar = 0
                    .imgFichero = 0
                    .imgGeneral = 0
                    .imgIdioma = 0
                    .imgPersonalizar = 0
            End Select
            opcAcerca.Tag = .imgAcerca
            opcAplicar.Tag = .imgAplicar
            opcAyuda.Tag = .imgAyuda
            opcCerrar.Tag = .imgCerrar
            opcFichero.Tag = .imgFichero
            opcIdioma.Tag = .imgIdioma
            opcGeneral.Tag = .imgGeneral
            opcPersonalizar.Tag = .imgPersonalizar

            optImagenesPersonalizado_CheckedChanged(Nothing, Nothing)

            TryCast(opcAcerca.DropDownItems(.imgAcerca), ToolStripMenuItem).Checked = True
            opcAcerca.Image = opcAcerca.DropDownItems(.imgAcerca).Image
            TryCast(opcAplicar.DropDownItems(.imgAplicar), ToolStripMenuItem).Checked = True
            opcAplicar.Image = opcAplicar.DropDownItems(.imgAplicar).Image
            TryCast(opcAyuda.DropDownItems(.imgAyuda), ToolStripMenuItem).Checked = True
            opcAyuda.Image = opcAyuda.DropDownItems(.imgAyuda).Image
            TryCast(opcCerrar.DropDownItems(.imgCerrar), ToolStripMenuItem).Checked = True
            opcCerrar.Image = opcCerrar.DropDownItems(.imgCerrar).Image
            TryCast(opcFichero.DropDownItems(.imgFichero), ToolStripMenuItem).Checked = True
            opcFichero.Image = opcFichero.DropDownItems(.imgFichero).Image
            TryCast(opcIdioma.DropDownItems(.imgIdioma), ToolStripMenuItem).Checked = True
            opcIdioma.Image = opcIdioma.DropDownItems(.imgIdioma).Image
            TryCast(opcGeneral.DropDownItems(.imgGeneral), ToolStripMenuItem).Checked = True
            opcGeneral.Image = opcGeneral.DropDownItems(.imgGeneral).Image
            TryCast(opcPersonalizar.DropDownItems(.imgPersonalizar), ToolStripMenuItem).Checked = True
            opcPersonalizar.Image = opcPersonalizar.DropDownItems(.imgPersonalizar).Image
        End With

        If lvConfigs.CheckedItems.Count > 0 Then
            For Each lvi As ListViewItem In lvConfigs.Items
                lvi.Checked = False
            Next
        End If

        btnDeshacer.Enabled = False
        btnAceptar.Enabled = False

        deshaciendo = False
    End Sub

    ''' <summary>
    ''' Comprueba si se han cambiado los datos
    ''' con respecto a como estaban inicialmente
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub datosCambiados()
        If deshaciendo Then Exit Sub

        Dim cambiados As Boolean = False

        With My.Settings
            If Me.chkMostrarImagenes.Checked <> .mostrarImagenesMenus Then
                cambiados = True
            End If
            If Me.chkMostrarImagenesTodos.Checked <> .mostrarImagenesTodos Then
                cambiados = True
            End If
            If Me.optDirDocumentos.Checked <> .usarMisDocumentos Then
                cambiados = True
            End If
            If Me.optDirPersonalizado.Checked <> .usarDirPersonalizado Then
                cambiados = True
            End If
            If Me.optDirProg.Checked <> .usarDirPrograma Then
                cambiados = True
            End If
            If Me.optDirPersonalizado.Checked <> .usarDirPersonalizado Then
                cambiados = True
            End If
            If Me.txtDirPersonal.Text <> .dirConfig Then
                cambiados = True
            End If
            Select Case .tipoImagenesMenus
                Case 1
                    If Me.optImagenesGuille.Checked <> True Then
                        cambiados = True
                    End If
                Case 2
                    If Me.optImagenesPersonalizado.Checked <> True Then
                        cambiados = True
                    End If
                Case Else ' 0
                    If Me.optImagenesJuansa.Checked <> True Then
                        cambiados = True
                    End If
            End Select

            If TryCast(opcAcerca.DropDownItems(.imgAcerca), ToolStripMenuItem).Checked <> True Then
                cambiados = True
            End If
            If TryCast(opcAplicar.DropDownItems(.imgAplicar), ToolStripMenuItem).Checked <> True Then
                cambiados = True
            End If
            If TryCast(opcAyuda.DropDownItems(.imgAyuda), ToolStripMenuItem).Checked <> True Then
                cambiados = True
            End If
            If TryCast(opcCerrar.DropDownItems(.imgCerrar), ToolStripMenuItem).Checked <> True Then
                cambiados = True
            End If
            If TryCast(opcFichero.DropDownItems(.imgFichero), ToolStripMenuItem).Checked <> True Then
                cambiados = True
            End If
            If TryCast(opcIdioma.DropDownItems(.imgIdioma), ToolStripMenuItem).Checked <> True Then
                cambiados = True
            End If
            If TryCast(opcGeneral.DropDownItems(.imgGeneral), ToolStripMenuItem).Checked <> True Then
                cambiados = True
            End If
            If TryCast(opcPersonalizar.DropDownItems(.imgPersonalizar), ToolStripMenuItem _
                       ).Checked <> True Then
                cambiados = True
            End If

            If lvConfigs.CheckedItems.Count > 0 Then
                cambiados = True
            End If
        End With

        btnDeshacer.Enabled = cambiados
        btnAceptar.Enabled = cambiados
    End Sub

    Private Sub optImagenesJuansa_CheckedChanged(ByVal sender As Object, _
                                                 ByVal e As EventArgs) _
                                                 Handles optImagenesJuansa.CheckedChanged, _
                                                         optImagenesGuille.CheckedChanged
        Dim opt As RadioButton = TryCast(sender, RadioButton)
        If opt Is Nothing Then Exit Sub

        If opt.Checked Then
            optImagenesPersonalizado.Checked = False
        End If
        datosCambiados()
    End Sub

    Private Sub optImagenesPersonalizado_CheckedChanged(ByVal sender As Object, _
                                                        ByVal e As EventArgs) _
                                                        Handles optImagenesPersonalizado.CheckedChanged
        Me.grupoImagenesMenus.Enabled = optImagenesPersonalizado.Checked
        datosCambiados()
    End Sub

    Private Sub optDirPersonalizado_CheckedChanged(ByVal sender As Object, _
                                                   ByVal e As EventArgs) _
                                                   Handles optDirPersonalizado.CheckedChanged
        Me.txtDirPersonal.Enabled = optDirPersonalizado.Checked
        Me.btnExaminar.Enabled = optDirPersonalizado.Checked
        directorioCfg()
        datosCambiados()
    End Sub

    Private Sub optDirProg_CheckedChanged(ByVal sender As Object, _
                                          ByVal e As EventArgs) _
                                          Handles optDirProg.CheckedChanged, _
                                                  optDirDocumentos.CheckedChanged
        Dim opt As RadioButton = TryCast(sender, RadioButton)
        If opt Is Nothing Then Exit Sub

        If opt.Checked Then
            optDirPersonalizado.Checked = False
        End If
        directorioCfg()
        datosCambiados()
    End Sub

    Private Sub directorioCfg()
        Dim dirCfg As String

        ' Para guardar el fichero en el directorio del ejecutable
        With My.Application.Info
            If Me.optDirDocumentos.Checked Then
                ' Mis Documentos\<aplicacion>
                dirCfg = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\" & .ProductName
                If My.Computer.FileSystem.DirectoryExists(dirCfg) = False Then
                    My.Computer.FileSystem.CreateDirectory(dirCfg)
                End If
            ElseIf Me.optDirPersonalizado.Checked Then
                dirCfg = Me.txtDirPersonal.Text ' My.Settings.dirConfig
                If String.IsNullOrEmpty(dirCfg) Then
                    dirCfg = My.Computer.FileSystem.SpecialDirectories.MyDocuments
                End If
                If dirCfg.ToLower().Contains(My.Application.Info.ProductName.ToLower()) = False Then
                    dirCfg = Path.Combine(dirCfg, My.Application.Info.ProductName)
                End If
                Try
                    If My.Computer.FileSystem.DirectoryExists(dirCfg) = False Then
                        My.Computer.FileSystem.CreateDirectory(dirCfg)
                    End If
                Catch ex As Exception
                    ' Si da error al crear el path indicado, usar el del programa
                    dirCfg = .DirectoryPath
                    Me.optDirProg.Checked = True
                    Me.optDirPersonalizado.Checked = False
                    Me.optDirDocumentos.Checked = False
                End Try
            Else
                dirCfg = .DirectoryPath
            End If
        End With

        Me.txtDirPersonal.Text = dirCfg
    End Sub


    Private Sub cambiarImagenOpciones(ByVal opPrinc As ToolStripDropDownButton, ByVal sender As Object)
        If deshaciendo Then Exit Sub

        Dim op As ToolStripMenuItem = TryCast(sender, ToolStripMenuItem)
        If op Is Nothing Then Exit Sub

        If (TypeOf opPrinc Is ToolStripDropDownButton) = False Then
            Exit Sub
        End If

        For Each it As ToolStripMenuItem In opPrinc.DropDownItems
            it.Checked = False
        Next
        op.Checked = True
        opPrinc.Image = op.Image
        ' Al final del nombre de la opción está el índice
        opPrinc.Tag = vb.Right(op.Name, 1)

        datosCambiados()
    End Sub

    Private Sub opcFichero1_Click(ByVal sender As Object, _
                                  ByVal e As EventArgs) _
                                  Handles opcFichero0.Click, opcFichero2.Click, opcFichero1.Click
        cambiarImagenOpciones(opcFichero, sender)
    End Sub

    Private Sub opcAplicar1_Click(ByVal sender As Object, _
                                  ByVal e As EventArgs) _
                                  Handles opcAplicar0.Click, opcAplicar2.Click, opcAplicar1.Click
        cambiarImagenOpciones(opcAplicar, sender)
    End Sub

    Private Sub opcPersonalizar1_Click(ByVal sender As Object, _
                                       ByVal e As EventArgs) _
                                       Handles opcPersonalizar0.Click, opcPersonalizar2.Click, _
                                               opcPersonalizar1.Click
        cambiarImagenOpciones(opcPersonalizar, sender)
    End Sub

    Private Sub opcCerrar1_Click(ByVal sender As Object, _
                                 ByVal e As EventArgs) _
                                 Handles opcCerrar0.Click, opcCerrar2.Click, opcCerrar1.Click
        cambiarImagenOpciones(opcCerrar, sender)
    End Sub

    Private Sub opcAyuda1_Click(ByVal sender As Object, _
                                ByVal e As EventArgs) _
                                Handles opcAyuda0.Click, opcAyuda2.Click, opcAyuda1.Click
        cambiarImagenOpciones(opcAyuda, sender)
    End Sub

    Private Sub opcGeneral1_Click(ByVal sender As Object, _
                                  ByVal e As EventArgs) _
                                  Handles opcGeneral0.Click, opcGeneral2.Click, opcGeneral1.Click
        cambiarImagenOpciones(opcGeneral, sender)
    End Sub

    Private Sub opcAcerca1_Click(ByVal sender As Object, _
                                 ByVal e As EventArgs) _
                                 Handles opcAcerca0.Click, opcAcerca2.Click, opcAcerca1.Click
        cambiarImagenOpciones(opcAcerca, sender)
    End Sub

    Private Sub opcIdioma0_Click(ByVal sender As Object, _
                                 ByVal e As EventArgs) _
                                 Handles opcIdioma0.Click, opcIdioma1.Click, opcIdioma2.Click
        cambiarImagenOpciones(opcIdioma, sender)
    End Sub

    Private Sub chkMostrarImagenesTodos_CheckedChanged(ByVal sender As Object, _
                                                       ByVal e As EventArgs) _
                                                       Handles chkMostrarImagenesTodos.CheckedChanged

        grupoImagenes.Enabled = chkMostrarImagenesTodos.Checked
        datosCambiados()
    End Sub

    Private Sub chkMostrarImagenes_CheckedChanged(ByVal sender As Object, _
                                                  ByVal e As EventArgs) _
                                                  Handles chkMostrarImagenes.CheckedChanged
        datosCambiados()
    End Sub

    Private Sub btnExaminar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExaminar.Click
        Dim fbd As New FolderBrowserDialog

        Dim dirCfg As String = Me.txtDirPersonal.Text

        If String.IsNullOrEmpty(dirCfg) Then
            dirCfg = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        End If
        If dirCfg.ToLower().Contains(My.Application.Info.ProductName.ToLower()) = False Then
            dirCfg = Path.Combine(dirCfg, My.Application.Info.ProductName)
        End If

        With fbd
            .Description = My.Resources.fLanCfgFBDDesc
            .RootFolder = Environment.SpecialFolder.MyComputer
            .ShowNewFolderButton = True
            .SelectedPath = dirCfg
            If .ShowDialog() = DialogResult.OK Then
                dirCfg = .SelectedPath
                ' Comprobar si incluye el ProductName
                ' si no es así, agregarlo
                If dirCfg.ToLower().Contains(My.Application.Info.ProductName.ToLower()) = False Then
                    dirCfg = Path.Combine(.SelectedPath, My.Application.Info.ProductName)
                End If
                Me.optDirPersonalizado.Checked = True
                Me.optDirProg.Checked = False
                Me.optDirDocumentos.Checked = False
                ' Crear el directorio con el nombre de la aplicación
                Try
                    If My.Computer.FileSystem.DirectoryExists(dirCfg) = False Then
                        My.Computer.FileSystem.CreateDirectory(dirCfg)
                    End If
                Catch ex As Exception
                    ' Si da error al crear el path indicado, usar el del programa
                    MessageBox.Show(My.Resources.fLanCfgFBDMsg1 & vbCrLf & _
                                    My.Resources.fLanCfgFBDMsg2 & vbCrLf & _
                                    ex.Message & vbCrLf & vbCrLf & _
                                    My.Resources.fLanCfgFBDMsg3, _
                                    My.Resources.fLanCfgFBDMsg4, _
                                    MessageBoxButtons.OK, _
                                    MessageBoxIcon.Exclamation)
                    dirCfg = My.Application.Info.DirectoryPath
                    Me.optDirProg.Checked = True
                    Me.optDirPersonalizado.Checked = False
                    Me.optDirDocumentos.Checked = False
                End Try

                Me.txtDirPersonal.Text = dirCfg
            End If
        End With
    End Sub

    Private Sub btnObtenerCfg_Click(ByVal sender As Object, _
                                    ByVal e As EventArgs) _
                                    Handles btnObtenerCfg.Click
        ' Obtener las configuraciones de red                        (30/Dic/07)
        Dim colCfg As List(Of LanCfg) = LanCfg.GetLanConfig()
        If colCfg Is Nothing OrElse colCfg.Count = 0 Then
            ' No se ha podido obtener la información
            Exit Sub
        End If
        lvConfigs.Items.Clear()
        For Each c As LanCfg In colCfg
            Dim lvi As ListViewItem = lvConfigs.Items.Add(c.ToString(LanCfg.Formatos.Info))
            lvi.Tag = c
            lvi.Checked = False
        Next
    End Sub

    Private Sub lvConfigs_ItemChecked(ByVal sender As Object, _
                                      ByVal e As ItemCheckedEventArgs) _
                                      Handles lvConfigs.ItemChecked
        datosCambiados()
    End Sub

End Class

 

Espero que te sea de utilidad.

Nos vemos.
Guillermo



 


Haz tu donativo a este sitio con PayPal
 

Busca en este sitio con Google
 
Google

La fecha/hora en el servidor es: 19/04/2024 18:52:02

La fecha actual GMT (UTC) es: 

©Guillermo 'guille' Som, 1996-2024