Remove Ads

Share on Facebook Share on Twitter

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
VB.Net Dialogs
#1
[color=#00BFFF][b][size=15]How to use Dialogs in VB![/size]

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 [b]OpenFile Dialog[/b], [b]SaveFile Dialog[/b], [b]Font Dialog[/b] and [b]ColorDialog[/b]. 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:

[b][size=12]OpenFile Dialog[/size][/b][/color]

Well you can add [b]OpenFile dialog[/b] from toolbox or just use with [b]Using [/b]or use it as [b]String [/b](which is better). So you will just need to add [b]TextBox1[/b] and [b]Button1[/b], just drag them from ToolBox. So now I will first teach you basic, double click [b]Button1[/b] 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[/code]

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

[color=#FFD700][b]How this works?[/b][/color]

With this code you declare our [b]OpenFileDialog[/b]:
[code]Dim OpenMe As New OpenFileDialog[/code]

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

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[/code]

[color=#FFFFFF][b][size=12]SaveFile Dialog[/size][/b][/color]

For this Dialog we will use [b]Textbox1 [/b]too, [b]Button1[/b] and [b]Textbox2[/b]. Same as OpenFile Dialog but with second TextBox, we will use [b]New[/b] 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[/code]

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

[color=#FFD700][b]How this works?[/b][/color]

This code will call [b]SaveFileDialog[/b], same like [b]OpenFileDialog[/b]'s code:
[code]Dim SaveMe As New SaveFileDialog[/code]

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[/code]

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 [b]StreamWriter [/b](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[/code]

[color=#FFFFFF][b][size=12]Font Dialog[/size][/b][/color]

Well this one can be really useful in some text editors. Just add Button1 and Textbox2 and that's all. Same like [b]OpenFileDialog [/b]we will use New code too. Lets start, double click [b]Button1 [/b]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[/code]

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

[color=#FFD700][b]How this works?[/b][/color]

This code will call [b]FontDialog[/b]:
[code]Dim Font12 As New FontDialog[/code]

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[/code]

[color=#FFFFFF][b][size=12]Color Dialog[/size][/b][/color]

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 [b]Button1 [/b]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[/code]

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

[color=#FFD700][b]How this works?[/b][/color]

This will call Color Dialog:
[code]Dim color123 As New ColorDialog[/code]

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[/code]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)