Senin, 02 Februari 2009

Directly filling in a control on Form2 vb 2005


IDE : VS 2005,

posted by Pidgeon


1. Directly filling in a control on Form2

This is the easy one. An object (control) on Form2 is directly referred to using the properties of the formtype Form2
It could also be used with a public variable, but since this is considered bad practice by most programmers, I'll refer anyone for that method to the 2nd way: using properties

Code: Dim F As New Form2()
F.TextBox1.Text = 5
F.TextBox2.Text = "SomeOtherValue"
F.Show()
'or F.ShowDialog

The code opens a new instance of Form2, and since F is dimmed as Form2, it 'knows' its properties and objects. So you can directly fill in a TextBox (as in the example) or any other control.

2. Using properties on the target form

This will often be the most efficient way. Any value given to properties can be checked first on flaws.
On the Target form (here Form2) add the properties, such as:

Code: Property FirstValue() As Integer
Get
Return CInt(TextBox1.Text) 'this of course can also be a variable, which will mostly be the preferred way.
End Get
Set(ByVal Value As Integer)
TextBox1.Text = CStr(Value )
End Set
End Property

Property SomeOtherValue() as String
Get
Return TextBox2.Text
End Get
Set(ByVal Value as String)
'You could do some alternative processing here to make the value the right format or to start some other events
'For example the value must be in capitals and have a maximum of 10 characters
If Value.Length > 10 Then Value = Value.SubString(0, 10)
Value = Value.ToUpper
TextBox2.Text = Value
End Set
End Property

Now Form2 has 2 properties which can be accessed. Use this code on the Source Form (Form1) to change the values:

Code: Dim F As New Form2()
F.FirstValue = 5
F.SomeOtherValue = "SomeOtherValue"
F.Show()
'or F.ShowDialog

Using properties makes the code more manageable and easier to read then writing the text boxes directly.
If checking of the data needs to be done with the first method (writing to controls directly), you can add event handlers of the control where checking is done.

3. Overloading the New() statement of the target form

One of the many advantages of using .NET: overloading. This means you can have more then one sub, function or property with the same name, but with different parameters. (C++ programmers already know this method). The right version will be chosen based on the parameters you give (that would be a different FAQ )
On the target form (Form2) add:

Code: Sub New(ByVal FirstValue As Integer, ByVal SomeOtherValue As String)
'the first 2 commands are required for proper functioning: they 'create' the form.
'Be sure to always include InitializeComponent when overloading sub new in a form.
'In your own class, this will not be necessary
MyBase.New()
InitializeComponent()
TextBox1.Text = FirstValue
TextBox2.Text = SomeOtherValue
End Sub

Now the new sub in Form2 is overloaded, this means you have two ways of opening Form2. The normal way: .... New Form2 and one with parameters ..... New Form2(Value1,Value2)
To see this in action, add the following code to the Source Form (Form1)

Code:
Dim F As New Form2(5, "SomeOtherValue")
F.Show()

There is no best method for transferring data. The best suitable approach will always vary depending on the problem.

If the form is already open
The examples above open a new form, if you want to use a form that's open, the best bet would be to keep a variable, either in the Source Form or in a public Module. e.g.:

Code: Module PublicVariables
public MyInstanceOfForm2 as Form2
End Module

when opening a new Form2, set that variable to the Form2 value

Code: MyInstanceOfForm2 = New Form2
MyInstanceOfForm2.Show

The methods described above will then be available for MyInstanceOfForm2.



source :http://www.vbcity.com

-------------------------------------------------------

Trik Gambar Bergerak

Trik Gambar-dimouse

Trik hapus pwd mysql

Trik insertin to db

Trik jadi root dilinux

Trik jam-distatus-bar

Trik Koneksi-ke database

Trik Koneksi-msql-php

Trik lihat-database-mysql

Trik membahas-fungsi-else

Trik member-area