strange behavior of a inherited class
strange behavior of a inherited class
strange behavior of a inherited class
strange behavior of a inherited class
strange behavior of a inherited class
strange behavior of a inherited class strange behavior of a inherited class strange behavior of a inherited class strange behavior of a inherited class strange behavior of a inherited class strange behavior of a inherited class strange behavior of a inherited class strange behavior of a inherited class
strange behavior of a inherited class strange behavior of a inherited class
strange behavior of a inherited class
Go Back  Xtreme Visual Basic Talk > > > strange behavior of a inherited class


Reply
 
Thread Tools Display Modes
  #1  
Old 08-04-2017, 10:53 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default strange behavior of a inherited class


I'm trying to make a new tool using a class
and inheriting a combobox.

I was having issues with my combobox adding
items twice when clearly, in the code, it's only
being added one time.

All i did was create the class, then i inherited the
combobox. Then i created a public sub new()
and put my code in there to add items to the
Inherited combobox. Me.items.add. I assume
that using ME is correct since i get no errors.
Still though, no matter what i do, items get
added twice to the combobox.

Still using the sub new sub, i commented out all my
code and replaced that with a msgbox. What i found
out is that sub new gets called on three times. It gets
called when the control is placed on the form. It gets
called again when the form loads and again when the
form closes. I thought that sub new only got called
once, when the control is placed on the form.

Am i going about creating a new tool/control the
wrong way? Should i be using a user control to
do this with?

Either way, i'd still like to know why sub new gets called
three times, if anyones knows the answer that is.
Reply With Quote
  #2  
Old 08-07-2017, 01:05 PM
passel's Avatar
passelstrange behavior of a inherited class passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

Well, I don't know but I would guess that it gets called whenever the IDE's version of the form is recreated or changed, i.e. when you add the control, or when you exit debugging and return to the IDE. Of course, you would expect it to be called when you start the executable as the controls are created for the run time.

I guess without researching, a work around would be to check to see if the items collection already has data before executing the code to add data to the list in the new sub.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #3  
Old 08-08-2017, 07:10 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

I always thoughtt that when placing a control class
on a form, that was the only time that sub new is
fired.

Maybe i am wrong but it makes sense to me
because that is the time when the control is
actually being created.

I've searched around for an answer to this
but i havent found anything good yet. Your
answer is better than what i have so far

I was trying to load data into a combobox
in sub new but the data gets loaded at least
twice and sometimes three times, just because
i placed the control on the form.

I guess sub new isn't a good place to load item data
into a combo or listbox.
Reply With Quote
  #4  
Old 08-09-2017, 02:47 AM
Goggy's Avatar
Goggy Goggy is offline
Contributor
 
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 670
Default To put my 2 cents in

I suspect it has to do with how the controls are serialized.

As with your instructions i made a small class like the following...
Code:
Public Class MyCombobox
    Inherits System.Windows.Forms.ComboBox

    Public Sub New()
        Me.Items.Add("This should be the only item")
        MsgBox("Sub New called")
    End Sub
End Class
Then i build the project, and dragged the new control to the form.
Now let's take a look at the designer file it created for the form.

Code:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    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()
        Me.MyCombobox1 = New TestI.MyCombobox() <----- Sub new is called only once....
        Me.SuspendLayout()
        '
        'MyCombobox1
        '
        Me.MyCombobox1.FormattingEnabled = True
        Me.MyCombobox1.Items.AddRange(New Object() {"This should be the only item"}) <---------- Take a look at this line (its serialized in there.)
        Me.MyCombobox1.Location = New System.Drawing.Point(81, 98)
        Me.MyCombobox1.Name = "MyCombobox1"
        Me.MyCombobox1.Size = New System.Drawing.Size(121, 21)
        Me.MyCombobox1.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 261)
        Me.Controls.Add(Me.MyCombobox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub
Do you see what the problem is? the item is added once in the sub new and once in the designer....
__________________
As finishing touch, god created the Dutch!

utterly useless, but always trying to help

Last edited by Goggy; 08-09-2017 at 03:13 AM.
Reply With Quote
  #5  
Old 08-09-2017, 11:50 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

That's funny because i looked in the designer
several times and it didn't dawn on me that that
was a problem. Live and learn i guess.

I was trying to recreate the vb6 drive list box but
using a class isnt the way to go in this case. I'll
just use a user control instead. I was trying to
avoid that if possible because i'd have to hide a
lot of properties from intelesense.

Anyways, Thanks guys for the replies.
Reply With Quote
  #6  
Old 08-10-2017, 01:20 AM
Goggy's Avatar
Goggy Goggy is offline
Contributor
 
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 670
Default

You can still do that, and in the way you thought it should go...
Take a look at this simple class...

Code:
Public Class MyCombobox
    Inherits System.Windows.Forms.ComboBox

    Public Sub New()
        Dim AllDrives() As System.IO.DriveInfo = System.IO.DriveInfo.GetDrives()
        Dim Drive As System.IO.DriveInfo

        For Each Drive In AllDrives
            Me.Items.Add(Drive.Name)
        Next

    End Sub
End Class
The items are loaded only once. This because the dynamic variable's can't be serialized. So they don't end up in the disigner... and there only loaded once the control gets created..
__________________
As finishing touch, god created the Dutch!

utterly useless, but always trying to help
Reply With Quote
  #7  
Old 08-10-2017, 06:29 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Thats about the same code i was using to load
the combobox with the drives.

This code is doing the same thing. Its loading
the drive letters twice.

I dont think that using a class for this project
is going to work because i have to use sub new
to load the drives.

Thanks for trying though. I appreciate the help.
Reply With Quote
  #8  
Old 08-11-2017, 02:01 AM
Goggy's Avatar
Goggy Goggy is offline
Contributor
 
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 670
Default

Your rite, i didnt test it well enough...
Let me get back to you...
This schouldnt be such a problem...
__________________
As finishing touch, god created the Dutch!

utterly useless, but always trying to help
Reply With Quote
  #9  
Old 08-11-2017, 04:57 AM
Goggy's Avatar
Goggy Goggy is offline
Contributor
 
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 670
Default Would you be so kind as to try the following?

Code:
Public Class MyCombobox
    Inherits System.Windows.Forms.ComboBox

    ''' <summary>
    ''' Can't use the sub New, because the designmode bit isn't set yet.
    ''' Only Add the item's when in design mode, when the user drag & drops the control to the form.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub MyCombobox_HandleCreated(sender As Object, e As EventArgs) Handles Me.HandleCreated
        If DesignMode Then
            Me.Items.AddRange(System.IO.DriveInfo.GetDrives())
        End If
    End Sub

End Class
Lets see if i tested this enough...
__________________
As finishing touch, god created the Dutch!

utterly useless, but always trying to help
Reply With Quote
  #10  
Old 08-11-2017, 12:05 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

That actually worked. The drives are
only loading once when placed on the
form. I really didnt think it would be
possible to use a class for this control
but you proved me wrong. Thankyou
for your time and hard work.

I never knew there was a designmode
variable. Learned another trick. Thanks
Reply With Quote
  #11  
Old 08-12-2017, 02:09 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Well. I replied back hours ago. Aparently
my reply didnt take.

Anyway, your lastest code worked like
A charm. I wasn't aware that there was
a variable to test if the program is in
design mode or not.

Thanks for your help Goggy. I appreciate
It.
Reply With Quote
  #12  
Old 08-12-2017, 03:41 AM
Goggy's Avatar
Goggy Goggy is offline
Contributor
 
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 670
Default

Quote:
Originally Posted by VB_Alien View Post
Well. I replied back hours ago. Aparently
my reply didnt take.
Sorry about that, i wasn't online last night.
But anyway, Your very welcome, and thank you for the puzzle.
__________________
As finishing touch, god created the Dutch!

utterly useless, but always trying to help
Reply With Quote
  #13  
Old 08-12-2017, 07:19 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Unhappy

You misunderstand goggy. I wasn't
complaining about you, i was complaining
because i came back hours later, only
to find out that my reply to you did not
Get posted. Or so i thought. After i made
my last post, i looked and there was my
reply to you.

I'm using a cellphone to post with and
they do funny things when online.

I wasn' complaining about you at all
and now i feel bad. Sorry about the
misunderstanding.
Reply With Quote
  #14  
Old 08-13-2017, 07:57 AM
Goggy's Avatar
Goggy Goggy is offline
Contributor
 
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 670
Default


No need to feel bad my friend...
English isnt my native language, so once every while i misinterpret stuff...
But its all goooood
__________________
As finishing touch, god created the Dutch!

utterly useless, but always trying to help
Reply With Quote
Reply

Tags
combobox, called, form, control, class, inherited, code, items, times, added, answer, anyones, loads, user, creating, tool/control, wrong, behavior, closes, errors, time, created, create, inheriting, adding


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
strange behavior of a inherited class
strange behavior of a inherited class
strange behavior of a inherited class strange behavior of a inherited class
strange behavior of a inherited class
strange behavior of a inherited class
strange behavior of a inherited class strange behavior of a inherited class strange behavior of a inherited class strange behavior of a inherited class strange behavior of a inherited class strange behavior of a inherited class strange behavior of a inherited class
strange behavior of a inherited class
strange behavior of a inherited class
 
strange behavior of a inherited class
strange behavior of a inherited class
 
-->