How to use the coding from one form to another form

Posted by Agopi.net under VB.NET on 1/29/2010 | Views : 2367 | Status : [Member] | Replies : 4
Hi,

I am using vb.net 2005. I need, how to use the coding from one form to another form. I have two more forms like, Login form, Registration form, Calculation form.
I used the same code to each forms. But i need i wrote the common code only in one form and it used to another forms without wrote the each forms.

for ex.

Imports System.Data
Imports System.Data.OleDb


Public Class Form1
Dim con As OleDbConnection
Dim com As OleDbCommand
Dim adp As OleDbDataAdapter
Dim reader As OleDbDataReader
Dim ds As DataSet


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
.
.
.
.

End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\gopi\password\pass.mdb")
con.Open()

End Sub
End Class

The above bold code will be use in another form. I need to use it without write into another form. Is it possible? if possible means please explain with example.

Thanks
Gopi A




Responses

Posted by: Rbsaroj on: 1/29/2010 [Member] Starter

Up
0
Down
Dim con As OleDbConnection
Dim com As OleDbCommand
Dim adp As OleDbDataAdapter
Dim reader As OleDbDataReader
Dim ds As DataSet


As you add new form. same way right click on your project and add new module.
and copy this declaration part in module file.

After that you will be able to access it from any form in that particular projects

hope this will work


Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mcamkumaran on: 1/30/2010 [Member] Starter

Up
0
Down
if you want the any kind of coding from the any form , first you have to create the class file and then create the method and you just place the code with in the method and which form you want you have to call the method only try it
for example
public class class1
{
public void call()
{
Dim con As OleDbConnection
Dim com As OleDbCommand
Dim adp As OleDbDataAdapter
Dim reader As OleDbDataReader
Dim ds As DataSet
}
}



MuthuKumaran.R
Senior Software Engineer
Techcello
India.

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Agopi.net on: 2/1/2010 [Member] Starter

Up
0
Down
Hi Kumaran,

I understood your point. Please give me an exaple. Just how to call the class. please.. complete example

by
gopi A

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mcamkumaran on: 2/1/2010 [Member] Starter

Up
0
Down
in solution explorer ->right click->add new item->class

you just get class1.cs file

like
using System.IO;
using System.data.sqlclient;
publi class class1
{
class1()
{
}
//you have to writte the code below the method
public void common()
{
Dim con As OleDbConnection
Dim com As OleDbCommand
Dim adp As OleDbDataAdapter
Dim reader As OleDbDataReader
Dim ds As DataSet
}
}

so the above code can be called from the button click event
button1_click(e sender like argument(default)
{
class1 obj=new class1()
obj.common();//method called so you get the code from the class
}

i think it is very useful for you

MuthuKumaran.R
Senior Software Engineer
Techcello
India.

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response