Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET General (http://www.xtremevbtalk.com/-net-general/)
-   -   strange behavior of a inherited class (http://www.xtremevbtalk.com/-net-general/328703-strange-behavior-inherited-class.html)

VB_Alien 08-04-2017 09:53 PM

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.

passel 08-07-2017 12:05 PM

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.

VB_Alien 08-08-2017 06:10 PM

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

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.

Goggy 08-09-2017 01:47 AM

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...

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.


<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)
            If disposing AndAlso components IsNot Nothing Then
            End If
        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.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
        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.Name = "Form1"
        Me.Text = "Form1"

    End Sub

Do you see what the problem is? the item is added once in the sub new and once in the designer....

VB_Alien 08-09-2017 10:50 AM

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.

Goggy 08-10-2017 12:20 AM

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


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

    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..

VB_Alien 08-10-2017 05:29 PM

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.

Goggy 08-11-2017 01:01 AM

Your rite, i didnt test it well enough...
Let me get back to you...
This schouldnt be such a problem...

Goggy 08-11-2017 03:57 AM

Would you be so kind as to try the following?

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
        End If
    End Sub

End Class

Lets see if i tested this enough... ;)

VB_Alien 08-11-2017 11:05 AM

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

VB_Alien 08-12-2017 01:09 AM

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

Goggy 08-12-2017 02:41 AM


Originally Posted by VB_Alien (Post 1411034)
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. :D

VB_Alien 08-12-2017 06:19 PM

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

Goggy 08-13-2017 06:57 AM

:whoops: :chuckle: :D
No need to feel bad my friend...
English isnt my native language, so once every while i misinterpret stuff...
But its all goooood :D

All times are GMT -6. The time now is 06:01 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2018 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.