XTab's Blog

Ged Mead's Blog at vbCity

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

OctNovember 2005Dec
SMTWTFS
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

Archives

Topics

Ramblings

VB.NET

Wednesday, November 02, 2005 #

   It looks like the beginning of the end for one of  my  most popular articles on devCity (at least, most popular, according to the number of hits and the general positive feedback, that is ).

 

  It’s the one that deals with Multiple Forms in VB.NET.  http://www.devcity.net/net/article.aspx?alias=multipleforms 

 

This particular  article helps to solve that first day problem that many of us have when moving from VB6 to VB.NET.  That is the sudden sinking realisation that you simply don’t know how to show a new Windows Form any more.  :-{

 

   In VB.NET prior to 2005, the tried and trusted VB6 code of  :

Form2.Show

  Will only reward you with the soon-to-be-hated wiggly blue line.

 

    Of course, like so many things programming related, the answer – once you know it – is simplicity itself.   But it’s something that has caused a lot of grief, frustration and general gnashing of teeth for upgraders since VB.NET first hit the streets.  

 

   The good news then for current VB6-ers  is that with the release of VB2005 (soon to be known simply as “VB”  - now won’t that cause a cartload of confusion in the forums for the next few months?) this particular stumbling block to happy upgrading has been removed.

 

  The default Windows Form instance is back!   If you really, really don’t want to create your own first instance of a form then you will be able to use the VB6 style syntax, as shown above, to do it for you.

 

   Whether you should, of course, is a different question and I look forward to reading many long and heated discussions, arguments and flames appearing on the VB Forums in the coming months.  

  

   So it looks as though that my article will become almost totally redundant.  At least for VB2005 users.     In the meantime, I suppose I’d better find some replacement VB2005 (Excuse me!   VB) stumbling blocks to write about in future devCity articles.     Although from my experimenting with the latest version to date, this might actually be a more difficult challenge.    It’s a much improved version from the 2002 and 2003 offerings in many ways, they seem to have taken on board all the major complaints of upgraders/potential upgraders and fixed them.   Add that to the already existing benefits of .NET and I reckon  it’s going to be even harder to resist the change now.

posted @ 1:21 PM

  Here’s a nifty little tip that you may possibly find useful one day .   I had to work out how to display superscript and subscript characters using the DrawString method.   Now, Drawstring itself is a fairly easy beast to tame once you’ve done it a couple of times, but the extra requirement of  the super and subscript digits needed an extra bit of thought.

 

   Anyhow, here’s how I went about it:

 

I created two separate fonts, one for the standard size/position text and one for the superscript:

 

 

Code Copy HideScrollFull
'  A font for the standard size text:
     Dim fnt As Font = New Font("Verdana", 13, FontStyle.Regular)
'  A smaller font for the superscript part

     Dim smallfnt As New Font("Verdana", 8, FontStyle.Regular)
. . .

Then we need a brush to write with and somewhere to write.   In this case I was drawing the string on a label named Label1:

 

Code Copy HideScrollFull
'  A Brush to write the text:
        Dim b As Brush = New SolidBrush(Color.Black)
        '  Graphics object
        Dim lg As Graphics = Label1.CreateGraphics

. . .

For code clarity and reusability, I created variables to hold the standard text and the superscript separately:

 

Code Copy HideScrollFull
'  Text variables and values
        Dim Name As String = "XY"
        Dim Number As String = "3"

. . .

Now we can calculate the width of the Name part of the display. 

 

Why do we need this?   So that - after we have drawn the Name part - we know exactly where the Number (the Superscript bit) should be drawn.

 

Code Copy HideScrollFull
'  Calculate the size of the Name part
        Dim TextSize As New SizeF(lg.MeasureString(Name, fnt))
        '  and note the width
        Dim wordwidth As Integer = CInt(TextSize.Width)


. . .

The next step is to draw the Name part:

 

 

Code Copy HideScrollFull
' Ensure that label is empty before we start:
        lg.Clear(Color.White)
        '  Draw the name
        lg.DrawString(Name, fnt, b, 10, 10)


. . .

You can change the start point of the text (currently location 10, 10) on the label to suit your particular requirements.

 

    The superscript text can now be drawn at a location which is 5 pixels to the right of the Name text and 4 pixels higher.   We use the smaller font for this.

 

Code Copy HideScrollFull
'  Now you can calculate where the superscript part should be ...
        '   and draw it
        lg.DrawString(Number, smallfnt, b, wordwidth + 5, 6)



 Finally, the standard housekeeping to dispose of finished objects:

 

Code Copy HideScrollFull
'  Housekeeping:
        b.Dispose()
        lg.Dispose()




. . .

 

Here’s the above code all in one lump, if you want to try it out:

 

 

Code Copy HideScrollFull
'  A font for the standard size text:
        Dim fnt As Font = New Font("Verdana", 13, FontStyle.Regular)
        '  A smaller font for the superscript part
        Dim smallfnt As New Font("Verdana", 8, FontStyle.Regular)

        '  A Brush to write the text:
        Dim b As Brush = New SolidBrush(Color.Black)
        '  Graphics object
        Dim lg As Graphics = Label1.CreateGraphics

        '  Text variables and values
        Dim Name As String = "XY"
        Dim Number As String = "3"

        '  Calculate the size of the name part
        Dim TextSize As New SizeF(lg.MeasureString(Name, fnt))
        '  and identify the width it will occupy
        Dim wordwidth As Integer = CInt(TextSize.Width)

        ' Ensure that label is empty before we start:
        lg.Clear(Color.White)
        '  Draw the name
        lg.DrawString(Name, fnt, b, 10, 10)

        '  Now you can calculate where the superscript part should be ...
        '   and draw it
        lg.DrawString(Number, smallfnt, b, wordwidth + 5, 6)

        '  Housekeeping:
        b.Dispose()
        lg.Dispose()





. . .

  I think that if I was going to use this  a lot, I would tweak it further by:

a.   Creating variables for the x and y positions, in place of the hard coded numbers I’ve used here.

b.   Turning it into a procedure (it’s currently in a button click event), passing in the various values needed, so increasing the reusability of the thing.

posted @ 1:15 PM