Programmatically (Many different controls w/ Handlers)

jwcoleman87

Well-known member
Joined
Oct 4, 2014
Messages
124
Programming Experience
Beginner
        Dim CommentsTab As New TabPage With {.Name = "Comments", .Text = "Add Comments"}
        TabControl1.TabPages.Add(CommentsTab)
        Dim flpcomments As New FlowLayoutPanel With {.Parent = TabControl1.TabPages("Comments"),
                                                     .Dock = DockStyle.Fill,
                                                     .BorderStyle = BorderStyle.Fixed3D}
        Dim txtComments As New TextBox With {.Parent = flpcomments}
        Dim btnSave As New Button With {.Parent = flpcomments, .Text = "Save"}


So lets say I create a tab page, assign it to a tab control, add a flowlayoutpanel to that tabpage, and create a textbox and button in that flowlayoutpanel in that tabpage in that tab control.

I began writing a handler that would handle the clicking of the save button, but then I realized I was missing the textbox part. The comment would be contained in the textbox and would be needed to be passed as a parameter to whatever function the handler will call in order to "save a comment"

I started off like this:

    'Handlers for generated form controls
    Private Sub SaveComment(ByVal sender As Object, ByVal e As EventArgs)
        Dim rc As New RepairControl
        rc.SetStatusAndAssignment(lblSelectedSN.Text, lblStatus.Text, txtComments.Text)
    End Sub


But the txtComments.Text parameter obviously doesn't work because it's created in a programmatic way. As far as this sub is concerned it doesn't exist, but it does at run-time of course. If I use this sub as a handler for the button, sender becomes the button object, but my questions is how do I pass the comments from the text box. I have a feeling it has something to do with E as eventargs. Perhaps I can use the textbox.text as the eventargs parameter?
 

jwcoleman87

Well-known member
Joined
Oct 4, 2014
Messages
124
Programming Experience
Beginner
    Private Sub SaveComment(ByVal sender As Object, ByVal e As EventArgs)
        Dim rc As New RepairControl
        rc.SetStatusAndAssignment(lblSelectedSN.Text, Me.User, lblStatus.Text, DirectCast(sender.Parent, FlowLayoutPanel).Controls("CommentBox").Text())
    End Sub


This worked, but I wonder if there is a robust way to do this. This relies on the text box being named "CommentBox"
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
You can for example do this:
Dim evtControl = CType(sender, Control)
Dim tbox = evtControl.Parent.GetNextControl(evtControl, True)

Other options I can think of right now:
  • You could also utilize the buttons Tag property to store reference to the textbox.
  • If there are multiple groups of button+textbox to be placed in panel you could design them as a single UserControl to be easier to handle as a unit.
  • FlowLayoutPanel.Controls.GetChildIndex that can be used to get next control at a following index.

I also recommend you turn on Option Strict, this code "sender.Parent" shows you hasn't.
 

jwcoleman87

Well-known member
Joined
Oct 4, 2014
Messages
124
Programming Experience
Beginner
Thank you, I will try using the tag solution tomorrow, that way I can define everything at run-time (which is what I'm going for)

What I'm trying to accomplish looks like this:

Capture.JPG

As you can see there, a serialnumber is selected in the working units list and it displays itself in the Unit actions group box, the status of the unit is also displayed.

Ultimately, I want to define what actions are available depending on which processID is held in the database. In unit repair I want the technicians to have access to a tab that allows them to order parts, in triage I want them to be able to select a root cause, in screening I want them to be able to give it a refurbishment grade.

Right now I'm experimenting with creating these tabs at runtime so that ultimately, depending on what the database will dictate. (What department are you in, which warehouse, what process step is the current selected repair in) - Okay you have these options available to work from.

I've been working to expand my teletrack application to be robust enough to be multi-departmental and multi-warehouse. For this to happen I need to move the framework to the database (all the stuff that determines what you can do when), and for the client to support it I need to figure out how to do all of this in a programmatic way that doesn't look like crap.

I appreciate your response and I will implement one of your solutions as soon as tomorrow. I'll let you know how it goes!
 
Top Bottom