Monday, February 23, 2009

Adjust the dropdownheight of a ComboBox automatically in VB.NET

I would like to adjust the size of a combobox to fit all of the items in it automatically.  One thing I noticed when testing it was if I set the dropdownheight to some large number, it automatically adjusted it to the size at runtime.  For example, if you combobox when fully filled out would need to be sized at say 200 pixels, but when you Designer you set the dropdownheight to 1000 when you run the program the combobox will only be sized at 200 pixels.  It won't create an enormously large combobox at 1000 pixels.

However the above solution just didn't seem right and I would like to have something where I wasn't hardcoding a number.  I came across this blog post below:
http://rajeshkm.blogspot.com/2006/11/adjust-combobox-drop-down-list-width-c.html

Basically he does what I want, but does it for the width of a combobox.  I converted the code to VB.NET and created a function that sets the Height as well.  The converted code and the modified function to adjust the height is below:

To call it just pass the name of the combobox to the routines

  Public Shared Sub SetComboScrollWidth(ByVal sender As Object)
    Try
      Dim senderComboBox As ComboBox = DirectCast(sender, ComboBox)
      Dim width As Integer = senderComboBox.Width
      Dim g As Graphics = senderComboBox.CreateGraphics()
      Dim font As Font = senderComboBox.Font

      'checks if a scrollbar will be displayed.
      'If yes, then get its width to adjust the size of the drop down list.
      Dim vertScrollBarWidth As Integer = IIf((senderComboBox.Items.Count > senderComboBox.MaxDropDownItems), SystemInformation.VerticalScrollBarWidth, 0)

      'Loop through list items and check size of each items.
      'set the width of the drop down list to the width of the largest item.

      Dim newWidth As Integer
      For Each s As String In DirectCast(sender, ComboBox).Items
        If s IsNot Nothing Then
          newWidth = CInt(g.MeasureString(s.Trim(), font).Width) + vertScrollBarWidth
          If width < newWidth Then
            width = newWidth
          End If
        End If
      Next
      senderComboBox.DropDownWidth = width
    Catch objException As Exception
      'Catch objException
    End Try
  End Sub

  Public Shared Sub SetComboScrollHeight(ByVal sender As Object)
    Try
      Dim senderComboBox As ComboBox = DirectCast(sender, ComboBox)
      Dim cboheight As Integer = senderComboBox.Height
      Dim g As Graphics = senderComboBox.CreateGraphics()
      Dim font As Font = senderComboBox.Font

      'Loop through list items and check size of each items.
      'increment the newheight to be the size of the items
      Dim newHeight As Integer = cboheight
      For Each s As String In DirectCast(sender, ComboBox).Items
        If s IsNot Nothing Then
          newHeight += CInt(g.MeasureString(s.Trim(), font).Height) '+ vertScrollBarWidth
        End If
      Next
      senderComboBox.DropDownHeight = newHeight
    Catch objException As Exception
      'Catch objException
    End Try
  End Sub

Wednesday, February 18, 2009

Select Block Text in Visual Studio

To select block, hold down the ALT key while dragging the cursor in the code window.  This way you can select a block of text rather than line by line.


Power Packs

I'm a little slow and had no idea that microsoft had released these power packs.  One great tool in there is the line and rectangle controls.  In my old vb code we had these line controls and I now draw them by hand.  I wish I had these power packs when I was doing the conversion.  Also the printing is made easier as I had to redo our entire printing when doing the conversion.

http://msdn.microsoft.com/en-us/vbasic/ms789080.aspx


Great explanation of Refresh vs Invalidate vs Update or using all three

http://www.vbforums.com/showthread.php?t=426684

Friday, February 13, 2009

Get the height and width of a polygon or a list of points in array in vb.net

When drawing a polygon, I place the points in an array and then draw it out.  However what if you want to know the height and width of your polygon.  One solution is to create a new GraphicsPath object from your array and then use the GetBounds.Width and GetBounds.Height call to get it.

           Dim apolygon As New GraphicsPath
           apolygon.AddPolygon(m_Points)
           If (apolygon.GetBounds.Width >= 10 AndAlso apolygon.GetBounds.Height >= 10) Then
                             do something
m_Points is an array of points

Thursday, February 5, 2009

Cannot have an enum of strings

When you try to do the following

Public Enum Enum_Type as String
  VALUE1 = "Value1"
  VALUE2 = "Value2"
End Enum

I get some error that the enum must be of integral type.

My work around was to do the following (nothing elaborate here):
Public CONST VALUE1 = "Value1"
Public CONST VALUE2 = "Value2"

And then I just accessed the values the same way I would have from the enum

-- Ted

Wednesday, February 4, 2009

Things you can do with the find box - mini buffer

http://blogs.msdn.com/saraford/archive/2007/11/26/did-you-know-how-to-have-fun-with-the-find-combo-box.aspx

  • Goto a line – type the line number and press Ctrl+G (i like showing this off as how you can do a "go to line" without popping up the go to dialog box) 
  • Goto a file – type the name of the file (either in your project or on the INCLUDE path)and press Ctrl+Shift+G
  • Set a breakpoint on a function – type the name of the function and press F9
  • Get help – type the keyword and press F1


Did you know... How to dock the Find and Replace window? - #076

http://blogs.msdn.com/saraford/archive/2007/11/05/did-you-know-how-to-dock-the-find-and-replace-window.aspx

Monday, February 2, 2009

a problem has occurred in retrieving the digital rights management machine identification

I got a new machine and when I tried to download a audiobook from Overdrive I got an error similar to the one mentioned above.  I ended up doing a number of things which may or may not have worked.  Below is that I did:

I followed the directions found here - http://www.microsoft.com/windows/windowsmedia/player/webhelp/default.aspx?&mpver=11.0.5721.5230&id=C00D11E2&contextid=71&originalid=C00D2781
Basically they ask you to reinstall Windows Media Player.  But when uninstalling it you need to remove about 3 other programs, and restart the computer.  Then you need to delete whatever is in your drm folder and then again restart the computer.  However that didn't work and I still got the error.

What I then did was in Overdrive, I went to Tools->Windows Media Player Security Update.  But I got an error when trying to do this.  So what I ended up doing was renamed the folder "C:\Documents and Settings\All Users\DRM" to "C:\Documents and Settings\All Users\DRM.old" then I tried the Tools->Windows Media Player Security Update again, and it worked.  One thing with the renaming of the folder is that you need to "Show all files" which is an options in Windows Explorer.

I think the second step is really what I needed to do and reinstalling Windows Media Player was just a waste of time.

-- Ted

Firefox tweak for Sharepoint

Whenever I have to go to the know or to a teamsite our our sharepoint server, I get this dialog box which prompts me to enter a username and password in firefox.  I don't get this in IE.   The fix in firefox is this:

1. Open Firefox

2. Navigate to the url about:config

3. Locate the following preference names and put as the value the comma separated values of the address roots.

network.automatic-ntlm-auth.trusted-uris

network.negotiate-auth.delegation-uris

network.negotiate-auth.trusted-uris

Your value should look something like this: localhost,noblis.org

-- Ted

Sorting Algorithms

This one will take only a minute, but it's a great way to graphically see the differences between sorting algorithms.

 

http://www.cs.rit.edu/~atk/Java/Sorting/sorting.html