Jump to content


Check out our Community Blogs

Vswe

Member Since 30 Apr 2009
Offline Last Active Jul 23 2014 08:47 AM
-----

#593219 CountDown timer Class in VB.Net

Posted by Vswe on 05 March 2011 - 06:12 AM

Handy code! Thanks!
I found making the countdown public (rather than dimming it in the form_load) made it easier to access from other elements on my forum.


If you refer to the example I posted earlier, it was just to show what commands to use. I agree with you that the countdown instance shouldn't be dimmed in a load_event. If it should be Public, Private or having another scope depends on where you want to use it though.
  • 1


#581978 CountDown timer Class in VB.Net

Posted by Vswe on 28 November 2010 - 04:47 AM

You can just use <countername>.AddTime(0,30).
  • 1


#577197 Happy Birthday CodeCall!

Posted by Vswe on 18 October 2010 - 10:39 PM

Found it through google, first hit on "programming forum". For the full story you could read my old blog post about it http://forum.codecal...ht-changes.html :) In real life I doesn't have any programmer friends so I guess that's why I'm staying. And I'm pretty sure I don't need that +rep :P
  • 1


#575509 How to make listview to add new items down, not right

Posted by Vswe on 04 October 2010 - 11:43 AM

Change its View to Detailed List rather than List, then you have to use sub-items and columns though. I don't know if there's another way.
  • -1


#554972 VB.NET From beginner to advanced programmer

Posted by Vswe on 07 May 2010 - 07:03 AM

It's now about a year ago I made my 21 parts long tutorial for beginners in VB.NET. I've now merged them all to a single pdf file(it has 84 pages). In this new version I have:
  • Updated the design
  • Fixed grammar and spelling mistakes
  • Added images for easier learning
  • Updated the screenshots for a more professional look
  • Rewritten some parts of the tutorial
  • Added some parts that I had missed and removed some that didn't make sense.

The tutorial is located in this post's attachment (see below.)

You can share this as you want but make sure you give me credit for it(ie. don't remove the front page). If you read it please give me any feedback.

/Vswe :D

Attached Files


  • 6


#552645 Desktop Icon

Posted by Vswe on 20 April 2010 - 10:54 PM

Take it easy now guys, there's a reason he ask and the reason is that he doesn't know how to do it. He doesn't need someone telling what he already know.

Anyways, I don't really know the windows 7 file path but on xp/vista you can find the exe here ...\Visual Studio 2008\Projects\[your project's name]\[your project's name]\obj\Debug\

Of course it also depends on your VB version.

*moving to VB forum too *
  • -1


#550416 Help Understanding titles, e.g. Learning Programmer, Code Guru, etc.

Posted by Vswe on 04 April 2010 - 03:41 AM

There's a page on the wiki but the wiki is currently down for maintaining, but here's a link to a cached version from Google.
CodeCall - CodeCall Programming Wiki
  • -1


#525031 RGB to hex colors and hex colors to RGB - PHP

Posted by Vswe on 13 November 2009 - 05:50 PM

When using Colors in HTML and CSS you need to use a hexadecimal color (something like #FFFFFF) which is the base 16 value from the RGB color scale. Sometimes you'll need to use the RGB scale instead, for example when allocating colors when drawing images with PHP and you maybe just prefer to write it in one of the ways even though you need to have it in the other. I will in this tutorial show you how you can make two functions(fromRGB and toRGB) which can convert hexadecimal colors to RGB colors and vice versa.






From RGB to Hexadecimal

We will begin with converting RGB colors to the hexadecimal colors you use in HTML. To make it simple to use we'll have a function with one parameter for each of the Red, Green and Blue values which could look something like this:



function fromRGB($R, $G, $B){


}





So now the function will accept all required values so we'll be able to convert it to the value we want. These 3 values will need to be converted from base 10 to base 16. To use this we'll use something called "dechex()" which will convert decimal values to hexadecimal values, we can use it like this:



 function fromRGB($R, $G, $B){
$R=dechex($R);
$G=dechex($G);
$B=dechex($B);
}


Now we'll convert the values to hexadecimal, but the HTML colors has 6 digits(2 for each value) and if we have a low value this will be converted to a one digit number, not a 2 digit number. Since we always wants it to be 2 digits we have to add a leading 0 to the 1 digit numbers. A solution for this is to use "strlen" to get the length of them, is the length is lower then 2 we have to add a leading 0. So if we add this to the function it will look like this:ยด





function fromRGB($R, $G, $B){

$R=dechex($R);
If (strlen($R)<2)
$R='0'.$R;

$G=dechex($G);
If (strlen($G)<2)
$G='0'.$G;

$B=dechex($B);
If (strlen($B)<2)
$B='0'.$B;

}



Now we've already made the main thing so we should now return the values together. By adding a # in the beginning it will look as it should. The function will now look like this:

function fromRGB($R, $G, $B){

$R=dechex($R);
If (strlen($R)<2)
$R='0'.$R;

$G=dechex($G);
If (strlen($G)<2)
$G='0'.$G;

$B=dechex($B);
If (strlen($B)<2)
$B='0'.$B;

return '#' . $R . $G . $B;


}





Now if we want to test it we can do it like this:

echo fromRGB(115,25,190);


And then the result should be:

#7319be










From Hexadecimal to RGB


Now we also want to do it the other way around. This time we will only use one parameter, the parameter containing the hexadecimal color:


function toRGB($Hex){


}




We only want the actual value, so if the color is starting with a "#" we want to remove it, to test if it does we will use substr to get the first character, if it is "#" we will just remove it.



function toRGB($Hex){

if (substr($Hex,0,1) == "#")
$Hex = substr($Hex,1);

}


Now we will always only have the actual value, now we want to split this up into the red, the green and the blue values. We can use substr here too to get the different parts. The two first characters is stored in the variable called $R, character number 3 and 4 is stored in $G and lastly the 5th and the 6th character is stored in the last variable $B.


function toRGB($Hex){

if (substr($Hex,0,1) == "#")
$Hex = substr($Hex,1);


$R = substr($Hex,0,2);
$G = substr($Hex,2,2);
$B = substr($Hex,4,2);
}




$R, $G and $B does now contain the values we want, but they are still hexadecimal. To convert their 16 base to the 10 base we want we will have to use "hexdec()" which is the opposite to "dechex()" which we used earlier when converting it the other way. If we use this on all the three values we will get the values we want.


function toRGB($Hex){

if (substr($Hex,0,1) == "#")
$Hex = substr($Hex,1);


$R = substr($Hex,0,2);
$G = substr($Hex,2,2);
$B = substr($Hex,4,2);

$R = hexdec($R);
$G = hexdec($G);
$B = hexdec($B);
}



The 3 variables does now contains the decimal values we want to get. Now we should only return this in a good way. One way is to store them all in a variable called $RGB and ten return that one. The whole function will then look like this:

function toRGB($Hex){

if (substr($Hex,0,1) == "#")
$Hex = substr($Hex,1);



$R = substr($Hex,0,2);
$G = substr($Hex,2,2);
$B = substr($Hex,4,2);

$R = hexdec($R);
$G = hexdec($G);
$B = hexdec($B);

$RGB['R'] = $R;
$RGB['G'] = $G;
$RGB['B'] = $B;

return $RGB;

}



Now we want to try if it works, to do this you have to do something like this:

$RGB = toRGB("#49fe3a");
echo "R = " . $RGB['R'] . "<br />";
echo "G = " . $RGB['G'] . "<br />";
echo "B = " . $RGB['B'] . "<br />";


Which would give us the output:

R = 73
G = 254
B = 58







So if we now want to test if these two function actually works we can use them together and wee if we will get he original value back, the code for taking a hexadecimal color, convert it to RGB and then convert it back looks like this:

$RGB =  toRGB("#0076cc");
echo fromRGB($RGB['R'], $RGB['G'], $RGB['B']);



And the output is...


#0076cc

...exactly the same so therefor we know that it's working.




That was everything I had for this tutorial, I hope you enjoyed it :)
  • 2


#525018 Colors in VB.NET

Posted by Vswe on 13 November 2009 - 04:25 PM

I will in this tutorial teach you about how to use Colors in VB.NET, colors can then be used to changed the color of object etc.(not so hard to guess). In VB.NET it exists a data type called color, this is what we will be using. To declare a color variable we simply to it like this:

        Dim C As Color







Creating Colors



But there's a lot of ways of creating a color, one way is using one of the colors that already exists, it could look something like this:

        Dim C As Color
C = Color.Red
C = Color.Tomato
C = Color.Black
C = Color.WhiteSmoke



You probably get the idea. If you only have a string value of one of all the colors you can also create a color, however you must use Color.FromName(), an example is showed below:


        Dim C As Color
C = Color.FromName("Blue")
C = Color.FromName(myStringColorVariable)




You can also use the KnownColors, this will work a bit like the first way but here you can also get some special colors, like the WindowText color which is the color of the text used in client areas in Windows. Here's follows an example:


       Dim C As Color
C = Color.FromKnownColor(KnownColor.Brown)
C = Color.FromKnownColor(KnownColor.ActiveCaptionText)
C = Color.FromKnownColor(KnownColor.WindowText)




Another way which is very useful is using FromArgb, when using this you'll use Integers to specify the alpha, the red, the green and the blue in a color to create it. All these values should be between 0 and 255. You don't have to use alpha if you don't want to. Here follows some examples:


        C = Color.FromArgb(30, 155, 0, 144)
C = Color.FromArgb(255, 255, 0)
C = Color.FromArgb(0, 0, 0)






Getting info about Colors

If we already have a color we could get some info about it, sometimes it could be useful to just show some info about it, use it in conditions for checking if the colors is "good" or when creating new colors. Firstly if you only want some generally info you can use ToString, however this is not the best thing to use since it will output a name or a Argb value depending on what you used when creating the color. It's better to use something which always returns the same type. However it could be good for debugging. Here comes two examples:


        Dim C As Color
C = Color.FromArgb(255, 255, 255, 255)
MessageBox.Show(C.ToString)


will output:


Color [A=255, R=255, G=255, B=255]


While this:

        Dim C As Color
C = Color.LightGoldenrodYellow
MessageBox.Show(C.ToString)


will output this:


Color [LightGoldenrodYellow]







However, you can use ToKnownColor to get the name of the Known color(if it is a know color, else it will return 0). To use it you'll need to do something like this:


        Dim C As Color
C = Color.Aqua
MessageBox.Show(C.ToKnownColor)



The problem now is that it only gives us the index of the KnowColor's Enum member. To solve this we will have to use ToString on ToKnownColor, like this:


        Dim C As Color
C = Color.Aqua
MessageBox.Show(C.ToKnownColor.ToString)



But since there's no Enum member at index 0 called "Not a known color" or something similar we will still just get "0" if the color isn't a known color, to solve this we can simply add an if statement:


        Dim C As Color
C = Color.FromArgb(1, 2, 3, 4)

Dim Known As KnownColor
Known = C.ToKnownColor

If Known = 0 Then
MessageBox.Show("That color is not a known color")
Else
MessageBox.Show(Known.ToString)
End If








We can also get the Argb value by using ToArgb, but the integer value you'll get is pretty hard to get any information from I recommend you to get The Alpha value, the Red value, the Green value and the blue value one by one by using A, R, G and B, like so:


        Dim C As Color
C = Color.Chartreuse

MessageBox.Show("Alpha = " & C.A & " Red = " & C.R & " Green = " & C.G & " Blue = " & C.B)



This could be good when you easy want to get the color's construction.





I haven't really showed how to use a color then but I think you've understood that it works like a normal variable and therefor you can just do like this:


        Dim C As Color
C = Color.Chartreuse
Me.BackColor = C





And that was pretty much what I wanted to teach you in this tutorial. Have a fun time coding, bye. :)
  • 2


#524981 Using Using VB.NET

Posted by Vswe on 13 November 2009 - 03:21 PM

I understand that the title may be a little bit confusing but it tells exactly what I will teach you in this tutorial, how to use Using in VB.NET. Using is used when using temporary recourses for a part of your code. An example is to use Using to create a new Graphics, then we can use this Graphics inside the block which will start with the Using and the with End Using. When reaching the end of the block our Graphics(in the example, it could be something else too) is disposed. So this will allow us to easily create a temporary object we only want to use a little time and then automatically remove it when we don't need it. This means we can only access this object within the Using block. Note that all objects that are used as Using must be disposable, so for example Integers and Strings won't work. So the code for the above example could look something like the code below, not that I'm only using Graphics as an example, if you actually want to learn about it read this tutorial instead:



        Dim Img As New Bitmap(50, 50)


Using g As Graphics = Graphics.FromImage(Img)

g.FillRectangle(Brushes.White, New Rectangle(0, 0, Img.Width, Img.Height))
g.FillEllipse(Brushes.Blue, New Rectangle(5, 5, 20, 20))
g.FillEllipse(Brushes.Red, New Rectangle(25, 25, 20, 20))

End Using

Me.BackgroundImage = Img



So what the code do is that it uses Using to create a Graphics called g. Then we can use g as we want inside the Using block. Which we in the above example do three times. But then the Graphics have done what it was supposed to do and therefor we don't need it anymore, so at End Using the Graphics will be disposed which means it will be completely removed.






Since this object is only created because we want to use it, it's automatically made ReadOnly, this means we can't change the value of the Using's variable, for example this will NOT work:


        Using stream As New IO.FileStream("C:\myDirectory\myFile.extension", IO.FileMode.CreateNew)

stream = New IO.FileStream("C:\myDirectory\myOtherFile.extension", IO.FileMode.CreateNew)

End Using



And what more is special with ReadOnly variables? Since they are ReadOnly we can't change it's value later as I showed above, this means we have to set its value in the beginning, so this example below will NOT work either:


        Using stream As IO.FileStream

End Using









Something that is possible is to add as many resources to the same Using as you want, they will all be initialized at the beginning and they will all be disposed at the end, the example below shows a Using using both a Graphics and a SaveFileDialog:


        Using g As Graphics = Graphics.FromImage(Img), Save As New SaveFileDialog()

g.FillRectangle(Brushes.White, New Rectangle(0, 0, Img.Width, Img.Height))
Save.ShowDialog()
Img.Save(Save.FileName)

End Using




So when using Using you'll never forget to the dispose the object when you not need it anymore, furthermore you should make a variable ReadOnly if you'll only use it for reading, we won't forget to add this either since it's automatically included. Another thing is that you can't use your disposed objected by accident since the object could only be accessed from within the Using block, and also it's easier to see where you're using your objects and also that you won't use them any more later on which makes the code easier to read. This was everything for me and my tutorial with the strange name "Using Using VB.NET". Have a fun time coding and Bye :)
  • 2


#524683 Creating a Screen Shot tool - VB.NET

Posted by Vswe on 12 November 2009 - 07:02 AM

I will in this tutorial show you how to create a program taking screen shots, I will begin with just showing how to take a screen shot of the whole screen but I will then continue a little more advanced. In the tutorial I will show you a bit how to use bitmaps and graphics but if you want to have a better base about them before reading this tutorial you can go here. Anyway we should start now.







The whole Screen

First we will create the code that takes the image from the whole screen and saves it in a variable.


        Dim Img As New Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
Dim g As Graphics = Graphics.FromImage(Img)
g.CopyFromScreen(0, 0, 0, 0, Img.Size)
g.Dispose()


In the above code we're creating a new bitmap with the same size as the screen( by using Screen.PrimaryScreen.WorkingArea.Width and Screen.PrimaryScreen.WorkingArea.Height) and then creates a Graphics for the new bitmap called g. Now we're using the actual "screen shooter" by using CopyFromScreen. The 4 zeros means the upperleft corner of the image taken will be at the upperleft corner of your screen which will be stored at the upperleft corner of your image, we will come to this more later when we'll take screen shots of just part of the screen. Then we set the size of the image we want to take which should be the same size as the bitmap which is the same size as the screen. Then at the end we disposes the Graphics since we won't use it anymore. The bitmap variable will now "contain" your screen.






Part of the Screen

But it's not sure we actually want to take a screen shot of the whole screen as we just did. If we only want a part of the screen the easiest thins is to just take an image of a part of the screen, we don't have to get the whole screen to then crop it. It could be done like this:

        Dim Img As New Bitmap(50, 50)
Dim g As Graphics = Graphics.FromImage(Img)
g.CopyFromScreen(300, 50, 0, 0, Img.Size)
g.Dispose()


This above code will take a picture of the size 50x50 with its upperleft corner located at (300,50) on the screen. The two zeros that are still there is since the image we take's upperleft corner should be place in the image variable's upperleft corner even though it's smaller.







General function

We can now make a function which will work in any cases. We will create two version of the function, one which will take no parameters(if we want to take the whole screen) and the other with all parameters(size and location). Like this:



    Private Function TakeImage()
Return TakeImage(0, 0, Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
End Function
Private Function TakeImage(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer)
Dim Img As New Bitmap(Width, Height)
Dim g As Graphics = Graphics.FromImage(Img)
g.CopyFromScreen(X, Y, 0, 0, Img.Size)
g.Dispose()

Return Img
End Function


So in the above code we have one version of the function which takes the parameters X(the X position of the Screen where we want to take the image), Y(the Y position of the Screen where we want to take the image), Width(the width of the image we want to take) and Height(the height of the image we want to take) to take a screen shot from the screen at the wanted position with the wanted size. Then it returns that image. In the other version we're simply returning the value which will be returned from the other version while setting X=0, Y=0, Width=Screen.PrimaryScreen.WorkingArea.Width and Height=Screen.PrimaryScreen.WorkingArea.Height. In this way we can easy take images from the whole screen or from a part of the screen. The reason why one of the versions is using the other is that now if we want to change anything we just do it in the one that actually are taking the picture.






Removing the Form

Now when you're taking you're images your screenshot form is included in your images, to solve this we'll making the form invisible, take the screen shot and then make your from visible again. But if we just use Me.Visible = False and Me.Visible = True the form won't have time to become invisible before the image is taken. To solve this we will use Opacity since that will be applied faster.


    Private Function TakeImage()
Return TakeImage(0, 0, Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
End Function
Private Function TakeImage(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer)

Dim Opacity As Double = Me.Opacity
Me.Opacity = 0

Dim Img As New Bitmap(Width, Height)
Dim g As Graphics = Graphics.FromImage(Img)
g.CopyFromScreen(X, Y, 0, 0, Img.Size)
g.Dispose()

Me.Opacity = Opacity

Return Img
End Function


Here's what I talked about earlier, now we only need to modify one of those. Anyway, we declares a variable and stores the form's current Opacity. Then we set the Opacity of the form to 0 which means it will be invisible, then just before we're returning the images we will set the opacity back to the original value(the one we stored in the variable).


And that was pretty much it, now you can use the function we've made to return images from the screen, I won't show how to create a nice UI since that could different a lot depending what you want it to do and what you think is needed. Hope you found this tutorial useful, Bye :)
  • 3


#524624 Prevent programs from closing - VB.NET

Posted by Vswe on 12 November 2009 - 04:11 AM

Sometimes when the user tries to exit a program you want something else to happen instead of just closing the program which it will try to do. I think the most common reason why you want to do this is when asking the users if they want to save their works, and if the user clicks cancel we want the program to stop the shutdown. To do this we'll use the event FromClosing like this:


    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
End Sub


We use e.Cancel to choose if the closing action will be canceled, this is by default False. Note that since we don't cancel the action only when a condition is True this program will be impossible to shut down. We could therefor add a messagebox asking the user if he/she wants to save his/her unsaved work(not that we won't check if the worked is saved or not, we won't actually have anything to save at all).


    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
MessageBox.Show("You haven't saved your current work. All unsaved progress will be lost if you close, do you want to save?", "Exit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
End Sub



So now we ask the user if it wants to save the work and gives it the options Yes, No and Cancel. When the user clicks one of these button the form will just close since we don't use the info we receives to do anything, we could do it something like this:


    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim choice As DialogResult = MessageBox.Show("You haven't saved your current work. All unsaved progress will be lost if you close, do you want to save?", "Exit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)

If choice = Windows.Forms.DialogResult.Cancel Then
e.Cancel = True
End If
End Sub


Now it will check if the user pressed cancel, if it did the form won't close since we've used e.Cancel = True just as we did before. We may also want it to save the file if we press yes, to do this we will use a SaveFileDialog to ask the user where it want to save the file:


    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim choice As DialogResult = MessageBox.Show("You haven't saved your current work. All unsaved progress will be lost if you close, do you want to save?", "Exit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)


If choice = Windows.Forms.DialogResult.Yes Then
Dim Save As New SaveFileDialog()
Save.ShowDialog()
If Save.FileName <> "" Then
IO.File.WriteAllText(Save.FileName, "Save this String")
Else
e.Cancel = True
End If
ElseIf choice = Windows.Forms.DialogResult.Cancel Then
e.Cancel = True
End If
End Sub



Now if the user clicks Yes we will show a SaveFileDialog so the user can select where to save the file, if the SaveFileDialog's file name is empty afterwards it means the user clicked cancel on the SaveFileDialog, then we should take the user back to the program by cancel the closing action using e.Cancel = True as always. If the user choice a file path we saves the string "Save this String" at that location, of course you'll replace this if you want to use it. Note that all this was only a simple example showing a good way to cancel the closing of a program/form.









Close Reason


We can also check the close reason, i.e. what ordered the program to close. We could use this if we want different things to happen depending on if the user closed the program on his/her own or if the program were closed since the computer was shut down. The different reasons' name talks pretty much for themselves, and they are:


            CloseReason.ApplicationExitCall
CloseReason.FormOwnerClosing
CloseReason.MdiFormClosing
CloseReason.None
CloseReason.TaskManagerClosing
CloseReason.UserClosing
CloseReason.WindowsShutDown


Do use it you use e.CloseRason in a condition, it could look something like this:


            If e.CloseReason = CloseReason.UserClosing Then
e.Cancel = True
End If



If we continue on the example I used before we can specify the close reason to make another thing happen if the computer is shutting down while the program is open:

    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If e.CloseReason = CloseReason.UserClosing Then

Dim choice As DialogResult = MessageBox.Show("You haven't saved your current work. All unsaved progress will be lost if you close, do you want to save?", "Exit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)

If choice = Windows.Forms.DialogResult.Yes Then
Dim Save As New SaveFileDialog()
Save.ShowDialog()
If Save.FileName <> "" Then
IO.File.WriteAllText(Save.FileName, "Save this String")
Else
e.Cancel = True
End If
ElseIf choice = Windows.Forms.DialogResult.Cancel Then
e.Cancel = True
End If
ElseIf e.CloseReason = CloseReason.WindowsShutDown Then
IO.File.WriteAllText("AutoSave.txt", "Save this String")
End If
End Sub



So now we checks if the user tried to close the program, if so we will ask it if it wants to save. But if it closes since the computer is shutting down all applications to be able to shut down we just quickly autosaves all data in another file, then we can allow the user to retrieve the autosaved data next time it will start the program. If the reason is something else the program will just be closed as normal. And that was pretty much it, hope you learned anything new and finds this useful :)
  • 2


#524568 Creation icons in the Notification bar - VB.NET

Posted by Vswe on 11 November 2009 - 03:48 PM

In your Notification bar you can found a lot of icons from different programs, some for just letting you open the program fast(by clicking or double-clicking the icon) while some has more functions in a menu(by right clicking the icon) but most programs with the icons have both. I will in this tutorial show how to add a icon like this and also how to add a menu to it.




First we need to create an NotifyIcon, do that by dragging it from the toolbox and then name it exampleIcon. Change Text to "This is my Icon". We also need to set which icon it should use, if you want another one then your program has then you can choose it, otherwise we can use some code to copy the one that is used by the program itself.


    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
exampleIcon.Icon = Me.Icon
End Sub



As simple as that. So what it does is when the forms load we copy the forms icon to the NotifyIcon's icon. So now if you test your application an icon will pop up in the Notification Bar with the text "This is my Icon" and with the same icon as your program. But if you closes the application the icon still remains, this is because it don't updates the icons all the time, move your mouse over the icon and it will now update in realize it should be there and therefor disappear. To solve this problem we could add some more code to the program which makes the icon disappear when the form is closed.


    Private Sub frmMain_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
exampleIcon.Dispose()
End Sub


So when the form is disposed we also dispose the icon.



But our icon doesn't currently do anything at all more then just "being there". We should add some code to make it show the form if we double clicks on the icon, to do this we need to do two things, we need to unminimize the form if it is minimized so the user can see it and also Activate it won't be covered by other programs. But when unminimizing the form it will automatically be Activated so we only need to Activate the form if it isn't minimized.




    Private Sub exampleIcon_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles exampleIcon.MouseDoubleClick
If Me.WindowState = FormWindowState.Minimized Then
Me.WindowState = FormWindowState.Normal
Else
Me.Activate()
End If
End Sub









BalloonTips


We can also add something that is called BalloonTip, it's the bubbles that are showed from the icon sometimes. Remember that these things could be annoying for the user if done to much so think it through before using them. But I'll show how to do it of course. We will need to go back to the property window for the NotifyIcon to add the BalloonTip. Change the BalloonTipIcon to Info the BalloonTipText to "This could be annoying" and BalloonTipTitle to "Information". Now we've created the BalloonTip and only need to make it show. We need to use ShowBalloonTip to show the Tip. We now need to write how many milliseconds this Tip should be showed, to show how annoying it could be we adds it to the Load event of the form.



   Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
exampleIcon.Icon = Me.Icon
exampleIcon.ShowBalloonTip("3000")
End Sub



Note that even though I'm pointing out it could be annoying it don't have to be that if you use it in the right way, I just want to warn you from using it too much in the wrong situations.










ContexMenuStrips


Now there's time to add a menu, the menus is called ContexMenuStrips. Drag one from the toolbox onto your form and name it exampleStrip, when having the ContexMenuStrip selected we will see some boxes in top of the visual form. Here you can just type some text to make items to the menu. If you want to have a little more control you can go to properties and then edit Items, here you will have all options for the items. Now just create some items. If you want things to happen when you click on a menu you just add a click event to that one with the desired code.


Now go back to the NotifyIcon and set the ContexMenuStrip property to the ContexMenuStrip(exampleStrip). Then it's time to test it, run your program and right click your icon, now the menu will appear. So that was mostly it, now you can create a NotifyIcon and use it to make your program easier to use. I hope you liked this one, Cya :)
  • 3


#524556 Playing sounds in VB.NET

Posted by Vswe on 11 November 2009 - 02:52 PM

In this tutorial I will show two ways of playing sounds in VB.NET, the first way is easy to use but not so powerful, the other one could do more things but is a little bit more difficulty to use. Depending on what you need the sound to do you can only pick the one that fits best. If you only need simple stuff use the first one but if it can't do what you want then you just use the other one, as simple as that.


The first way is the inbuilt function, what's very negative with this way is that it could only play .wav files. To play a .wav file from your computer you simply uses it path together with the function My.Computer.Audio.Play(), like this:


My.Computer.Audio.Play(musicPath)


To stop it you just use:

My.Computer.Audio.Stop


and here you don't need to specify any parameters since you can only play one sound at the time.


By default the sound will be played in the background but you can also set how it will be played, in the background, in the background looping until it stops by My.Computer.Audio.Stop or wait to complete which will pause the calling code until the sound has finished.


My.Computer.Audio.Play(musicPath, AudioPlayMode.Background)
My.Computer.Audio.Play(musicPath, AudioPlayMode.BackgroundLoop)
My.Computer.Audio.Play(musicPath, AudioPlayMode.WaitToComplete)









The other way to play sounds is much more advanced, it can play a lot different sound formats(I don't have a list of which ones) and can also play more then one sound at the time. To do all this you have to declare this function:


Public Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer



Now you'll be able to use all its functionalities by using mciSendString.

To make everything simpler we'll declare to variables to store some info in, musicPath and musicAlias:


    Dim musicAlias As String = "myAudio"
Dim musicPath As String = "C:\Users\Public\Music\Sample Music\Symphony_No_3.wma"


musicAlias is which name we'll give our sound, by giving different sounds different names we can control them one by one. musicPath is where we have the sound on the computer, her I'll just use some Sample music found on my computer. To "create" a sound we will Open it from the file and giving it an alias which will be used when we're doing anything with it, so to open the file at the path saved in musicPath with the name stored in the variable musicAlias we're doing like this:



       mciSendString("Open " & Chr(34) & musicPath & Chr(34) & " alias " & musicAlias, CStr(0), 0, 0)



So now we will have an audio called myAudio since that was what we have stored in musicAlias. Now we can use this name to control the sound. To start playing it we'll use:


mciSendString("play " & musicAlias, CStr(0), 0, 0)


And since everything now is working in the same way I don't think I need to explain it, just look at the commented code below:



        'Start playing the sound
mciSendString("play " & musicAlias, CStr(0), 0, 0)

'Pauses the sound
mciSendString("pause " & musicAlias, CStr(0), 0, 0)

'Resumes the paused sound
mciSendString("resume " & musicAlias, CStr(0), 0, 0)

'Stop the playing sound
mciSendString("stop " & musicAlias, CStr(0), 0, 0)

'Close the sound, this is the opposite to Open and will make so
'you can't use the sound any more'unless you open it again of course.
mciSendString("close " & musicAlias, CStr(0), 0, 0)




So if you now want to play two sounds simultaneous you just give them other aliases(for example myAudio1 and myAudio2) or if you want to have a background music playing and playing while sound effects are played from time to time you just do the same(you can name one myBackgroundMusic which you play all the time, then you can name other ones like new, prompt, welcome and so on and play them while needed).


And that was pretty much it, if you think I've forgotten anything just leave a comment below. Bye :)
  • -2


#524525 Graphics in PHP

Posted by Vswe on 11 November 2009 - 01:51 PM

I will in this tutorial show you how to create images with PHP, this uses inbuilt functions and doesn't need anything else then just PHP to work. Let's begin.








Creating the image


When creating the image you can do it in two ways, one way is to use mageCreate and just set the size of the image to create an empty image with the size you selected, the other way is to use ImageCreateFromPng(you can replace "Png" with "gif" or "jpeg" for other formats) to use an existing image with the format PNG as base(means your image will look like that one):

Empty Image:

$myImage=ImageCreate(400,60);



With Image as base:

$myImage=ImageCreateFromPng("http://www.website.com/Image_Base.png");



So in both ways we're creating an Image and stores it in a variable, this variable will now contain our image.









Adding colors


Then we need to create some colors, to create them we'll use ImageColorAllocate and our image variable together with 3 values creating a RGB color value. RGB is when you have a red® value from 0 to 255, a green(G) value from 0 to 255 and a blue(B) value from 0 to 255 to create one of 16,581,375 different colors. We'll then store them in variables to later be able to use them. If you created an empty image the first color you create will be added as the background color.


$white=ImageColorAllocate($myImage, 255, 255, 255);
$black=ImageColorAllocate($myImage, 0, 0, 0);
$red=ImageColorAllocate($myImage, 255, 0, 0);
$green=ImageColorAllocate($myImage, 0, 255, 0);
$blue=ImageColorAllocate($myImage, 0, 0, 255);









Drawing forms


You can also draw geometrical forms on your images, there's some different you can choose from:


ImageEllipse()
ImageArc()
ImagePolygon()
ImageRectangle()
ImageLine()


To use them we need to set the image variable to use, the form's position, the form's location and which color to use(a variable with a color we've already created). To create a rectangle we're first setting the first parameter to $myImage since that is our image, then the start x index and then the start y index, then the form's width and height and lastly the color, like $blue(since we've created it).


ImageRectangle($myImage, 50, 20, 200, 15, $blue);
ImageRectangle($myImage, 300, 10, 300, 35, $green);



Now only the edge will be painted, to fill them with color to we have to add "Filled", like this:



ImageFilledRectangle($myImage, 50, 20, 200, 15, $blue);
ImageFilledRectangle($myImage, 300, 10, 300, 35, $green);









Writing Text

We can also paint text on the image by using imagettftext. We will then be able to write a custom text in which color we want with what Font we want and we can also select size, position and angle of the text, you use it like this:

imagettftext(Image variable, Text size, Text angle, X position, Y position, Color, Font,  Text);

An example could look like this:


imagettftext($myImage, 12, 0, 5, 20, $black, "Fonts/Oblivious font.ttf",  "This is a text");



You maybe noticed that you need to select your own font, if you don't already have any fonts you can link to you can easily find free fonts for download by just searching the net for "Free fonts"








Outputting the finished image


When you're image is completed you have to output it to the page, remember that this is the only thing you can have in the page.

So we first tell the page that it "is" an image( a png image to be exact) and then we outputs the image at the page, this things is done like this:


header("Content-type:image/png");
ImagePng($myImage);


Note that you can replace "png" to "gif" or "jpeg" if you want another image format. And now we don't need the image in our variable anymore so then we'll do some cleanup, like so:

ImageDestroy($myImage);





Since this page is only showing the image you will just use the HTML IMG tag to show it on your site(or you can use it as the background to other elements instead). This could be done like this:



<img src="http://www.website.com/myPHPpage.php" alt="myImage" />






And that was everything for this tutorial, if you have any question just post them below :)
  • 1




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download