Solved

Deriving from ComboBox won't allow toggling Enabled at design time with VB.NET

Posted on 2006-12-01
4
240 Views
Last Modified: 2010-04-23
In VB.NET 2005, create the following derived component is a solution that has 2 projects, one for Library, one for test application.
Drag a JunkComboBox component from Toolbox to Form1.
Using the Properties window, toggle Enabled property back and forth. No Problem.
Then toggle Read_only property from False to True.  It won't toggle.
Adding button with event handler does work.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        JunkComboBox1.Read_only = Not JunkComboBox1.Read_only
    End Sub

The 500 point question is why not, and more importantly, how to add a property which toggles the enabled property at design time.
The key point is that it doesn't modify the enabled property at design time.
I also know that the signature should be: Public Property [Readonly] as Boolean, but don't want discussion to focus on that.
-----------------

Imports System.Windows.Forms

Public Class JunkComboBox
    Inherits ComboBox

    Public Property Read_only() As Boolean
        Get
            Return Not Enabled
        End Get
        Set(ByVal value As Boolean)
            Enabled = Not value
        End Set
    End Property
End Class
0
Comment
Question by:brotherbill999
  • 2
  • 2
4 Comments
 
LVL 34

Expert Comment

by:Sancler
ID: 18058191
I've not actually set up the project, but this looks like the truth table that the code would produce.

Enabled starts off as true
Readonly starts off as false
Set means Enabled = not false = true
Get Readonly returns Not enabled = false
No change to either Enabled or Readonly


Enabled starts off as false
Readonly starts off as false
Set means Enabled = not false = true
Get readonly returns not enabled = false
Change in Enabled but not in Readonly


Enabled starts off as True
Readonly starts off as true
Set means enabled = not true = false
Get readonly returns Not enabled = true
Change in enabled but not in readonly


Enabled starts off as false
Readonly starts off as true
Set means enabled = not true = false
Get readonly returns not enabled = true
No change to either Enabled or Readonly

If that's correct, the value of readonly is never changed.  The code is different from        

   JunkComboBox1.Read_only = Not JunkComboBox1.Read_only

Or am I missing the point here?

Roger
0
 

Author Comment

by:brotherbill999
ID: 18058427
Let's set readonly to True
Then Enabled = Not value, thus Enabled = Not True, thus False
Get Readonly = Not Enabled, thus Not False, thus True.
So your truth table is not quite true.

All I am attempting to do is to have another Property having the name: Readonly
which is Not Enabled.  This is so that users can use the Readonly property in a derived component consistently.
I don't want to have to educate users to use Not Enabled, rather that Readonly
0
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 18059668
My post

>>
Enabled starts off as True
Readonly starts off as true
Set means enabled = not true = false
Get readonly returns Not enabled = true
Change in enabled but not in readonly
<<

Your post

>>
Let's set readonly to True
Then Enabled = Not value, thus Enabled = Not True, thus False
Get Readonly = Not Enabled, thus Not False, thus True.
<<

I don't see the difference, except in the words used, to describe the same situation.  In both cases, Readonly starts off as TRUE and ends up as TRUE.  Readonly has not had its value changed.  But you say you want to TOGGLE Readonly.  The reason it is not TOGGLING is because the logic as set out above is that its original value is NOTted twice, once in the set and once in the get.

So, in terms of the "truth table" itself.  I beg to differ.

But, having now tested your code, I reckon that this is not where the problem lies.  There are two issues.  

First, the logic of the truth table is wrong (or at least misleading).  This is because, when the property is "toggled" in the properties window the "value" that is sent to the Set is the NEW value.  That is, if the current value displayed is TRUE, there is only any action if it is the FALSE that is then clicked in the properties window.  So it is FALSE that is sent as the value to the Set, which means that the property is set to NOT FALSE = TRUE, so the Get then returns NOT TRUE = FALSE, and that is the value that is then displayed in the properties window.  TRUE was displayed.  FALSE is now displayed.  It has toggled. To illustrate try this

Imports System.Windows.Forms

Public Class JunkComboBox
    Inherits ComboBox

    Private m_readonly As Boolean

    Public Property Read_only() As Boolean
        Get
            Return Not m_readonly
        End Get
        Set(ByVal value As Boolean)
            m_readonly = Not value
        End Set
    End Property

End Class

That works - there are, in total, from the visual display in the properties window, three NOTs, not TWO.  The "extra" one has been introduced by clicking the alternative to the current display which, as we are dealing with Booleans, is NOT the current display.  Now change

        Get
            Return Not m_readonly
        End Get

to

        Get
            Return m_readonly
        End Get

That doesn't work.  We are back to an even number of NOTs, so nothing apparently happens.

But the real issue, as I now see it, is that the designer simply will not recognise changes to a property unless it is made through the properties window.

Try this

Imports System.Windows.Forms

Public Class JunkComboBox
    Inherits ComboBox

    Private m_readonly As Boolean

    Public Property Read_only() As Boolean
        Get
            Return Not m_readonly
        End Get
        Set(ByVal value As Boolean)
            m_readonly = Not value
            Enabled = m_readonly
        End Set
    End Property

End Class
 
That is the working version of the code above with a line added to set Enabled as well.  So far as I can see (both from the properties window and from building and inspecting the Designer.vb file for the form) that new line has no impact on the value of Enabled at all.  I do not know the inner workings of the IDE vis a vis the properties window.  My guess, however, is that (unlike when the code has been compiled) the designer software is only structured to make changes that are specifically input through the designer.  Setting Enabled by code within a different property's Set would not conform with that model.

I stress that this is a guess, based on observation and experiment and not on any technical knowledge.  I am far from saying that - with some more intricate knowledge of the designer software - someone might not be able to achieve what you want.  But, I am afraid, whilst I think I have identified why your current approach is not working, I cannot myself suggest how you might make it work.

Roger
0
 

Author Comment

by:brotherbill999
ID: 18060974
Hi Roger,
The trick was to use an attribute for the Get, rather than Enabled directly.
The following is what works.  The only downside is that the designer doesn't reflect the changed visually, but it does toggle, and it does work at run-time

Imports System.Windows.Forms

Public Class JunkComboBox
    Inherits ComboBox

    Protected m_readonly As Boolean

    Public Property [Readonly]() As Boolean
        Get
            Return m_readonly
        End Get
        Set(ByVal value As Boolean)
            m_readonly = value
            Enabled = Not m_readonly
        End Set
    End Property
End Class

Thanks so much.  You may not consider yourself to be an expert, but you were on this one!
Brother Bill
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Object to array 7 28
Merging PDFs with VB.NET 10 31
Vb.Net Menu 1 22
VB.Net - CSV to Oracle table 4 32
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now