Software Expressions

.Net / Software / Visual Studio / TFS / other ramblings
 

Goto

Categories

 

Archive for November, 2008

11 18th, 2008

Author: Troy

I am always dazzled with your consistency and your practical approach to issues. You surely must be brilliant for being able to create the architecture we have - QA Lead

11 14th, 2008

Button theory

Author: Troy

Buttons on Windows applications have been around since the first version of Windows. One would think that the patterns for using buttons would have been firmly established by now. In particular rules for disabling and enabling buttons.
Concerning visibility:

  • Buttons should never be made invisible based on some transient state. i.e. Don’t hide the button when the user is disconnected from the internet, or hide the button when the order has already been placed.
  • Users should expect to see buttons in the same order and same location for a given context.
  • Hiding buttons is good for things like user permissions. If you don’t have the permission to delete the customer information, there’s no need to see the button ever.
  • Concerning enable and disable:

  • If a button is no longer available due to a transient state, or state of the underlying object, disable it. This keeps the screens and button layout consistent.
  • Don’t disable the button without giving the user a reason. This may seem strange, but you can display a tooltip on a disabled button. Let the user know why they’re not able to perform the action. Don’t leave them guessing.
  • I suppose there could be exceptions to this. In some cases it may really be obvious.
  • If it’s not really obvious and you can’t display a tooltip on a disabled button, leave the button enabled and display a pop-up message indicating why.
  • The more rules there are that control whether or not the button is available, the more reasons there are to clearly communicate this to the user.
  • 11 14th, 2008

    Serializing images

    Author: Troy

    The System.Drawing.Image class is not serializable by default. This class can be used to serialize images as well as load the image from the IDataRecord.

       1:  Imports System.Drawing
       2:  Imports System.Runtime.Serialization
       3:  Imports System.Drawing.Imaging
       4:  Imports System.IO
       5:   
       6:  <Serializable()> _
       7:  Public Class SerializableImage
       8:      Implements ISerializable
       9:   
      10:      Private _master As MasterEnum = MasterEnum.Image
      11:      Private _imageBuffer As Byte()
      12:      Private _image As Image
      13:      Private _serializationFormat As ImageFormat = ImageFormat.Png
      14:   
      15:      Public Property Image() As Image
      16:          Get
      17:              If _master <> MasterEnum.Image Then
      18:                  CreateImageFromBuffer()
      19:              End If
      20:              Return _image
      21:          End Get
      22:          Set(ByVal value As Image)
      23:              If (value Is _image) Then Return
      24:              _image = value
      25:              ‘The image becomes the master once the setter is used
      26:              _master = MasterEnum.Image
      27:              _imageBuffer = Nothing
      28:          End Set
      29:      End Property
      30:   
      31:      Private Sub CreateImageFromBuffer()
      32:          ‘Once this method is called, the image becomes the master
      33:          _master = MasterEnum.Image
      34:          ‘Check if there is a buffer
      35:          If _imageBuffer Is Nothing OrElse _imageBuffer.Length = 0 Then
      36:              Image = Nothing
      37:          Else
      38:              ‘http://support.microsoft.com/?id=814675
      39:              Using s As New MemoryStream(_imageBuffer)
      40:                  If Not s Is System.IO.Stream.Null Then
      41:                      Dim tempImage As Image = _
      42:                          DirectCast(Bitmap.FromStream(s), Bitmap)
      43:                      Image = New Bitmap(tempImage)
      44:                      Dim g As Graphics = Graphics.FromImage(tempImage)
      45:                      g.DrawImage(Image, New PointF(0, 0))
      46:                      g.Dispose()
      47:                      tempImage.Dispose()
      48:                  End If
      49:                  s.Close()
      50:              End Using
      51:          End If
      52:      End Sub
      53:   
      54:      Private Property ImageBuffer() As Byte()
      55:          Get
      56:              Select Case _master
      57:                  Case MasterEnum.Image
      58:                      ‘Used for serialization
      59:                      Return CreateBufferFromImage()
      60:                  Case MasterEnum.Buffer
      61:                      Return _imageBuffer
      62:              End Select
      63:              Return _imageBuffer
      64:          End Get
      65:          Set(ByVal value As Byte())
      66:              If (value Is _imageBuffer) Then Return
      67:              _imageBuffer = value
      68:              ‘The Buffer becomes the master
      69:              _master = MasterEnum.Buffer
      70:              _image = Nothing
      71:          End Set
      72:      End Property
      73:   
      74:      Private Function CreateBufferFromImage() As Byte()
      75:          If _image Is Nothing Then Return Nothing
      76:          ‘Convert the image to a byte array using the serialization format
      77:          Using stream As New MemoryStream
      78:              _image.Save(stream, SerializationFormat)
      79:              Dim result As Byte() = stream.ToArray()
      80:              stream.Close()
      81:              Return result
      82:          End Using
      83:      End Function
      84:   
      85:      Public Property SerializationFormat() As ImageFormat
      86:          Get
      87:              Return _serializationFormat
      88:          End Get
      89:          Set(ByVal value As ImageFormat)
      90:              _serializationFormat = value
      91:          End Set
      92:      End Property
      93:   
      94:      Public Sub New()
      95:   
      96:      End Sub
      97:   
      98:      Public Sub New(ByVal image As Image)
      99:          Me.Image = image
     100:      End Sub
     101:   
     102:      Public Sub New(ByVal record As IDataRecord, ByVal fieldIndex As Integer)
     103:          Load(record, fieldIndex)
     104:      End Sub
     105:   
     106:      Public Sub New(ByVal record As IDataRecord, ByVal fieldName As String)
     107:          Load(record, fieldName)
     108:      End Sub
     109:   
     110:      Private Sub Load(ByVal record As IDataRecord, ByVal fieldName As String)
     111:          If record Is Nothing Then Throw New ArgumentNullException(“record”)
     112:          Load(record, record.GetOrdinal(fieldName))
     113:      End Sub
     114:   
     115:      Private Sub Load(ByVal record As IDataRecord, ByVal fieldIndex As Integer)
     116:          If record Is Nothing Then Throw New ArgumentNullException(“record”)
     117:          If record.IsDBNull(fieldIndex) Then Return
     118:          ImageBuffer = DirectCast(record.GetValue(fieldIndex), Byte())
     119:      End Sub
     120:   
     121:      Public Sub New(ByVal info As SerializationInfo, _
     122:          ByVal context As StreamingContext)
     123:          ‘Image
     124:          Dim data As Byte() = _
     125:              DirectCast(info.GetValue(“image”, GetType(Byte())), Byte())
     126:          If data IsNot Nothing AndAlso data.Length > 0 Then
     127:              ‘http://support.microsoft.com/?id=814675
     128:              Using s As New MemoryStream(data)
     129:                  If Not s Is System.IO.Stream.Null Then
     130:                      Dim tempImage As Image = _
     131:                          DirectCast(Bitmap.FromStream(s), Bitmap)
     132:                      Image = New Bitmap(tempImage)
     133:                      Dim g As Graphics = Graphics.FromImage(tempImage)
     134:                      g.DrawImage(Image, New PointF(0, 0))
     135:                      g.Dispose()
     136:                      tempImage.Dispose()
     137:                  End If
     138:                  s.Close()
     139:              End Using
     140:          End If
     141:      End Sub
     142:   
     143:      Public Sub GetObjectData(ByVal info As SerializationInfo, +
     144:          ByVal context As StreamingContext) Implements ISerializable.GetObjectData
     145:          info.AddValue(“image”, ImageBuffer, GetType(Byte()))
     146:      End Sub
     147:   
     148:      Private Enum MasterEnum
     149:          Buffer
     150:          Image
     151:      End Enum
     152:   
     153:  End Class

    Some friends were experiencing problems with a virus (run32dll - possibly WhisperB-worm), while searching for the best approach to resolve this issue I came across this really simple and impressive article. I guess I’m rather old-school when it comes to viruses and haven’t dealt much with them for many years. Back in the DOS days you would create a boot disk with your virus software. You would boot up on the clean boot disk, and run the scan on C:. There was nothing else to it. It seems to be rather more complicated these days - and I’m not sure it needs to be. For one thing, how can you download virus scanning software and run it on a machine with a virus? How can you run a Windows virus scanning software on a machine where when Windows starts up it already has the virus loaded. This was evidently the problem in this case because the virus scanners that were downloaded picked up no viruses. Indeed the virus software had cleverly intercepted the scanner. It was obvious because during the scan the machine would reboot. The scanning software couldn’t even download all it’s updated virus signatures…
    http://askthegeek.kennyhart.com/2005/12/how-to-make-bootable-thumb-drive-virus.html