MessageBox error

davenet

Member
Joined
Apr 5, 2007
Messages
8
Programming Experience
Beginner
I have this on my code:

MessageBox.Show("Proses backup Drive C: telah selesai.", "Pesan")


MessageBox is underlined and the following error message appear:
Declaration expected

could anyone help how to fix the code?
 

You need a reference to the namespace
System.Windows.Forms

Make sure you have

Imports
System.Windows.Forms

on top of your code
 
I revised my code to the following:

PHP:
Imports System.Windows.Forms
Public Class Form1
Inherits System.Windows.Forms.Form
 
MessageBox.Show("Proses backup Drive C: telah selesai.", "Pesan")
End Class

The same error still appear

Declaration expected.
 
put it within a subroutine or something

that area is for declaring variables, that's why it says declaration expected

you can just double click the form in the designer and start a subroutine for the form1_load event
 
VB.NET:
Public Class Form1
Inherits System.Windows.Forms.Form
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
[SIZE=5][COLOR=red]Imports System.Windows.Forms[/COLOR][/SIZE]
MessageBox.Show("Proses backup Drive C: telah selesai.", "Pesan")
End Sub
End Class

I receive this error message:

Syntax error

and Imports is highlighted.
 
sorry I meant just put the messagebox.show line in the sub...

imports is outside of a sub as the entire class would import

but you really shouldn't need to import that, that wasn't what was causing the error in the first place

shouldn't need to inherit either
 
The Old way

This is sure to work... I like to old fashion way of somethings...

VB.NET:
[SIZE=2][SIZE=2]MsgBox([/SIZE][SIZE=2][COLOR=#a31515]"Proses backup Drive C: telah selesai."[/COLOR][/SIZE][SIZE=2], MsgBoxStyle.Information, [/SIZE][SIZE=2][COLOR=#a31515]"Pesan"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[/SIZE]

Worth noting is: I used your original code in the first post above and it worked with no errors. I did not have [highlight]Imports System.Windows.Forms[/highlight] at the top of the code window either as it is not necessary.
 
Last edited:
here's all you need:
VB.NET:
Public Class Form1
  Inherits System.Windows.Forms.Form
 
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    MessageBox.Show("Proses backup Drive C: telah selesai.", "Pesan")
  End Sub
End Class

and it's extremely recommended that everyone stay away from using MsgBox, use Messagebox.Show() instead
 
hmmm, back when VS2003 came out everyone was saying remove the Imports System.VisualBasic namespace so you couldn't use they old compatability code easily which meant you could only use Messagebox.Show unless you knew exactly where MsgBox was located in the framework, so unless they've moved where MsgBox is located in the framework in v2.0 I doubt most of us VS 2003 people will use MsgBox again

was an interesting read seeing an article for VS 2005 actually tell you to use vb6 wherever possible
 
nothing wrong with msgbox

I came from vb5 up to vs2005 and I have never used messagebox.show(). Never. I am use to msgbox(), I like it, and I don't ever see myself changing unless i'm forced to do so. Personally I think the whole .net thing has just made things more complicated but one does have to change with microsoft or be left in the dirt. There are SEVERAL other older vb6 functions & methods I use because I am use to them and don't have the time to learn a completely new language. Sure I sometimes run across different ways of doing things and change to the new way. But if it works why mess with it if you have more things t do? The forms in .net are a perfect example of not leaving well enough alone. To those who use msgbox() theres no reason to let someone degrade you for it. I drive a 1995 vehicle and I'm not willing to buy a new one just because someone else thinks its hip to have a shinny new car. Let them make the payments...........
 
Last edited:
Back
Top