Archives
-
Lack of updates...
Haven't updated in a while. My time has been spent in tech design at work (not much coding) and enjoying the holidays with my family. Hopefully, everyone else is having a good time too!
-
Unit Testing Abstract classes
Opinions wanted: How do you write unit tests for abstract (MustInherit in VB.NET) classes? Do you derive a simple class off your abstract class in your unit test assembly? Or do you just write unit tests for the derived classes? Hmmm.....
-
Find all SQL Identity columns
I had a need today to find all columns in a SQL database that were identity columns (across all tables). Thanks to SQL's system tables, it was a breeze:
-
Technology Backlash!
I don't know what's going on here. First, I have a hard-drive crash that takes a bunch of data with it. I actually took it to one of those data recovery places. It was $95 to look it over, but then it would be anywhere from $1300 - $2500 to fix it. Yikes! No way I could justify spending that kind of money. But I forked over the $95 because I was curious as to what went wrong. A few days later they called with the verdict: It's dead. Totally dead. No recovery possible -- which they said is rare these days to see this kind of crash. At least this way, I don't need to agonize over the decision that spending the money could recover the data...
-
Weather Alerts
From heLP .Net Blog:
-
New .NET Aggregator
Haven't fired up SharpReader the whole weekend so I don't know if anyone else has blogged this, but there's a new .NET-based aggregator that integrates with Outlook 2003/XP (like Newsgator). This one is free and I think I'll try it out to see how I like it. It's called intraVnews.
-
SpamBayes Follow-up
Not 5 seconds after posting about my experience with SpamBayes, SharpReader finishes updating and shows me that Steve Smith likes it too. But the titles of both of our posts bear a striking resemblance -- I promise: It's pure conincedence!
-
SpamBayes Rocks!
Based on positive comments from someone else (can't find the link right now), I downloaded and installed SpamBayes for Outlook. Two words: It rocks! It's easy to install and get running. One thing to note if you're considering using this for SPAM: It needs to be trained on what is SPAM and what isn't. If you think you *might* try SpamBayes some day, then save your SPAM in a folder. When (if) you ever do try SpamBayes, you can point it to that folder and it will help the training process go much quicker.
-
DVD Burner
I've been researching DVD RW drives for quite a while now. Waiting for the price to hit the right spot. A number of $130 - $150 range brands have been showing up in the weekend ads (CompUSA, BestBuy, OfficeDepot, etc...) I had been eyeing the I/O Magic for a while. I had found a few positive reviews on the drive across the Internet and Circuit City had them on sale this week for $139.
-
Plink!
We've all read about this. Maybe even it's happened to you. The hard drive crash. I got my first 10MB hard-drive back in the "old" days (1990?) and hooked it up to my Atari ST. In the past 13 years I've only had one hard drive go bad, and that was simply an over-heating problem. Maxtor promptly sent me a new drive out and I've been good ever since. Until this past weekend...
-
No COM+ Statistics?
For an exercise, I whipped up a simple little object that would be poolable in COM+
-
GANG Meeting Tonight
GANG (Great Lakes Dot Net Users Group) is having their monthly meeting tonight. Richard Hale Shaw of The Richard Hale Shaw Group will be presenting "Utilizing Code Access Security". Should be a good one! The meeting starts at 6:30pm at Microsoft's Southfield, MI office.
-
Perfect Software
Julia is looking for perfection in her designs. Designing the "perfect" system is difficult. But when you design software for the space shuttle, you need to get as close to perfect as possible:
-
Be careful of your conditionals!
Doug Reilly posts about C#'s conditional operator in response to Rachel's post on VB.NET's IIf() statement.
-
The Bitmap class is NOT a paint program
I needed to convert a bitmap to an icon. After discovering Paint Shop Pro didn't support this (seemed odd to me), I thought I'd just quickly write up a three line .NET app to convert a bitmap to an icon.
-
A "Don't Miss!" Blog
Zane Thomas from Abderaware has started a blog. As Zane comments: "I am all about challenging assumptions". This blog should be interesting.
-
A 21st century solution...
Some programmer humor for this morning:
-
Initializing non-serialized fields
Matthew Reynolds has a nice tip on how to cleanly handle initialization of non-serialized fields during the de-serialization process.
-
Lego Mindstorms + .NET
From CLaueR's Blog:
-
The existential mouse
Raymond Chen answers the question: If a mouse moves and nobody receives the message, did it really move?
-
Ignoring Errors? Not if they're an Exception!
Brad Abram points out a number of reasons why Exceptions are better than error codes. One that I liked in particular:
-
CSS Preview
This is normal text.
-
Top Blogger.
I've enjoyed every single one of Raymond Chen's posts. His blog is one of the most informative and interesting -- and they make great history lessons! Kudos to Raymond!
-
What a coincedence!
Just last night, I made a post about my journey into full-time C# use. This morning, I see Eric Gunnerson has mentioned a new magazine called "C# Pro Magazine". I headed over there to check it out and what do I see -- an article titled "5 C# Tips for VB Developers".
-
Sharpening my skills.
I've been a VB programmer for a long time. Therefore, I took to VB.NET pretty easily. I can pound out code pretty quickly in VB.NET, and I'm familiar enough with C# syntax that I can read C# code fluently. But the past year at my current job has involved a little bit of Java coding. Curly braces and semi-colons don't scare me, although I'm not a big fan of case-sensitivity. I've done a little bit of C# coding here and there, but I mainly use VB.NET.
-
I hate to repeat myself, but...
-
Keeping your Enums and Lookup tables in sync
Steve Smith has developed a unit test to make sure your .NET enums (used during coding) match up wth their corresponding lookup tables (used for RI checking). Nice safety net.
-
Converting GMT dates
I have a DB2 database giving me a date in GMT format and I need to convert it to my local timezone. The .NET TimeZone class makes this sooooo easy:
-
Microsoft will *NOT* send you a critical update.
Most of you reading this post already know this, but Microsoft will not send you a personal email with a critical update. I've been flooded with about 20 of these today. They are very authentic looking -- they resemble the Microsoft home page, same layout, fonts, trademark and copyright notices, the works. But the attachment is just another virus/worm.
-
GANG Meeting
Had a great GANG meeting tonight. Bob Crosley from Wise Solutions discussed "Deployment nightmares: How poor software deployments destroy schedules, projects and your customer's computer". He also demo'd Wise for Visual Studio.NET and it looked slick! It integrates directly into the IDE as a new project type and has a ton of nice point-and-click options. It also supports some complex stuff like database installs, virtual directory creation, GAC install and even nGen on the client. Nice.
-
Have you even done unit testing?
Roy challenges you to ask the developer next to you: Have you ever done unit testing?
-
The next VB.NET
David McNamee has a great preview of some of the changes coming in the Whibey version of VB.NET. It's great to see some of these things now being demo'd to the public. I've seen many of the things he posted about previously, but was under NDA and couldn't discuss. Since it's now been shown publically, I'd like to add a quick comment to one of David's observations on aligning controls:
-
Interesting...
From a DOTNET-CLR post: An interesting feature of MSIL that is not currently exposed in any of the languages -- "method overload based on return type only." Very interesting!
-
A new security bulletin!
This just in: Buffer Overrun In RPCSS Service Could Allow Code Execution
-
Coding Styles when Dim'ing an object
From a newsgroup post: What's the difference between these three declarations:
-
Have you measured it?
A neat tidbit on enums from Brad Abrams.
-
Withdrawl...
Only two postings in August? Ugh... I've been so busy with work (non-.NET stuff...) and home stuff. Haven't done any .NET stuff in a few weeks. I am jonesing to do some .NET stuff. Need to find more time in the day...
-
VB.NET Features creep into C#
From Jeff Key's Blog:
-
The work we produce
A nice snippet from Scoble:
You know what's different about Microsoft? S&&t doesn't fly here. You try to put lipstick on a pig and people will call you on it. That hasn't always happened elsewhere in my career.
*sigh*... I wish more tech companies worked this way. Does anyone still wonder why Microsoft is #1? -
Turn off 'Hide Advanced Members'!
I'm not sure why this is a default option for VB.NET, but if you look at VS.NET's options for VB.NET (Tools, Options, Text Editor, Basic) there's an entry "Hide Advanced Members". It's turned on by default. According to the docs:
-
Another Kudos!
I don't want to sound like a broken record (since many others have praised Scott and his work), but the new interface is really nice! I still miss my WYSIWYG preview from Bloggar, but this is a great improvement. And I love the new styles! Thanks Scott!
-
Inheriting Constructors
An interesting thread on inheriting constructors, which .NET -- along with Java and C++ -- don't do. If you want all the same constructors as your base class, you need to add code for them.
-
Refactoring in .NET
Brenton House is asking about refactoring tools for VS.NET. I've also used Eclipse in the past and it's refactoring tools are nice. I really miss them in VS.NET. Of the links he posted, Xtreme Simplicity looks pretty nice. I'll be following the comments on his post closely.
-
FxCop and CLS Compliance
After finding out that VB.NET does not enforce CLS compliance (even with the CLSCompliant attribute set to True), I thought I could use FxCop to check this. Although ServerGeek said that FxCop complains about non-CLS compliance, I did not see such behavior. I created a simple, non-CLS compliant VB.NET assembly:
-
NGEN *still* requires the IL.
While many people who are "into" .NET may already know this, I still see this come up in the Microsoft newsgroups from time to time. People read a little about NGEN and how it produces a native executable and they think they're in the clear (i.e. don't need to worry about the .NET framework or ILDASM). This is not true. Eric Gunnerson pointed this out in a newsgroup post a couple of years ago. It bears repeating:
-
VB.NET and DirectCast
Paul Vick: "...as it turns out, there are some narrow situations where using DirectCast can make things a tiny bit fast than they might otherwise be."
-
MS Tech Lead on VB.NET is blogging.
Scoble sends word that Paul Vick, MS Technical Lead on Visual Basic.NET now has a blog. Subscribed!
-
VB6, Variants and COM-Interop
Ran into an interesting issue with COM-interop and Variants. In VB6, I had a "Value" Property that was of type 'Variant'. While I loathe variants, it really was required for this project since it could hold all sorts of data. Since it could hold anything, this "Value" property had a "Property Get", "Property Set" and "Property Let". I needed to use this interface in a .NET class so I used TLBIMP.EXE and get a runtime-callable-wrapper (RCW).
-
.NET Success
Jason Alexander blogs about his success in moving an old ASP site to .NET:
-
For/Each and Try/Finally
For some odd reason, an idea popped into my head based on my post about the Exit Try issue. It's bad idea -- a hack. Yet I couldn't help but let it run it's course.
-
Automatic calling of "Dispose" in VB.NET
For some odd reason, an idea popped into my head based on my post about the Exit Try issue. It's bad idea -- a hack. Yet I couldn't help but let it run it's course.
-
Update on Exit Try
Just wanted to clarify a few things based on comments to my original post on the "Exit Try" issue:
-
Beware "Exit Try"!
Brad Abrams' recent post about the IL code generated by the C# compiler reminded me of an issue that someone found with the VB.NET "Exit Try" statement. I thought I'd post it here to make sure all three of my subscribers know about this... :)
-
Case-sensitive websites
I've never understood why you would want a case-sensitive webserver. ScotG's blog recently mentioned his purchase of an iSight. Since I don't follow Apple hardware (nor Apple in general) I clicked on his link to the iSight, http://www.apple.com/iSight. I got to an Apple page about it being an invalid link. So I went to Apple's product pages and saw a link to the iSight. I clicked on the linked and when it popped up in my browser, the URL looked like the same one ScotG had in his blog. I went back and double-checked his blog entry: iSight vs. isight.
-
On Code Generators.
AngryCoder: "...the myth of the code generator."
-
Unit Testing with JUnit
No -- that's not a typo: "JUnit" (not NUnit). At my current job (which is usually VB6/SQL/Oracle development) I've had to do some Java coding. For someone familiar with .NET, it's not too bad.
-
Sam, COM+ and .NET
Sam Gentile: This just rocks! COM+, like all other things in .NET, is just so much easier to write.
-
More on multiple attributes
Mattew Reynolds adds another tidbit on Attributes:
-
Applying an attribute multiple times
I had designed a custom attribute for fields and wanted to be able to apply it multiple times. For example:
-
Martin Fowler on Typed Collections
From Martin Fowler:
-
Windows Forms Controls chat
Just saw this post in the Microsoft supports newsgroups:
-
VB6 in .NET?
-
Let go of my assembly!
As noted earlier, I'm using FxCop for the first time today. So I've got FxCop up with it's results, I'm doing ALT-TAB back and forth to clean up some of the easier things. Now I want to re-build and re-analyze but FxCop has my assembly locked. No biggie, I'll start up a new FxCop project and re-build. Still locked. I had to shut down FxCop before I could rebuild.
-
VB.NET and FxCop 1.21
I played around with FxCop for the first time today -- very cool app! One thing that is very annoying for VB.NET developers: If you turn on automatic "code reformatting" in VS.NET, the "Set" clause of a Property statement is generated as:
-
Welcome Eric Gunnerson
Sam Gentile sends word that C# Program Manager Eric Gunnerson is now blogging! This should be a great source of information for .NET developers.
-
VB.NET vs. C#
No -- I'm not starting another language war. I was converting some C# code to VB.NET today. After getting everything converted, I had to change the name of the class (nothing to do with the conversion). I changed it and then thought "shoot -- now I need to change all of the constuctors" (it has three overloaded constructors). Then I remembered I'm in VB.NET and all of the constructors are named "New" -- no changes required.
-
GANG Meeting tonight
The Great Lakes Area .NET User Group (G.A.N.G) is having their monthly meeting tonight.
-
Posting with w.bloggar!
Got w.bloggar set up. Thanks go to Steve Smith for his recent post on w.bloggar setup. The real nice thing with w.bloggar is the preview window. Since it allows a lot of customization, I've got (pretty much) a 100% WYSIWYG preview of my weblogs.asp.net postings.
-
VB.NET documentation bug with 'Throw'
I've notified Microsoft of a small bug in the VB.NET documentation. For the 'Throw Statement', the documentation says the expression part of the statement is required:
-
Oracle and ADO.NET
Sam has posted a great list of resources on ADO.NET with Oracle.
-
A little off topic...
Apologies for the slightly off-topic post. I haven't been posting much because I've been home helping my wife out with our newborn! She's a beauty and her older sisters adore her.
-
Passing Delegates to Unmanaged Code
Adam Nathan writes about the issues with passing delegates to unmanaged code. Namely: it can get garbage collected right out from under the unmanaged code:
-
VS.NET and editing Resx files
Am I missing something or is it really true that the built-in resource editor in VS.NET only supports strings? I can't find anyway to put binary files (for example, a bitmap) into a ResX in VS.NET's resource editor. Thankfully, there's Lutz Roeder's Resourcer.
-
Remote Tracing
Simon Mourier has come up with what looks like a very cool library to trace .NET apps over the network:
-
VB.NET: Under The Covers
Being a curious kind of guy, I decided to look into some of the features unique to VB.NET. A combination of some VB.NET code, ILDASM and Anakrino and my questions were answered!
-
Static Local Variables in VB.NET
VB.NET has support for "local static variables". These are variables local to a method, but retain their method call between invocations of the method. The CLR does not support this, so how does VB.NET do it if it runs under the CLR? Just some simple compiler tricks!
-
Catching Windows messages
I needed to capture mouse clicks before my windows app got a hold of them. Seeing that the Form class has a PreProcessMessage method I thought I was all set. As the documentation stated:
-
Is Ingo Rammer going to Microsoft?
First was Don Box. Then Tim Ewald, Chris Sells and Robert Scoble. Now Ingo Rammer is posting that his first "assignment" will be in mid-August and he'll be in Redmond the last week of June. Hmmm.... Could the remoting guru be Microsoft's next big catch?
-
Beware of the DataSet serialization
From a Visual Studio Magazine HotTip:
-
Frans says goodbye...
... to Hungarian Notation:
-
Do you miss ASPToday.com?
-
Moving a form by clicking anywhere on it.
You've seen applications that allow you to move the form around by simply clicking anywhere on the form (not just the caption bar). Can you do this in .NET? Yes! And it's very easy. All you have to do is handle the proper windows message and the rest is easy.
Option Strict On Option Explicit On
Imports System Imports System.Windows.Forms Imports System.Drawing
Public Class Form1 Inherits Form
Private Declare Function ReleaseCapture Lib "user32" () As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const WM_NCLBUTTONDOWN As Integer = &HA1 Private Const HTCAPTION As Integer = 2
Public Sub New() Me.Text = "Drag anywhere to move" AddHandler Me.MouseDown, AddressOf frmMain_MouseDown End Sub
<STAThread()> _ Shared Sub Main() Application.Run(New Form1()) End Sub
Private Sub frmMain_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) 'Don't drag the sceen if it is the right button or the wheel. If e.Button = MouseButtons.Left Then ReleaseCapture() SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0) End If
End Sub
End Class -
Browsing for a folder (1.0)
Still using the .NET 1.0 framework and need a "browse for folder" dialog? You'll need to resort to platform invoke (p/invoke) using the Win32 API's. Here's a class that encapsulates all of it.
-
Pulling a bitmap off the clipboard.
Here's an example of pulling a bitmap off the clipboard and displaying it in a windows form.
Option Strict On
Option Explicit On
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Public Class WinApp
Inherits System.Windows.Forms.Form
Private m_PB As PictureBox
<STAThread()> _
Shared Sub Main()
Application.Run(New WinApp())
End Sub
Public Sub New()
Me.Text = "This is my form"
m_PB = New PictureBox()
m_PB.Location = New Point(0, 0)
m_PB.Size = Me.Size
m_PB.Image = GetImageFromClipboard()
If Not m_PB.Image Is Nothing Then
Me.ClientSize = New Size(m_PB.Image.Width, m_PB.Image.Height)
m_PB.Size = Me.ClientSize
End If
Me.Controls.Add(m_PB)
End Sub
Public Function GetImageFromClipboard() As Image
If Not Clipboard.GetDataObject() Is Nothing Then
Dim dobj As IDataObject = Clipboard.GetDataObject()
If dobj.GetDataPresent(DataFormats.Bitmap) Then
Dim img_obj As Object = dobj.GetData(DataFormats.Bitmap)
Return CType(img_obj, Bitmap)
End If
End If
End Function
End Class -
Adjusting to life without ItemData
In VB6, the ListBox and ComboBox controls had the ItemData property. This was a “companion” list of long integers which could be used to store additional information related to an Item. For example, you could display a list of individuals in a ListBox and keep their age in the ItemData property:
-
Creating an image from a URL.
This example shows how you can download and create an Image based on a URL.
Option Strict On Option Explicit On
Imports System Imports System.Windows.Forms Imports System.Drawing Imports System.Net Imports System.IO
Public Class WinApp Inherits System.Windows.Forms.Form
<STAThread()> _ Shared Sub Main Application.Run(new WinApp()) End Sub
Dim m_pb As PictureBox
Public Sub New Me.Text = "This is my form"
m_pb = new PictureBox() m_pb.Location = new Point(0,0) m_pb.Image = GetURL("http://radio.weblogs.com/0110109/images/dotnet.gif") m_pb.Size = m_pb.Image.Size Me.ClientSize = m_pb.Image.Size
Me.Controls.add(m_pb) End Sub
Private Function GetURL(url as String) As Bitmap Dim wc As WebClient = new WebClient() Dim strm As Stream = wc.OpenRead(url) Dim bmp As BitMap = new Bitmap(strm)
strm.Close() Return bmp End Function
End Class -
Enumerate Network Adapters
This sample enumerates all of the installed network adapters to reveal such information as MAC address, IP Address, IP Subnet, etc...
'
' base on C# code originally posted by Willy Denoyette
' http://groups.google.com/groups?selm=eeeD%24NrxAHA.2188%40tkmsftngp02
'
Option Strict On
Option Explicit On
Imports System
Imports System.Management
Public Class ConsoleApp
Shared Sub Main
Network.EnumNetworkAdapters()
End Sub
End Class
Public Class Network
Public Shared Sub EnumNetworkAdapters()
Dim query as ManagementObjectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration")
Dim queryCollection as ManagementObjectCollection = query.Get()
Dim mo as ManagementObject
Dim s as String
for each mo in queryCollection
Console.WriteLine( "'{0}'", mo.ClassPath)
Console.WriteLine( "'{0}'", mo.Options)
Console.WriteLine( "Index '{0}'", mo("Index"))
Console.WriteLine( "Description '{0}'", mo("Description"))
Console.WriteLine( "MacAddress '{0}'", mo("MacAddress"))
if(CType(mo("IPEnabled"), Boolean) = true)
dim addresses() as string = CType(mo("IPAddress"), String())
dim subnets() as string = CType(mo("IPSubnet"), String())
Console.WriteLine( "DNS Host '{0}'", mo("DNSHostName"))
Console.WriteLine( "DNS Domain '{0}'", mo("DNSDomain"))
for each s in addresses
Console.WriteLine( "IP Address '{0}'", s)
next
for each s in subnets
Console.WriteLine( "IP Subnet '{0}'", s)
next
end if
next
End Sub
End Class -
OPML Swapping
Something I just noticed: If you've got a bunch of subscriptions in RSS Bandit and want to play around with SharpReader, the OPML format exported by RSS Bandit is not immediately usable by SharpReader. I'm not up on the OPML format so I don't know which program is at fault, but there's a simple fix:
-
Getting the raw HTML from a web control
When run, you should get the following output in the console window:
<INPUT style="HEIGHT: 200px; BACKGROUND-COLOR: red"> -
Using VB.NET Objects in regular ASP Pages
Using a VB.NET object in an ASP page can be accomplished with COM-Interop. While the whole interop mechanism can be complex depending on what you want to do, below is a basic example that should get you started.
1. Create a file called Class1.vb and placed this code in it:Public Class SampleClass Public Function GetMyName() As String Return "This is from the .NET component" End Function End Class
2. Create a keyfile with the strong name (sn) utility:sn -k demokey.snk
3. Compile the VB app with:vbc /t:library /keyfile:demokey.snk class1.vb
4. Install the library into the GAC with:gacutil -i class1.dll
5. Use RegAsm to register it as a COM class:regasm /tlb:class1.tlb class1.dll
Test it in ASP with:
<%
set x = server.CreateObject("SampleClass")
response.Write x.GetMyName()
%>
-
RSS Bandit Comments
Now that's I've dumped Radio, I'm looking for a news aggregator. I worked a little with RSS Bandit this morning. A nice little app. A few things I'd like to see:
-
URL Encoding in Windows Forms
If you need to do some URL encoding in a Windows Forms app, the HttpUtility class has a shared (static) method to URL encode a string. Below is a sample.
NOTE: The HttpUtility class also contains a shared (static) HTML encoder too!
Option Strict On
Option Explicit On
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Web
Public Class WinApp
Inherits System.Windows.Forms.Form
private m_url as TextBox
<STAThread()> _
Shared Sub Main
Application.Run(new WinApp())
End Sub
Public Sub New
Me.Text = "URL Test"
m_url = New TextBox()
m_url.Size = new Size(280,30)
m_url.Location = new point (5,5)
Controls.Add(m_url)
m_url.Text = "http://www.microsoft.com?value="; & HttpUtility.UrlEncode("encode me please")
End Sub
End Class -
Getting RTF Text off the clipboard.
Need to get RTF text off the clipboard? Use the IDataObject interface to see if there is RTF data on the clipboard. In the sample below, copy some RTF data onto the clipboard from another application then simply click anywhere on this form to see the actual RTF string data.
Option Strict On
Option Explicit On
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports Microsoft.VisualBasic
Public Class Form1
Inherits Form
Public Sub New()
Me.Text = "Clipboard Test"
AddHandler Me.Click, AddressOf MouseClick
End Sub
Public Sub MouseClick(byval sender As object, byval e As EventArgs)
If Not Clipboard.GetDataObject() Is Nothing
Dim dobj As IDataObject = Clipboard.GetDataObject()
If dobj.GetDataPresent(DataFormats.RTF) then
Dim rtfobj As Object = dobj.GetData(DataFormats.RTF)
MsgBox(rtfobj.ToString())
End if
End if
End Sub
<STAThread()> _
Shared Sub Main()
Application.Run(New Form1())
End Sub
End Class -
Detecting a click on the caption bar.
Need to detect a click on the caption bar? You can easily intercept the required windows message (WM_NCLBUTTONDOWN).
Option Strict On
Option Explicit On
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Public Class Form1
Inherits Form
Private ReadOnly WM_NCLBUTTONDOWN As Integer = &HA1
Public Sub New()
Me.Text = "Window Caption"
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
If m.msg = WM_NCLBUTTONDOWN Then
Console.WriteLine("caption click...")
End if
MyBase.WndProc(m)
End Sub
<STAThread()> _
Shared Sub Main()
Application.Run(New Form1())
End Sub
End Class -
Create and display a form based on its Name
This sample uses Reflection to create a form using only the name stored in a string. While the technology can do way more than what is presented in this example, this will give you a basic start. Note that in this example, an instance of Form2 is created and shown based only on the string "Form2" when the main form is clicked on.
Option Strict On
Option Explicit On
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Public Class Form1
Inherits Form
Public Sub New()
Me.Text = "Main Form"
AddHandler Me.Click, AddressOf FormClick
End Sub
Public Sub FormClick(byval sender As Object, byval e As Eventargs)
Dim myType As Type = Type.GetType("Form2")
Dim myForm As Form
Dim newForm As Object = Activator.CreateInstance(myType)
myForm = CType(newForm, Form)
myForm.Show()
End Sub
<STAThread()> _
Shared Sub Main()
Application.Run(New Form1())
End Sub
End Class
Public Class Form2
Inherits Form
Public Sub New()
Me.Text = "Second Form"
End Sub
End Class -
New Blog!
Thanks to Scott for setting me up (so quickly!) with a blog here on http://www.dotnetweblogs.com!