Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TuT] How to use all Dialogs in VB - Font, Color, Open & Save!
#1
How to use Dialogs in VB!
Written by BlackSpider/Soul Collector

Hello everyone.

Well I decide to make thread about Windows dialogs in Visual Basic. It's really simple, but I see people still asking how to use them. Well in this tutorial we are going to learn how to use OpenFile Dialog, SaveFile Dialog, Font Dialog and ColorDialog. Basically we are going to make basic notepad with cool features. Well if you have any question please post here, and yea you can use for example some image browser or something like that. I will explain everything about dialogs so let's start. Please post comment's with feedback and your ideas. Thanks Oui


OpenFile Dialog

Well you can add OpenFile dialog from toolbox or just use with Using or use it as String (which is better). So you will just need to add TextBox1 and Button1, just drag them from ToolBox. So now I will first teach you basic, double click Button1 and write following code:

Code:
Dim OpenMe As New OpenFileDialog
        OpenMe.Filter = "Executable file(*.exe)|*.exe"
        If OpenMe.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox1.Text = OpenMe.FileName
        Else
            MsgBox("Error, please select valid file.", MsgBoxStyle.Critical, "Error")
            Exit Sub
        End If

As you can see, we don't need OpenFile Dialog, we will just use it as New OpenFileDialog which is much better & faster way. Now I will explain how this work.

How this works?

With this code you declare our OpenFileDialog:
Code:
Dim OpenMe As New OpenFileDialog

This is filter of our OpenFileDialog, for this example I used only .exefiles. So OpenFileDialog can see only .exe files:
Code:
OpenMe.Filter = "Executable file(*.exe)|*.exe"

This code check is OK button clicked, so if it is and file selected then add .exe file's pathbin textbox1. If it's something else then show error message and exit sub:
Code:
If OpenMe.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox1.Text = OpenMe.FileName
        Else
            MsgBox("Error, please select valid file.", MsgBoxStyle.Critical, "Error")
            Exit Sub
        End If


SaveFile Dialog

For this Dialog we will use Textbox1 too, Button1 and Textbox2. Same as OpenFile Dialog but with second TextBox, we will use New code too because it's much easier and better. Alright let's start. Well for this example we will save text from TextBox2. This is code for saving:
Code:
Dim SaveMe As New SaveFileDialog
        Dim FileLink As String
        If SaveMe.ShowDialog = Windows.Forms.DialogResult.OK Then
            FileLink = SaveMe.FileName & ".exe"
        Else
            Exit Sub
        End If

Okay, I will exaplain now how this works, let's start.

How this works?

This code will call SaveFileDialog, same like OpenFileDialog's code:
Code:
Dim SaveMe As New SaveFileDialog

Well this is different, we will need this because when we save file, we usually use "/File.txt" which save in same folder with program. In this case we replace the name of Variable with that code so it will save in folder which we choosed with SaveDialog:
Code:
Dim FileLink As String

So this code will check if you clicked OK button and selected valid folder to save file it will link it. Well this won't really save file, this will just link it to some StreamWriter (example) so it will save it on that location:
Code:
If SaveMe.ShowDialog = Windows.Forms.DialogResult.OK Then
            FileLink = SaveMe.FileName & ".exe"
        Else
            Exit Sub
        End If


Font Dialog

Well this one can be really useful in some text editors. Just add Button1 and Textbox2 and that's all. Same like OpenFileDialog we will use New code too. Lets start, double click Button1 and write:
Code:
Dim Font12 As New FontDialog
        If Font12.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox1.Font = Font12.Font
        Else
            Exit Sub
        End If

Now I will explain how this work. Really simple, but still maybe someone don't understand. :3

How this works?

This code will call FontDialog:
Code:
Dim Font12 As New FontDialog

This code will open FontDialog and if user clicked ok and selected font then change a font of Textbox1:
Code:
If Font12.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox1.Font = Font12.Font
        Else
            Exit Sub
        End If


Color Dialog

Ok for this dialog we need Button1 and Textbox1. I wont explain a lot about it, because It's actually same like any other dialog. So I will skip this tutorial, ok double click Button1 and write (P.S. This will change color of Text):
Code:
Dim color123 As New ColorDialog
        If color123.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox1.ForeColor = color123.Color
        End If

Now, I will explain how this works. Pretty simple & basic =)

How this works?

This will call Color Dialog:
Code:
Dim color123 As New ColorDialog

This code will check if user checked color and clicked OK, thenbit will change color of text:
Code:
If color123.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox1.ForeColor = color123.Color
        End If

Well that was all for today, more will be added really soon. I will edit post, because I need to go to school now. :tongue:
Reply
#2
Soul Collector

THanks for this

This will help me in my Programs
Reply
#3
Helped alot.

Thanks
[Image: 20r9vh4.jpg]
Reply
#4
Many thanks to you! Thumbsup This helps me very much Superman
Reply
#5
Thank's very much....very usefull foor beginners! Smile
[Image: logo.png]
xSexy.org | The New Sexy Forum ! [Erotic Pics & Videos]
Reply
#6
thanks for this tutorial man...
Reply
#7
The SaveFileDialog code helped me, thanks Tongue
Reply
#8
Helped me man,
Thanks a lot.
Reply
#9
I love when tutorials actually explain the code, instead of a huge block of unexplained, confusing nonsense. Big Grin
Aut viam inveniam aut faciam.

Reply
#10
(05-28-2010, 05:52 PM)Cybr Wrote: I love when tutorials actually explain the code, instead of a huge block of unexplained, confusing nonsense. Big Grin

Same. I tend to understand when people do that. Then I can make my own crap.
[Image: vwx1mh.png]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [TUT] How to make a simple WebBrowser in VB 2010! [TUT] - [ Pictures] Statics 95 55,166 10-07-2012, 06:56 AM
Last Post: a99
  [VB.Net][SRC]ProgressBar Color HF~Legend 14 6,802 09-08-2012, 04:18 PM
Last Post: spesificrelax
  VB.NET Wrap Color BBCode Tags [Reply] AceInfinity 8 2,191 03-15-2012, 08:59 PM
Last Post: AceInfinity
  [TUT]Auto-Update System[TUT] HB Virus 3 2,140 01-07-2012, 02:21 PM
Last Post: Mastermrz
  [TUT]Enable and Disable TaskManger in vb.net [TUT] HB Virus 4 2,838 12-19-2011, 10:10 AM
Last Post: euverve

Forum Jump:


Users browsing this thread: 1 Guest(s)