How to use Dialogs in VB!
Written by 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 😉
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 SubAs 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 OpenFileDialogThis 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 IfSaveFile 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 IfOkay, 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 SaveFileDialogWell 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 StringSo 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 IfFont 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 IfNow 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 FontDialogThis 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 IfColor 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 IfNow, I will explain how this works. Pretty simple & basic =)
How this works?
This will call Color Dialog:
Code
Dim color123 As New ColorDialogThis 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 IfWell that was all for today, more will be added really soon. I will edit post, because I need to go to school now. 😝


