Monday, June 22, 2009

Padding out a cell with leading zeros in Excel

http://www.mrexcel.com/archive/General/14898.html

=REPT(0,8-LEN(A1))&A1

Or you can use the Format->Custom and enter 0000

Excel Formula to extract the Path from a filename

If you have C:\Temp\Files\1.txt
this wil lreturn C:\Temp\Files\

=MID(F1,FIND("*",SUBSTITUTE(F1,"\","*",LEN(F1)-LEN(SUBSTITUTE(F1,"\",""))))+1,LEN(F1))

Changing the Icon for an Application

For some reason I forgot about this. But when you launch an application, I know about having an icon for the form, but I forgot about having an icon for the application so that when it runs it has your icon on the task bar.

To do this its real simple. Just go to the Project Properties. Click on the Application Tab (it is probably the default selection). Then select "Icon:" and browse to in icon that you would lile. That's it.

Friday, June 19, 2009

How to return a dialog box result

I keep forgetting this but when you show a form via Showdialog you can check to see how that form was closed via  something like this:

If Form1.ShowDialog() = Windows.Forms.DialogResult.OK Then
   ....
End if

However when you do this in your Form1 you will need to specify the Dialog Result with something like this:

  Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    Me.Close()
    Me.DialogResult = Windows.Forms.DialogResult.OK
  End Sub

  Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    Me.Close()
    Me.DialogResult = Windows.Forms.DialogResult.Cancel
  End Sub

More info (barely) can be found here : http://msdn.microsoft.com/en-us/library/ms745820.aspx


Wednesday, June 17, 2009

Read in a tab delimited file in vb.net

If its a comma delimited file you can change ControlChars.Tab to ","
        Dim fields As String()
        Dim delimiter As String = ControlChars.Tab
        Using parser As New TextFieldParser(MultipleInputFile)
          parser.SetDelimiters(delimiter)
          While Not parser.EndOfData
            ' Read in the fields for the current line
            fields = parser.ReadFields()
            ' Add code here to use data in fields variable.
          End While
        End Using

How to determine if the Mouse is over a Control that is within another Control

You could so something in the MouseMove, MouseEnter, MouseLeave events, but I like this solution below. It uses a Timer and the on the Timer Click events it can determine if a control has Focus. In the situation below, I have two controls that are within a splitcontainer. when I mouse over the control, I want that control to have focus. For some reason it wasn't happening, and this code below takes care of that situation. I also had to use the PointToScreen call because the Controls are within the splitContainer and I wasn't getting the proper position otherwise.

Private WithEvents mTimer As Timer
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
mTimer = New Timer()
mTimer.Interval = 200
mTimer.Enabled = True
End Sub

Private Sub mTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles mTimer.Tick
Dim CursorPosition As Point = PointToScreen(Cursor.Position)
Dim Control1Bounds As New Rectangle(ImageContainer1.PointToScreen(Control1.Location), Control1.Size)
Dim Control2Bounds As New Rectangle(ImageContainer2.PointToScreen(Control2.Location), Control1.Size)

If Control1Bounds.Contains(CursorPosition) Then
Control1.Focus()
ElseIf Control2Bounds.Contains(CursorPosition) Then
Control1.Focus()
End If
End Sub

Detect Active Control on a Form

You can use the Me.ActiveControl property, additionally if a control is withing another control you can call this property from the control.  For Example if you have a SplitContainer which has another control in it you can do the following:

If Me.ActiveContainer Is SplitContainer1 Then
   If SplitContainer1.ActiveControl Is MyControl1 Then
        Do Something