5 Replies - 981 Views - Last Post: 26 January 2014 - 10:37 AM Rate Topic: -----

#1 KKUser  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 10-November 13

User Control Update

Posted 21 January 2014 - 12:28 PM

I have User Control (I just learning the advantages of user controls) which give a property to hide/show the Up - Down buttons on NumericalUpDown. When I change User Control properties in User Control Test Container the control is not updating

Imports System.ComponentModel
Public Class Numeric_No_Button
 

    Public Button_Show As Boolean '= True

 
    <System.ComponentModel.Description("Show or Hide Up & Down Buttons.")> _
    Public Property ShowButtons As Boolean 'Adds ShowButtons to Control Properties
        Get
            Return Button_Show
        End Get
        Set(ByVal value As Boolean)
            Button_Show = value
 
            Update() ' Make Changes
           
        End Set
    End Property
 
  
 
   
    Private Sub Update()
        If Button_Show = True Then ' Show buttons
            NumericUpDown1.Controls(0).Show()
        Else 'Hide Buttons
            NumericUpDown1.Controls(0).Hide()
        End If
    End Sub
 

    Private Sub Numeric_No_Button_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If Button_Show = True Then ' Show buttons
            NumericUpDown1.Controls(0).Show()
        Else 'Hide Buttons
            NumericUpDown1.Controls(0).Hide()
        End If
 
    End Sub
 
   
End Class




The Problems I have are:-

1) I would like to include the properties from the Numerical up & down in the control properties or display only the Numerical up & down properties & not all the control properties.

2) When I run the User Control Test Container & change "ShowButtons" property the button in the designer does not update.

I am new to User Controls & hoping it is silly novice misunderstanding

Is This A Good Question/Topic? 0
  • +

Replies To: User Control Update

#2 thecoat  Icon User is offline

  • D.I.C Regular

Reputation: 108
  • View blog
  • Posts: 382
  • Joined: 07-December 13

Re: User Control Update

Posted 22 January 2014 - 10:56 AM

What is NumericUpDown1? I just tried a quick user control with an exposed property hiding child controls to make sure it worked at design time without any extra effort, in some cases you do need to use elements of the System.ComponentModel.Design namespace to modify design time behavior, but it looks as if you are just trying to have the same behavior at both runtime and design time.

I'm left wondering if maybe you just need to use the visible property for the controls instead of the show method:

    Private Sub Update()
        If Button_Show = True Then ' Show buttons
            NumericUpDown1.Controls(0).visible = True
        Else 'Hide Buttons
            NumericUpDown1.Controls(0).Visible = false
        End If
    End Sub



Or to be more concise.

    Private Sub Update()

            NumericUpDown1.Controls(0).Visbile = Button_Show

    End Sub


Side note when using boolean values in an if conditional statement you don't need to implicitly state "= True"
If Button_Show Then


Or "=False" for that matter:
If Button_Show = False Then

If Not Button_Show Then

Was This Post Helpful? 0
  • +
  • -

#3 KKUser  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 10-November 13

Re: User Control Update

Posted 22 January 2014 - 12:13 PM

Thanks for replying. The true false is a bad habit & sometime easier to read when picked up later.

NumericUpDown1 is the automatic name supplied for a new Numerical Up & Down control from toolbox. (will rename later)

As I said I am relatively new to writing control & am experimenting. Could explain "exposed property hiding child controls to make sure it worked at design" in more depth.
Was This Post Helpful? 0
  • +
  • -

#4 thecoat  Icon User is offline

  • D.I.C Regular

Reputation: 108
  • View blog
  • Posts: 382
  • Joined: 07-December 13

Re: User Control Update

Posted 22 January 2014 - 04:04 PM

Was playing around a bit with this, I'm pretty sure the original code you posted should work, and in fact sort of does. At design time I had no problems with the arrow visibility switching in design mode. Note that in design mode, setting the property to false does not change the visibility in the designer (which is fairly normal behavior for controls), but it does dictate the initial visibility at runtime. One thing worth doing I suspect is changing

Public Button_Show As Boolean


to

Private Button_Show As Boolean


You don't want Button_Show exposed as a public field because if you change it's value, it doesn't run the "Set" method that calls the update method. Also, you should probably choose a different name for

Private Sub Update


It conflict with an inherited method of control, and I thought this might be the issue, but after changing it, it didn't affect things.

Ultimately I ended up dropping the numericupddown control on a form and tried things outside a user control. At run time when you hide or set the arrows invisible they don't disappear. If you start with them hidden then show them, they do appear. If they are visible and you hide them, then mimimize the form and look at it again they are replaced with a black box. It appears to be a repaint problem. I tried various control invalidate and refresh calls. Haven't found a solution yet. I should note I'm version 4.0 of the control, haven't tried it with other versions.
Was This Post Helpful? 0
  • +
  • -

#5 KKUser  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 10-November 13

Re: User Control Update

Posted 24 January 2014 - 12:10 PM

Thanks for the help.

I probably tried a difficult control to start with to learn a bit,

I am though glad that you are even have issues as this means I am not being stupid.

I will keep trying & post if any changes happen.

(I am using VS2012 & 4.5 .net)
Was This Post Helpful? 0
  • +
  • -

#6 thecoat  Icon User is offline

  • D.I.C Regular

Reputation: 108
  • View blog
  • Posts: 382
  • Joined: 07-December 13

Re: User Control Update

Posted 26 January 2014 - 10:37 AM

Okay figured out what's going on with the black box. It's a shadow from the control repaint, ie the control makes space for it even though it is set invisible.


The way I found around this is to create a new control inheriting from the original overriding the text box resize event:
Imports System.Windows.Forms
Imports System.Drawing

Public Class EnhancedNumericUpdown
    Inherits NumericUpDown

    Protected Overrides Sub OnTextBoxResize(ByVal source As Object, ByVal e As System.EventArgs)
        Select Case Controls(0).Visible
            Case True
                Controls(0).Size = New Size(16, ClientSize.Height)
                Controls(1).Size = New Size(ClientSize.Width - 16, ClientSize.Height)
            Case False
                Controls(0).Size = New Size(0, 0)
                Controls(1).Size = New Size(ClientSize.Width, ClientSize.Height)
        End Select
    End Sub

End Class

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1