473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reference a Control from a Class

wg
I am experienced in VB but sort of new to VB.NET. I have written a Error
Logger Class that will create a text log file and store errors and messages.
I am now attempting to use the same class to update a listbox on the form.
So far I have been unsuccessful in accessing anything on the form from a
class.

Any help would be greatly appreciated.

Thanks

wg
Nov 21 '05 #1
5 1638
When you refrence a new class you must create an instance of it.
dim myClass as new ErrorLoggerClass

myClass.method()
myClass.propertie = "xxx"

etc etc
--
JRB Technology
http://www.jrbtech.com
"wg" <wg@hotmail.com> wrote in message
news:1R*******************@bignews3.bellsouth.net. ..
I am experienced in VB but sort of new to VB.NET. I have written a Error
Logger Class that will create a text log file and store errors and
messages.
I am now attempting to use the same class to update a listbox on the form.
So far I have been unsuccessful in accessing anything on the form from a
class.

Any help would be greatly appreciated.

Thanks

wg

Nov 21 '05 #2
"wg" <wg@hotmail.com> schrieb:
I am experienced in VB but sort of new to VB.NET. I have written a Error
Logger Class that will create a text log file and store errors and
messages.
I am now attempting to use the same class to update a listbox on the form.
So far I have been unsuccessful in accessing anything on the form from a
class.


Your class needs a reference to the control in order to access it. You can
add a property to your class that stores the output control. This property
can be set when the class is instantiated. Alternatively you can raise an
event whenever a log message is added and add a handler to this event in
your form. Inside the handler you update the control.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
This can be one of the ways:

- Create an event to raise in your class.
- Pass the Form as an reference to the event.

Then you can access the ListBox in the form.

chanmm
"wg" <wg@hotmail.com> wrote in message
news:1R*******************@bignews3.bellsouth.net. ..
I am experienced in VB but sort of new to VB.NET. I have written a Error
Logger Class that will create a text log file and store errors and
messages.
I am now attempting to use the same class to update a listbox on the form.
So far I have been unsuccessful in accessing anything on the form from a
class.

Any help would be greatly appreciated.

Thanks

wg

Nov 21 '05 #4
wg
I guess I am missing something. How do I reference the control from within
the class?

Thanks

wg
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
"wg" <wg@hotmail.com> schrieb:
I am experienced in VB but sort of new to VB.NET. I have written a Error
Logger Class that will create a text log file and store errors and
messages.
I am now attempting to use the same class to update a listbox on the form. So far I have been unsuccessful in accessing anything on the form from a
class.
Your class needs a reference to the control in order to access it. You

can add a property to your class that stores the output control. This property can be set when the class is instantiated. Alternatively you can raise an
event whenever a log message is added and add a handler to this event in
your form. Inside the handler you update the control.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5
Give you one example.

Define an Interface in your solution:

Interface ICallback
Sub CallbackMethod(ByVal str As String)
End Interface

Define a class:

Friend Class Class1

Private icb As ICallback

Public Sub RegisterInterFaceForCallback(ByVal cb As ICallback)
'this method receives a reference to the client.

'Register the client for the callback
icb = cb

End Sub

Public Sub UnRegisterInterfaceForCallback()

'Unregister the client
icb = Nothing

End Sub

Public Sub UseInterfaceCallback()

'Make sure something is registered first
If Not icb Is Nothing Then
'Call back into the client code
icb.CallbackMethod("Item1")
End If

End Sub
End Class

The From you have. Implements the ICallBack Interface:

Public Class Form1
Inherits System.Windows.Forms.Form

Implements ICallback

Public Sub CallbackMethod(ByVal str As String) Implements
ICallback.CallbackMethod
'This method is called from Class1 after the form is registered
'with the instance.

ListBox1.Items.Add(str)

End Sub

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

'Register the client, passing a reference to itself
refClass1.RegisterInterFaceForCallback(Me)

'Call the method which will in turn call back into
'the client
refClass1.UseInterfaceCallback()

'Unregister the client
refClass1.UnRegisterInterfaceForCallback()
End Sub
End Class

Hope this get you a better idea.

chanmm

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: michaelkatsilis | last post by:
Hi, Are there any issues with returning a const reference value for public "get" accessor methods? eg. const string & getVal(); // or const MyString & getVal();
0
by: keith | last post by:
I created a custom control class which inherted from another control class in C#. The another control referenced another assembly. I put the custom control into toolbox and drew it into a form...
4
by: .pd. | last post by:
Hello, I have a control derived from a 3rd party control. My control sits in a class library. I have an app referencing my class library. When I compile the app, I'm told: ...
9
by: Moe Sizlak | last post by:
Hi There, I am trying to write the selected value of a listcontrol when a button is clicked and I keep getting the error "object not set to a reference of an object". The libox itself is in a...
7
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb --...
8
by: wASP | last post by:
Hi, I'm having a problem referencing the elements within an object after a method of that object (a member function) has been activated with an onsubmit handler: - - - - - - - - ...
8
by: PJ | last post by:
How can I get a reference to the master page class? It is defined as a partial class, but I cannot seem to type a variable to the name of the partial class? The compiler continually shows "The...
1
by: Nathan Sokalski | last post by:
I have a UserControl that I declare programmatically as follows: Dim userctrl as New rightside_portal() The codebehind file for this UserControl looks like the following: Partial Public...
35
by: Chris | last post by:
Hi, I tried to create a class which must change the propety 'visible' of a <linktag in the masterpage into 'false' when the user is logged. But i get the error: "Object reference not set to an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.