Windows Develop Bookmark and Share   
 index > Windows Forms Designer > ideas for composite control (is this possible?)
 

ideas for composite control (is this possible?)

hiya,
I have a winform that contains 2x userCtrls:

<layout>
 userCtrlJOB (displays the values of the "job" biz object)
 userCtrlPERSON (displays the values of the "person" biz object)
<\layout>

Instead of including a "NEW" button within each userCtrl..I was hoping that I could create a single "NEW"button, that when clicked would recognise the currently "active" userCtrl.


eg, when I click on the "new" button, is it possible for the form to be aware of  which one of the userCtrls is active?I could then write logic to display the biz object data-entry form that corresponds to the entity represented by the "active" userCtrl.

eg, the user clicks on the "NEW" button.The app recognises that the userCtrlJOB is active, and therefore displays a JOB data-entry form.

I have simplified this a bit for clarity..Can I clarify anything?

Many thanks, 
yogiberr
 



MigrationUser 1  Tuesday, October 19, 2004 6:37 PM
Hi,

Basicly this can be done in 2 ways

The first option is to Create a usercontrol (eg newAbleUsercontrol) 
with a abstract(must overide) sub CreateNew
inherit your controls from that NewAbl;eUsercontrol in stead of usercontrol
overide the createnew sub

The second option is to make a interface class eg Contract_newAble
like so


Public Interface Contract_Newable
    Sub CreateNiew()
End Interface

 
and implement the interface in  (both)  usercontrol
like so

Public Class UserControlJOB
    Inherits System.Windows.Forms.UserControl
    Implements Contract_Newable
    'designer generated code omitted
    
    Public Sub CreateNiew() Implements Contract_Newable.CreateNiew
        'do your new code here
        MsgBox("Some new Job")
    End Sub
    'your code


Now your done on the usercontrols

Put in your form a private variable (eg _Selectedcontrol) with the type of your interface clas(2nd option) or your baseclas (1st option)

like so


Public Class Form1
    Inherits System.Windows.Forms.Form
    Private _SelectedControl As Contract_Newable
      'designer generated code omited


In your enter Event of the controls, cast the sender to your baseclass / interface class
and store it in the private varible _Selectedcontrol

   
   Private Sub Uctl_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Job.Enter, Person.Enter
        _SelectedControl = CType(sender, Contract_Newable)
    End Sub


on the button click on your form Simply Cal the createniew on the private varible


    Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
        _SelectedControl.CreateNiew()
    End Sub


hope this is clear 

Remco

MigrationUser 1  Friday, October 22, 2004 7:55 AM
thanks Remco, 
I've made a good bit of progress :-) but I still get an error when I click on the "NEW" button.
I followed your advice and used the "interface" method.


<error>
object refence not set to an instance of an object
<\error>


<formCode>
Public Class Form1
    Inherits System.Windows.Forms.Form
    Private _SelectedControl As iCreate    'the interface that I made.

 Friend WithEvents UcPerson1 As projGenericNew.ucPerson
 Friend WithEvents UcJob1 As projGenericNew.ucJob
Friend WithEvents btnNew As System.Windows.Forms.Button


  Private Sub UcJob1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs)
        _SelectedControl = CType(sender, iCreate)
    End Sub

    Private Sub UcPerson1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles UcPerson1.Enter
        _SelectedControl = CType(sender, iCreate)
    End Sub


    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        _SelectedControl.CreateNew()
    End Sub
<\formCode>

I think that I am nearly there..Do you know where I've made the mistake?

ta  :-)
yogiberr



MigrationUser 1  Friday, October 22, 2004 11:59 AM
is it the createnieuw that gives a error or is there something in your sub 
that creates the error

i can't check right now (i'm not @ the office) it's now 2 in the morningover here :)



Remco
MigrationUser 1  Friday, October 22, 2004 8:04 PM
Maybe you removed the lines 

UcPerson1 = new  projGenericNew.ucPerson 
Ucjob1 = new  projGenericNew.ucjob 

dunno, just guesing

i think your missing here  a handles part (handles ucjiob1.enter)
Private Sub UcJob1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) 
_SelectedControl = CType(sender, iCreate) 
End Sub 


Yo can make a handler for both usercontrols like so
Private Sub UcPerson1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles UcPerson1.Enter , UCJob.enter
_SelectedControl = CType(sender, iCreate) 
End Sub 
</Code>

Don't  got .net installed on this computer , so i can verify what's your error, not until monday.

Cheers Remco
MigrationUser 1  Saturday, October 23, 2004 6:01 AM
hi Remco, 
Thanks for sticking with this :-)

My peoblem was that I was decalring the userCtrls on the parent form as:

    Friend WithEvents UcPerson1 As projGenericNew.ucPerson
    Friend WithEvents btnNew As System.Windows.Forms.Button

..instead of as:
 Friend WithEvents UcJob1 As projGenericNew.ucJob = New projGenericNew.ucJob
    Friend WithEvents UcPerson1 As projGenericNew.ucPerson = New projGenericNew.ucPerson
<\code>

So, as a result, the userCtrl "enter" event wasn't getting handled on the form..

Many thanks..
yogi 
MigrationUser 1  Sunday, October 24, 2004 8:33 AM
You welcome

Remco
MigrationUser 1  Monday, October 25, 2004 4:30 AM

You can use google to search for other answers

Custom Search

More Threads

• forms closing event firing twice
• Trigger on Group Insert or Group Delete
• Help with Data Export via VS2005
• Problems Serializing Object Properties
• Mouse wheel and scroll bar
• Inheriting DataNavigator
• Custom component selection problem
• Getting notified when inherited property changed at design time
• Why My Custom ToolboxItem did not work?
• Creating an bitmap from designer host surface.