Contoh Program Visual Basic

 Posted admin
Contoh Program Visual Basic Average ratng: 8,2/10 683 reviews
  1. Visual Basic 2010 Download
  2. Contoh Program Visual Basic Bengkel

Pilih All Programs, dan arahkan ke pilihan Microsoft Visual Studio 6.0 dan klik pada pilihan Microsoft Visual Basic 6.0. Gambar 1.1 Membuka Program Visual Basic 6.0 3. Setelah berhasil menjalankan Visual Basic untuk yang pertama kalinya, Anda akan melihat tampilan logo Visual Basic 6.0 dan tak lama kemudian akan muncul kotak dialog berikut. Program ini telah disatukan kedalam kedalam aplikasi yang bernama Microsoft Visual Studio yang dibuat perusahaan microsoft tersebut untuk memudahkan programmer berinteraksi dengan bahasa pemrograman lainnya. Didalam aplikasi tersebut mempunyai bahasa pemrograman lainnya, seperti: Visual C, Visual Basic, Visual Vox Pro dan Visual J#.

I've 6 TextBox and 6 CheckBox. Now I want to disable the TextBox1 with a CheckBox1 and reactivate it with the Same CheckBox. How a can do it?

Edit1 15.55 14/02/2013

I have done so to solve my problem!

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = False
ElseIf CheckBox1.Checked = False Then
TextBox1.Enabled = True End If End Sub
`

Simbox97
Simbox97Simbox97
462 gold badges3 silver badges10 bronze badges

5 Answers

This will work, just add more for the other check boxes

What this does: if checkbox1 is check, the checked_changed event fires and the code inside is ran. The if statement looks to see if the checkbox is checked or not. If it is checked, then it sets the textbox1 to enabled, if not it sets it to disabled. Be sure to set the enabled property to either enabled or disabled when you create your program. If you want it to be enabled from the start, that is the default....otherwise set it to disabled in its properties view.

MonkeyDougMonkeyDoug
SysDragonSysDragon
7,89515 gold badges42 silver badges76 bronze badges

Take a look at this tutorial below. After, look at the checkbox control's events and choose the most fitting one. The property you will be changing on the textbox is Enabled.

Luke WyattLuke Wyatt
9912 gold badges10 silver badges22 bronze badges

This works if you have a layer built in that you can send objects behind (therefore hide things). I use this as a way to make text boxes and other items appear and disappear depending on other selections.

Dave Clemmer
3,01211 gold badges44 silver badges72 bronze badges

Visual Basic 2010 Download

LaceyLacey
Jean-François Fabre
109k10 gold badges68 silver badges123 bronze badges
Joshua BittnerJoshua Bittner

Not the answer you're looking for? Browse other questions tagged vb.netcheckboxtextbox or ask your own question.

The first program to write is the same for all languages: Print thewords hello, world

—Brian W. Kernighan and Dennis M. Ritchie, The C Programming Language

It has become a tradition for programming books to begin with ahello, world example. The idea is that enteringand running a program—any program—may be the biggesthurdle faced by experienced programmers approaching a new platform orlanguage. Without overcoming this hurdle, nothing else can follow.This chapter contains three such examples: one that creates a consoleapplication, one that creates a GUI application, and one that createsa browser-based application. Each example stands alone and can be runas is. The console and GUI applications can both be compiled from thecommand line (yes, Visual Basic .NET has a command-line compiler!).The browser-based application requires a computer running InternetInformation Server (IIS).

This is the world’s favorite programmingexample, translated to Visual Basic .NET:

Contoh Program Visual Basic Bengkel

This version of hello, world is aconsole application --itdisplays its output in a Windows command-prompt window. To compilethis program, enter it using any text editor, such as Windows’sNotepad, save it in a file whose name ends with.vb, such as Hello.vb, andcompile it from the Windows command line with this command:

The command vbc invokes the Visual Basic .NETcommand-line compiler, which ships with the .NET Framework SDK, andinstructs it to compile the file named in the command-line argument.Compiling Hello.vb generates the fileHello.exe. After compiling, typeHello at the command line to run your program.Figure 1-1 shows the results of compiling andrunning this program.

Figure 1-1. Compiling and running hello, world

If you’re accustomed to programming in Visual Basic 6, you cansee even from this little program that Visual Basic has changeddramatically. Here’s a breakdown of what’s happening inthis code.

The first line:

indicates that the program may use one or more types defined in theSystemnamespace.(Types are grouped into namespaces to help avoid name collisions andto group related types together.) Specifically, the hello, world program uses the Console class, which is defined inthe System namespace. The Imports statement ismerely a convenience. It is not needed if the developer is willing toqualify type names with their namespace names. For example, thehello, world program could have been writtenthis way:

However, it is customary to use the Importsstatement to reduce keystrokes and visual clutter.

An important namespace for Visual Basic developers isMicrosoft.VisualBasic. The types in this namespace expose membersthat form Visual Basic’s intrinsic functions and subroutines.For example, the Visual Basic Trim function is amember of the Microsoft.VisualBasic.Strings class, while theMsgBox function is a member of theMicrosoft.VisualBasic.Interaction class. In addition, VisualBasic’s intrinsic constants come from enumerations within thisnamespace. Much of the functionality available in this namespace,however, is also duplicated within the .NET Framework’s BaseClass Library. Developers who are not familiar with Visual Basic 6will likely choose to ignore this namespace, favoring thefunctionality provided by the .NET Framework. The .NET Framework isintroduced later in this chapter and is explained in detail in Chapter 3.

Next, consider this line:

This line begins the declaration of a standard module namedHello. The standard-module declaration ends withthis line:

In Visual Basic 6, various program objects were defined by placingsource code in files having various filename extensions. For example,code that defined classes was placed in .clsfiles, code that defined standard modules was placed in.bas files, and so on. In Visual Basic .NET, allsource files have .vb filename extensions, andprogram objects are defined with explicit syntax. For example,classes are defined with the Class...End Classconstruct, and standard modules are defined with theModule...End Module construct. Any particular.vb file can contain as many of thesedeclarations as desired.

The purpose of standard modules in Visual Basic 6 was to hold codethat was outside of any class definition. For example, globalconstants, global variables, and procedure libraries were oftenplaced in standard modules. Standard modules in Visual Basic .NETserve a similar purpose and can be used in much the same way.However, in Visual Basic .NET they define datatypes that cannot beinstantiated and whose members are all static. This will be discussedin more detail in Chapter 2.

The next line in the example begins the definition of a subroutinenamed Main:

It ends with:

This syntax is similar to Visual Basic 6. The Substatement begins the definition of asubroutine --amethod that has no return value.

The Main subroutine is the entry point for theapplication. When the Visual Basic .NET compiler is invoked, it looksfor a subroutine named Main in one of theclasses or standard modules exposed by the application. IfMain is declared in a class rather than in astandard module, the subroutine must be declared with theShared modifier. This modifier indicates that theclass does not need to be instantiated for the subroutine to beinvoked. In either case, the Main subroutinemust be Public. An example of enclosing theMain subroutine in a class rather than in astandard module is given at the end of this section.

If no Main subroutine is found, or if more thanone is found, a compiler error is generated. The command-linecompiler has a switch(/main:location) thatallows you to specify which class or standard module contains theMain subroutine that is to be used, in the casethat there is more than one.

Lastly, there’s the line that does the work:

This code invokes the Console class’s WriteLine method, whichoutputs the argument to the console. The WriteLine method is definedas a shared (also known as astatic) method. Shared methods don’trequire an object instance in order to be invoked; nonshared methodsdo. Shared methods are invoked by qualifying them with their classname (in this case, Console).

Here is a program that uses a class instead of a standard module tohouse its Main subroutine. Note thatMain is declared with theShared modifier. It is compiled and run in thesame way as the standard module example, and it produces the sameoutput. There is no technical reason to choose one implementationover the other.

Here’s the GUI version ofhello, world:

This is similar to the hello, world consoleapplication, but with extra stuff required since this is a GUIapplication. Two additional Imports statements areneeded for drawing the application’s window:

The HelloWindows class has something that Visual Basic programs havenever seen before, the Inherits statement:

The Visual Basic .NET language has class inheritance. TheHelloWindows class inherits from the Form class, which is defined inthe System.Windows.Forms namespace. Class inheritance and theInherits statement are discussed in Chapter 2.

The next line declares a label control that will be used fordisplaying the text Hello,Windows:

The Label class is defined in the System.Windows.Forms namespace.

As is the case with console applications, GUI applications must havea shared subroutine called Main:

This Main method creates an instance of the HelloWindows class andpasses it to the Run method of the Application class (defined in theSystem.Windows.Forms namespace). The Run method takes care of thehousekeeping of setting up a Windows message loop and hooking the HelloWindows form intoit.

Next is another special method:

Like Main, New has specialmeaning to the Visual Basic .NET compiler. Subroutines namedNew are compiled intoconstructors.A constructor is a method that has no return value (but can havearguments) and is automatically called whenever a new object of thegiven type is instantiated. Constructors are explained further inChapter 2.

The constructor in the HelloWindows class instantiates a Labelobject, sets some of its properties, sets some properties of theform, and then adds the Label object to the form’s Controlscollection. The interesting thing to note is how different this isfrom how Visual Basic 6 represented form design. In Visual Basic 6,form layout was represented by data in .frmfiles. This data was not code, but rather a listing of the propertiesand values of the various elements on the form. In Visual Basic .NET,this approach is gone. Instead, Visual Basic .NET statements mustexplicitly instantiate visual objects and set their properties. Whenforms are designed in Visual Studio .NET using its drag-and-dropdesigner, Visual Studio .NET creates this code on your behalf.

The command line to compile the Hello, Windowsprogram is:

(Note that there is no break in this line.)

The command line for compiling the Hello, Windows program has more stuff in it than the one for theconsole-based hello, world program. In additionto specifying the name of the .vb file, thiscommand line uses the /references switch tospecify three .dlls that contain theimplementations of library classes used in the program (Form, Label,Point, etc.). The hello, world consoleapplication didn’t require references when being compiledbecause all it used was the Console class, defined in the Systemnamespace. The Visual Basic .NET command-line compiler includes tworeferences implicitly: mscorlib.dll (whichcontains the System namespace) andMicrosoft.VisualBasic.dll (which contains helperclasses used for implementing some of the features of Visual Basic.NET).

Besides the /references switch, the command linefor compiling the Hello, Windows programincludes the /target switch. The/target switch controls what kind of executablecode file is produced. The possible values of the/target switch are:

exe

Creates a console application. The generated file has an extension of.exe. This is the default.

Contoh program visual basic 2010 database
winexe

Creates a GUI application. The generated file has an extension of.exe.

library

Creates a class library. The generated file has an extension of.dll.

The output of Hello, Windows is shown in Figure 1-2.

Figure 1-2. Hello, Windows!

GUI applications are explained in detail in Chapter 4 and Chapter 5.

Here is a browser-based version of thehello, world application. Because the simplestversion of such an application could be accomplished with only HTML,I’ve added a little spice. This web page includes three buttonsthat allow the end user to change the color of the text.

To run this program, enter it using a text editor and save it in afile named HelloBrowser.aspx. Because theapplication is a web page that is meant to be delivered by a webserver, it must be saved onto a machine that is running IIS and hasthe .NET Framework installed. Set up a virtual folder in IIS to pointto the folder containing HelloBrowser.aspx.Finally, point a web browser toHelloBrowser.aspx. The output of theHello, Browser application is shown in Figure 1-3.

Figure 1-3. Hello, Browser!

Tip

Be sure to reference the file through the web server machine name orlocalhost (if the web server is on your localmachine), so that the web server is invoked. For example, if the fileis in a virtual directory called Test on yourlocal machine, point your browser to http://localhost/Test/HelloBrowser.aspx. Ifyou point your browser directly to the file using a filesystem path,the web server will not be invoked.

Going into detail on the Hello, Browser codewould be too much for an introduction. However, I’d like todraw your attention to the <asp:label> and<asp:button> tags. These tags representserver-side controls. A server-side control is a class thatis instantiated on the web server and generates appropriate output torepresent itself on the browser. These classes have rich, consistentsets of properties and methods and can be referenced in code likecontrols on forms are referenced in GUI applications.

ASP.NET has many other nifty features, some of which are:

  • Web pages are compiled, resulting in far better performance overclassic ASP.

  • Code can be pulled out of web pages entirely and placed in.vb files (called code-behind files) that are referenced by the web pages. Thisseparation of web page layout from code results in pages that areeasier to develop and maintain.

  • ASP.NET automatically detects the capabilities of the enduser’s browser and adjusts its output accordingly.

Browser-based applications are discussed in detail inChapter 6.