Deberían hacer un post en esta sección que contenga sólamente snippets y donde todos aporten snippets útiles
He hecho este snippet para agilizar el renombramiento de archivos, aquí tienen
PD: Uso "MOVE" porque de otra forma es imposible renombrar el archivo con el mismo nombre, como bien está explicado aquí por
NovLucker:
http://foro.elhacker.net/net/solucionado_iquestcomo_renombrar_un_archivo_o_carpeta_con_el_mismo_nombre-t378839.0.html ' Usage:
'
' RenameFile("C:\Test.txt", "TeSt.TxT")
' RenameFile("C:\Test.txt", "Test", "doc")
' RenameFile(FileInfoObject.FullName, FileInfoObject.Name.ToLower, FileInfoObject.Extension.ToUpper)
' If RenameFile("C:\Test.txt", "TeSt.TxT") Is Nothing Then MsgBox("El archivo no existe!")
#Region " RenameFile function "
Private Function RenameFile
(ByVal File As String,
ByVal NewFileName
As String,
Optional ByVal NewFileExtension
As String = Nothing) Try
Dim FileToBeRenamed
As New System.
IO.
FileInfo(File) If NewFileExtension Is Nothing Then
FileToBeRenamed.MoveTo(FileToBeRenamed.Directory.FullName & "\" & NewFileName) ' Rename file with same extension
Else
FileToBeRenamed.MoveTo(FileToBeRenamed.Directory.FullName & "\" & NewFileName & NewFileExtension) ' Rename file with new extension
End If
Return True ' File was renamed OK
Catch ex As Exception
' MsgBox(ex.Message)
Return False ' File can't be renamed maybe because User Permissions
End Try
Else
Return Nothing ' File doesn't exist
End If
End Function
#End Region
Y unos cuantos más...
Modificar atributos de archivos:
' Usage:
' Attrib("File.txt", IO.FileAttributes.ReadOnly + IO.FileAttributes.Hidden)
' If Attrib("File.txt", IO.FileAttributes.System) Is Nothing Then MsgBox("File doesn't exist!")
Private Function Attrib
(ByVal File As String,
ByVal Attributes
As System.
IO.
FileAttributes) Try
FileSystem.
SetAttr(File, Attributes
) Return True ' File was modified OK
Catch ex As Exception
' MsgBox(ex.Message)
Return False ' File can't be modified maybe because User Permissions
End Try
Else
Return Nothing ' File doesn't exist
End If
End Function
Controlar el mismo evento para varios controles:
Private Sub Button_Is_Clicked(sender As Object, e As EventArgs) Handles _
Button1.Click, _
Button2.Click, _
Button3.Click
Dim Clicked_Button As Button = CType(sender, Button)
If Clicked_Button.Name = "Button1" Then
' Things for Button1
ElseIf Clicked_Button.Name = "Button2" Then
' Things for Button2
ElseIf Clicked_Button.Name = "Button3" Then
' Things for Button3
End If
Ens Sub
Un link label:
' First add a LinkLabel control into the form.
Private Sub LinkLabel_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
System.Diagnostics.Process.Start("http://www.Google.com")
System.Diagnostics.Process.Start("mailto:ME@Hotmail.com")
End Sub
Procesar todos los archivos de texto de My.Resources:
For Each ResourceFile As DictionaryEntry In My.Resources.ResourceManager.GetResourceSet(Globalization.CultureInfo.CurrentCulture, True, True).OfType(Of Object)()
If TypeOf (ResourceFile.Value) Is String Then
MsgBox(My.Resources.ResourceManager.GetObject(ResourceFile.Key))
'MsgBox(ResourceFile.Key) ' Resource Name
'MsgBox(ResourceFile.Value) ' Resource FileContent
End If
Next
Procesar todos los archivos de imagen de My.Resources:
For Each ResourceFile As DictionaryEntry In My.Resources.ResourceManager.GetResourceSet(Globalization.CultureInfo.CurrentCulture, True, True).OfType(Of Object)()
If TypeOf (ResourceFile.Value) Is Drawing.Image Then
Button_2000_2006.Image = ResourceFile.Value
'MsgBox(ResourceFile.Key) ' Resource Name
'MsgBox(ResourceFile.Value) ' Resource FileContent
End If
Next
Ordenar un listview al clickar sobre la columna a ordenar:
' Instructions:
' 1. Add the class
' 2. Add the declaration
' 3. Add a listview
Dim ColumnOrder As String = "Down"
#Region " ListView Sort Column event "
Private Sub ListView_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
If ColumnOrder = "Down" Then
Me.ListView1.ListViewItemSorter = New OrdenarListview(e.Column, SortOrder.Ascending)
ListView1.Sort()
ColumnOrder = "Up"
ElseIf ColumnOrder = "Up" Then
Me.ListView1.ListViewItemSorter = New OrdenarListview(e.Column, SortOrder.Descending)
ListView1.Sort()
ColumnOrder = "Down"
End If
End Sub
#End Region
#Region " OrdenarListView [CLASS] "
Public Class OrdenarListview
Implements IComparer
Private vIndiceColumna As Integer
Private vTipoOrden As SortOrder
Public Sub New(ByVal pIndiceColumna As Integer, ByVal pTipoOrden As SortOrder)
vIndiceColumna = pIndiceColumna
vTipoOrden = pTipoOrden
End Sub
Public Function Ordenar(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim item_x As ListViewItem = DirectCast(x, ListViewItem)
Dim item_y As ListViewItem = DirectCast(y, ListViewItem)
Dim string_x As String
If item_x.SubItems.Count <= vIndiceColumna Then
string_x = ""
Else
string_x = item_x.SubItems(vIndiceColumna).Text
End If
Dim string_y As String
If item_y.SubItems.Count <= vIndiceColumna Then
string_y = ""
Else
string_y = item_y.SubItems(vIndiceColumna).Text
End If
If vTipoOrden = SortOrder.Ascending Then
If IsNumeric(string_x) And IsNumeric(string_y) Then
Return Val(string_x).CompareTo(Val(string_y))
ElseIf IsDate(string_x) And IsDate(string_y) Then
Return DateTime.Parse(string_x).CompareTo(DateTime.Parse(string_y))
Else
Return String.Compare(string_x, string_y)
End If
Else
If IsNumeric(string_x) And IsNumeric(string_y) Then
Return Val(string_y).CompareTo(Val(string_x))
ElseIf IsDate(string_x) And IsDate(string_y) Then
Return DateTime.Parse(string_y).CompareTo(DateTime.Parse(string_x))
Else
Return String.Compare(string_y, string_x)
End If
End If
End Function
End Class
#End Region
Un ejemplo de un SaveFileDialog:
Dim SaveFile As New SaveFileDialog
SaveFile.Title = "Save a Report File"
SaveFile.InitialDirectory = Environ("programfiles")
SaveFile.RestoreDirectory = True
SaveFile.DefaultExt = "txt"
SaveFile.Filter = "txt file (*.txt)|*.txt"
SaveFile.CheckPathExists = True
'SaveFile.CheckFileExists = True
'SaveFile.ShowDialog()
If SaveFile.ShowDialog() = DialogResult.OK Then
MsgBox(SaveFile.FileName)
End If
Centrar un form secundario en el form principal:
#Region " CenterForm function "
Function CenterForm(ByVal Form_to_Center As Form, ByVal Form_Location As Point) As Point
Dim FormLocation As New Point
FormLocation.X = (Me.Left + (Me.Width - Form_to_Center.Width) / 2) ' set the X coordinates.
FormLocation.Y = (Me.Top + (Me.Height - Form_to_Center.Height) / 2) ' set the Y coordinates.
Return FormLocation ' return the Location to the Form it was called from.
End Function
#End Region
' Form2 Load
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Location = Form1.centerForm(Me, Me.Location)
End Sub
' Private Sub Button_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover
' Form2.Show()
' End Sub
' Private Sub Button_MouseLeave(sender As Object, e As EventArgs) Handles Button1.MouseLeave
' Form2.Dispose()
' End Sub
Saludos!