Thursday, November 13, 2008

WEBSITE MOVED TO www.ToddVance.Com

Please see my site www.ToddVance.com.

Monday, June 23, 2008

myString.Replace("Blah", "Blah Blah") - DONT TRY THIS AT HOME! Strings are immutable!

I hope I can save someone some time from this little mind boggler....

I had a string, I wanted to change one particular part of the string... NO PROBLEM -- use .Replace right? well, sort of.

Strings are immutable objects and cannot be changed -- so saying

msgText.Replace ("5 of 6", "3 of 5")

may look good and will not even give you an error (unfortunately) BUT it also WILL NOT WORK!

You have two options... create a different string to house the Replaced string OR create a stringBuilder.

I chose to use a new string and the .Replace worked great.

myNewText = msgText.Replace ("5 of 6", "3 of 5")

all is well again.

Monday, June 16, 2008

SENDKEYS -- Copying to clipboard - open Wordpad - Paste clipboard all in code!

I think this is cool and I learned several useful tools in the process. I have plans on creating a sort of graph in a form.

But I wanted to give the user's a chance to print it --- without getting to detailed, I figured that I would grab a screen shot of the form, open Wordpad (which everyone should have), and then paste it within wordpad.

I figured they could take it from there....

So how did I do it?



First off, grab the active part of the screen using SENDKEYS with the sendwait method. I put this in a FUNCTION called screenCapture:

Public Function screenCapture() As Image

'CALL THE SENDKEYS WITH THE PRINT SCREEN METHOD -- '%' = ALT
SendKeys.SendWait("%{PRTSC}")

'CREATE AN OBJECT TO HOLD THE IMAGE - IN CASE YOU WANT TO USE IT SOMEWHERE ELSE IN YOUR CODE
Dim captImage As IDataObject = Clipboard.GetDataObject

'RETURN THAT IMAGE AND FORMAT IT AS A BITMAP - AGAIN, TO USE SOMEWHERE ELSE IN CODE (you dont need this to paste -- its ALREADY ON the clipboard)
Return captImage.GetData(DataFormats.Bitmap)

End Function
------

Now from within the button click that called the screen capture function we send a Shell command to open the WORDPAD and then use SENDKEYS again to call Control and V, thus pasting the clipboard straight into Wordpad.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Call the function
screenCapture()

'Open WordPad
Shell("C:\Program Files\Windows NT\Accessories\wordpad.exe", vbNormalFocus)

'Use SENDKEYS AGAIN TO CTRL+V ("^" = CTRL)
SendKeys.Send("^+{V}")

End Sub

Thats about it!!! In about 2 minutes you can drag a button to your form, include the above code and you have your very own screen capture functionality!!!!!

A couple other cool things you could do is SAVE the capture -- since the function call is an image you could write this:
screenCapture().Save("c:\screenCap.bmp", System.Drawing.Imaging.ImageFormat.Bmp)

You could also assign the captured image to a Picture Box on your form:
PictureBox1.BackgroundImage = screenCapture()

*I used Background image rather than Image because I can set the Layout of background to Stretch thus assuring that we see the entire image....

Pretty cool stuff and very quick and simple!!!

** GO SENDKEYS!

Thursday, June 12, 2008

Merging two dataTables

In this method I create a TEMP Table because I have two tables of information that I need to merge together (some names and their matching data) ---so I create a new table and clone it -- Clone copies the data structure of one table to another.
Dim tempTable As DataTable

'I clone the normal table that we need so I can insert the proper users later.
tempTable = dataset1.Tables(1).Clone

THEN we start going through our two tables and merging the users with their data-- INTO our temp table.
For iRowCount = 0 To dsReports.Tables(0).Rows.Count - 1

'Create the teachername for the rows
If Not dataset1.Tables(0).Rows(iRowCount)("fname") Is DBNull.Value Then teachername = dataset1.Tables(0).Rows(iRowCount)("fname")

If Not dataset1.Tables(0).Rows(iRowCount)("lname") Is DBNull.Value Then teachername = teachername + " " + dataset1.Tables(0).Rows(iRowCount)("lname")

'Now iterate through the other table and match the names via the matching ID
For iRowCount2 = 0 To dataset1.Tables(1).Rows.Count - 1

If dataset1.Tables(0).Rows(iRowCount)("sessid") = dataset1.Tables(1).Rows(iRowCount2)("session") Then

tempTable.ImportRow(dataset1.Tables(1).Rows(iRowCount2))
tempTable.Rows(tempTable.Rows.Count - 1)("teachername") = teachername

End If

Next

Next

FINALLY, we remove the two seperate tables that are no longer needed and we insert our finished temp table that will work with the rest of the code.

dataset1.Tables.Remove(dataset1.Tables(0))

dataset1.Tables.Remove(dataset1.Tables(1))

dataset1.Tables.Add(tempTable)

Wednesday, June 11, 2008

STRINGS and their stuff.

GET RID OF TRAILING COMMA:
(you have built something that leaves you with a string with a comma at the end and you want it gone -- use Left minus 1 of the length of string!)

dim myString as String = "1,2,3,4,5,"
myString = Left(myString, myString.Length - 1)
RESULT: myString = "1,2,3,4,5"


more to come...

Random (yet useful) SQL Commands

CHECKING IF A FIELD IS NULL:
SELECT lastName, ID
FROM tbl_customers
WHERE lastName IS NULL OR ID IS NULL
** THIS statement pulls all rows that either the lastName OR the ID is null. **

You could also do this if you wanted to replace NULLs with something:
WHERE ISNULL(lastName, 'test')
*This replaces all lastName 's that are null with the word 'test'



USING LIKE AND WILDCARDS:
There's a million uses for the LIKE keyword.

Use LIKE for incomplete words
Use % for anything after or before
Use _ for a single character

ex:
SELECT * FROM tbl_people WHERE lastname LIKE 'A%' - everyone who's last name starts with an A
SELECT * FROM tbl_people WHERE lastname LIKE '%A%' - use this to choose anything before and after.
SELECT * FROM tbl_people WHERE lastname LIKE 'Anders_n' - uses the _ wildcard which will ONLY give ONE char - so here you would ONLY get Anderson's or Andersen's but never Andershen's (because we all know how many Andershen's there are out there!)

Finally - dont forget that we can use the NOT keyword in conjuction with LIKE -- so if you DIDNT want to see anyone whose names started with Ander (like those dreaded Andershen's!) - we could write.
SELECT * FROM tbl_people WHERE lastname NOT LIKE 'Ander%'
more to come...