Thursday, April 23, 2009

Recursively Iterate through a folder without using FileSystemObject in VB.NET

    Dim files As ReadOnlyCollection(Of String)
    files = My.Computer.FileSystem.GetFiles("c:\modifiedfiles\", FileIO.SearchOption.SearchAllSubDirectories, "*.*")
    For index As Integer = 0 To files.Count - 1
       MsgBox files(index)
    Next

Tuesday, April 21, 2009

Increase the brightness on my Laptop

For some reason the brightness on my laptop screen is dark.  To increase/decrease the brightness use the Fn key and the Up/Down arrow.
 

Wednesday, April 15, 2009

Have a CheckedListBox only allow one selection at a time

At first I thought the SelectionMode.One property might be what I want but that doesn't seem to do it.  The block of code below seems to do it.

In the Handles clause add the CheckedListBox(s) that you want to only allow one selection for.

  Private Sub myList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged, CheckedListBox2.SelectedIndexChanged
    For index As Integer = 0 To sender.Items.Count - 1
      If index <> sender.SelectedIndex Then
        sender.setitemcheckstate(index, CheckState.Unchecked)
      End If
    Next
  End Sub


Wednesday, April 1, 2009

Change the cursor of the mouse to that of a circle

I needed a tool similar to one in a paint program where you can change the mouse cursor to that of a circle.  Initially my solution was to draw a circle around the mouse and to adjust this circle on mousemove.  Therefore you had a circle that followed the mouse around the screen.  However that was not a good solution as there were latent circles all over the image (it actually works if you don't have a background image).  I then tried using the OvalShape tool that came with the PowerPack3 but again that wasn't good with a background image.  Below is a solution that works.


Public Class Form1 
    Private radius As Integer = 20
    Private startAngle As Integer = 0
    Private sweepAngle As Integer = 360
    Private currentColor As Color = Color.Red
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateCursor()
    End Sub
 
    Private Sub CreateCursor()
        Dim bmp As New Bitmap(radius * 2, radius * 2)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim myPen As New Pen(currentColor, 1)
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
        g.DrawArc(myPen, 0, 0, bmp.Width - 1, bmp.Height - 1, startAngle, sweepAngle)
        g.Dispose()
        myPen.Dispose()
 
        Dim C As New Cursor(bmp.GetHicon)
        Me.Cursor = C
    End Sub

End Class


fill the area outside of a polygon with a color

The graphics object has a fillpolygon function.  I wanted a solution to the problem of receiving an image and a polygon points and the need to fill the area outside of that polygon with a color.  The solution below solves that problem.

Imports System.Drawing.Drawing2D
Public Class Form1

  Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    Dim polypoints() As Point

    ' Draw a polygon.
    polypoints = New Point() { _
        New Point(25, 100), _
        New Point(275, 350), _
        New Point(275, 100), _
        New Point(25, 350) _
    }

    Using gp As New GraphicsPath
      gp.AddPolygon(polypoints)
      Using rgn As New Region(gp)
        e.Graphics.ExcludeClip(rgn)
        e.Graphics.FillRectangle(Brushes.Red, Me.ClientRectangle)
      End Using
    End Using

  End Sub

End Class