problem with me.name

A

Altman

Ok I have a control that is inherited from another class. In the child I
put msgbox(me.name) in the load event. What always pops up is the name of
the parent class and not the name of the instance of the object. WHY? Also
if I can figure this out I'm hoping that I can put this in the load event of
the parent and still get it to work.
 
A

Altman

That doesn't work either. Here is a little more specifics.

I have a class called BaseControl
I have a class called Bin which inherits basecontrol

I put the bin on a form and in the name property I filled it in with Bin1.
When I put myclass.name or me.name or mybase.name in a msgbox() in the load
event of either the basecontrol class or the Bin class I always get "Bin"
and I want it to say "Bin1."
 
C

Cor Ligthert

Altman,

I do not understand you, when you see this code, what is than going wrong in
your opinion. Do not do it this way, this is only to test.
\\\
Class mytextbox
Inherits System.Windows.Forms.TextBox
Friend Sub showname()
Me.Name = "myname"
MessageBox.Show(Me.Name)
End Sub
End Class
Class Runner
Private name As String = "mytest"
Friend Sub run()
Dim mytexb As New mytextbox
MessageBox.Show(Me.name)
mytexb.showname()
End Sub
End Class
Class mytest
Public Shared Sub main()
Dim a As New Runner
a.run()
End Sub
End Class
////
 
H

Herfried K. Wagner [MVP]

Altman said:
Ok I have a control that is inherited from another class.
In the child I put msgbox(me.name) in the load event. What
always pops up is the name of the parent class and not the
name of the instance of the object.

The name of the parent class is the same as the name of your class, because
your object is an instance of its base class too. 'Name' is inherited, not
duplicated.
 
J

Jay B. Harlow [MVP - Outlook]

Altman,
Can you post the code in your Load event?

Are you talking the Load event of the control or the load event of the form?

It sounds like you are calling MsbBox before the InitializeComponent call of
the form, you should be calling it after, as the Name property should be set
inside the InitializeComponent method of the form.

Also verify that the Name property is indeed being set inside the
InitializeComponent method.

I don't remember when the load event of the control itself is called,
however it may be before the form has a chance to set the Name property of
the control, I would put a break point in both the control's load event &
the form's InitializeComponent to verify.

Hope this helps
Jay
 
A

Altman

I guess that what I am noticing is that any property that I set in the
design mode does not show up with the correct value in the load event. I
have changed other properties in design mode and I noticed that at the load
event, these values are not there. When are the values that I set in design
mode available?
 
A

Altman

It has to be that the load event is happening before the name is being
initialized. So far this is the only work around I can think of. I created
a variable called lFirstRun and initialized it true. Then I put this code
in the paint event and it seems to work.

If lFirstRun then
lFirstRun = false
msgbox(me.name)
End if
 
H

Herfried K. Wagner [MVP]

Altman said:
I guess that what I am noticing is that any property that
I set in the design mode does not show up with the
correct value in the load event.

That's not desired behavior. The values set in the properties window should
be set when 'Load' is raised. Mhm... Maybe you can post the code of your
form's constructor and 'Sub InitializeComponent'?
 
J

Jay B. Harlow [MVP - Outlook]

Altman,
Again: Are you talking the Load event of the control or the load event of
the form?

Can you more accurately describe what you are attempting & what you are
doing, so that we can actually help you, rather then making "guessing" on
what you are doing.

In other words: What are you really trying to do with the Name, and why do
you think you need to do it in the Load event? Don't forget we are not
really sure which Load event you are referring to.

Hope this helps
Jay
 
J

Jay B. Harlow [MVP - Outlook]

Altman,
When are the values that I set in design mode available?
At run time, the values you set at design time are available when you set
them, hence you need to do "something" with them in the Property Set
procedures. HOWEVER! the "do "something"" is the tricky part, for example in
a Color property the Property Set procedure may save the color value, and
then call Invalidate on the control, allowing the Paint event to repaint the
control in the new color...

Also remember that you can actually set the values at any time you want...

What are you wanting to do with them at Load time? Specifically.

Hope this helps
Jay

Altman said:
I guess that what I am noticing is that any property that I set in the
design mode does not show up with the correct value in the load event. I
have changed other properties in design mode and I noticed that at the load
event, these values are not there. When are the values that I set in
design mode available?
 
A

Altman

it is in the load event of the control. Basically the reason I am doing
this is I have a property that I want it to read an external file for. This
way I can set this property outside the development environment based on
what is needed. I am actually doing this through an ini file right now
where each object will look for its name in the ini file and retrieve its
value. This is why I need the name. I thought about making a different
property for this but I figured the name property works best and I know that
it will not be duplicated. So upon loading of the control I want it to go
read the ini file where it's name is located and retrieve a value. So the
code is in the load event of the control, not the form.
 
A

Altman

here is my code. I am somewhat of a newbie to VB.net but have done alot of
programming in FoxPro.

Imports System.Text

<Serializable()> Public Class BaseControl

Inherits System.Windows.Forms.UserControl

Private lFirstRun As Boolean



#Region " Windows Form Designer generated code "



Public Sub New()



MyBase.New()

Dim ctl As Object, myForm As System.Windows.Forms.Form

'This call is required by the Windows Form Designer.

InitializeComponent()

lFirstRun = True

'Add any initialization after the InitializeComponent() call



End Sub



'UserControl overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub



'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer



'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

'

'BaseControl

'

Me.Name = "BaseControl"



End Sub



#End Region



Private Declare Ansi Function GetPrivateProfileString Lib "kernel32" _

Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As
String, ByVal _

lpKeyName As String, ByVal lpDefault As String, ByVal
lpReturnedString As _

StringBuilder, ByVal nSize As Int32, ByVal lpFileName As String)
As Int32

Private Declare Ansi Function WritePrivateProfileString _

Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _

(ByVal lpApplicationName As String, _

ByVal lpKeyName As String, ByVal lpString As String, _

ByVal lpFileName As String) As Integer

Private m_ioValue As Boolean, m_IO As Short, m_ioMaster As IOMaster,
m_ioObject As IO

Protected Const INIFILE = "HMI.ini"



#Region "Properties"

Public Property IOValue() As Boolean

Get

IOValue = m_ioValue

End Get

Set(ByVal Value As Boolean)

Value = m_ioValue

End Set

End Property



Public Property IO() As Short

Get

IO = m_IO

End Get

Set(ByVal Value As Short)

m_IO = Value

addHandlerEvent()

End Set

End Property

Public Property IOMaster() As IOMaster

Get

IOMaster = m_ioMaster

End Get

Set(ByVal Value As IOMaster)

m_ioMaster = Value

addHandlerEvent()

End Set

End Property

#End Region

#Region "Subs and Functions"

Private Sub addHandlerEvent()

'remove the old handler if there is one

If Not m_ioObject Is Nothing Then

RemoveHandler m_ioObject.IOChange, AddressOf Me.ChangeIOValue

End If

If Not m_ioMaster Is Nothing And m_IO > 0 Then

m_ioObject = m_ioMaster.IOObject(m_IO)

If Not m_ioObject Is Nothing Then

'bind the changeIovalue sub to the event IOChange of the io
object



AddHandler m_ioObject.IOChange, AddressOf Me.ChangeIOValue

Else

'MsgBox("ioobject is nothing")

End If

Else

'MsgBox("iomaster is nothing " & m_IO.ToString)

End If

End Sub

Private Sub ChangeIOValue()

'The io Value changed so update the data and raise the IOChange
Event

m_ioValue = m_ioObject.IOValue

RaiseEvent IOChange()

End Sub

Protected Overridable Sub GetIO()

Dim RetVal As StringBuilder, X As Integer

RetVal = New StringBuilder(255)

X = GetPrivateProfileString("IO", Me.Name, "", RetVal, 255, CurDir()
& "\" & INIFILE)



If X > 0 Then

Me.IO = Val(RetVal.ToString)

Else

WritePrivateProfileString("IO", Me.Name, "0", CurDir() & "\" &
INIFILE)

End If

End Sub

#End Region



Public Event IOChange()



Private Sub BaseControl_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

If lFirstRun Then

lFirstRun = False

GetIO()

End If

End Sub

End Class


Jay B. Harlow said:
Altman,
When are the values that I set in design mode available?
At run time, the values you set at design time are available when you set
them, hence you need to do "something" with them in the Property Set
procedures. HOWEVER! the "do "something"" is the tricky part, for example
in a Color property the Property Set procedure may save the color value,
and then call Invalidate on the control, allowing the Paint event to
repaint the control in the new color...

Also remember that you can actually set the values at any time you want...

What are you wanting to do with them at Load time? Specifically.

Hope this helps
Jay
 
A

Altman

Oh btw I was calling GetIO() from the load event and that wasn't working. I
moved it to the Paint event and now it works. If there is anyway to get it
to work in the load event, that is where I'd rather have it.
 
J

Jay B. Harlow [MVP - Outlook]

Altman,I think we need to see your code! Actually a simple user control such as
below, that accurately displays your problem.

I just created a simple UserControl in VS.NET 2003 and the control's name is
set in the UserControl's Load event.

As you can see from the following control, I simply created a new User
Control, and added MessageBox.Show to the Load event handler...

I then added one of my UserControl1 to the designer of my form and set its
Name property.


Public Class UserControl1
Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container
End Sub

#End Region

Private Sub UserControl1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
MessageBox.Show(Me.Name, "Load")
End Sub

End Class

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top