Click here to Skip to main content
15,917,481 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi Everyone,
I have an application I'm working on and have come across a problem for which I can't find an answer.

I have created a subroutine that dynamically creates a form from a SQL Server table.

In the Save button on the BindingNavigator I have the following code:
Public Sub ImageButton_Click(ByVal sender As System.Object, _
                             ByVal e As System.EventArgs)
    For Each frm In My.Application.OpenForms
        MsgBox("Form.Tag: " & frm.Tag & " Form Name:" & frm.Name)
End Sub

The code is activated though the AddHandler method. All this code does is show all open forms in my application.

When you press the Save button on ANY of my dynamically generated forms you will get a message box showing the Form.Tag and Form.Name properties in the order they were generated starting with my LogIn form.

The LogIn form was created using the GUI designer and the Name property was set to frmLogIn. According to what I found on the Internet you cannot use the Name property on dymanically created forms; you must use the Tag property. So I tagged the Customers Form, Categories Form, and the Employees Form using their table names.

Which brings me to the problem I'm having: How can I click on the Save button on the BindingNavigator control on the Employee dynamically generated form and it show me only the message box containing the Form.Tag and Form.Name properties on the Employees Dynamic Form?

I took screen captures of the generated forms and the Message Boxes returned from the ImageButton_Click event code, but currently there is no way to incorporate them into this question.

I thank you for any assistance you can offer,

To start with: Name property is used by the designer and makes no sense for controls generated beyond the designer. If someone tells "don't use Name", it simply means that you cannot rely on those values. But it you know that those properties are not touched by the designer and assign values to them by yourself, why not? it would be even better that Tag.

But I doubt you ever need those properties. You should not try to identify anything by name, as this is not supportable. You already have a reference to each form you create and get a reference to it when you create one. Use the referenced to the form: it's the best for performance and the easiest.

Another advice is: don't create multiple forms in one application, this is not good for usability. Instead, have only one main form (I don't count few modal forms here; they would be fine for some adequate purposes). What to do with what you have now as separate forms? Make then different panels, or tab pages. You can put them all in one form and hide/show then, use then in accordion-like designs; use them in a TabControl; after all, you can even make a dockable design, pretty much as the one of Visual Studio.

Share this answer
MRM256 27-May-13 11:35am    
Oh how I wish I could show you the images.
The idea behind the dynamically generated forms was to have the ability to go to ANY SQL Server DB and show a form based on the DB table. Once the form is generated, the BindingNavigator control will let the user edit any record, save changes or add a new records. Then save these changes back to the DB.
Sergey Alexandrovich Kryukov 27-May-13 12:59pm    
Absolutely! The idea itself is good. I still advice you to make panels instead the forms. Functionality related to data and events would be the same, but UI would be much better. Anyway, I don't see the problem. You apply the binding to a control, not a form. When you create a control, you got a reference to it. Keep all control references when you create them (say, in some collection, or anything) and manipulate them when you need to do anything with binding. Is the point clear? If not, what is your problem?
MRM256 27-May-13 16:23pm    
Is there a way I can send you a Word Document containing the images I mentioned? I understand the Idea of panels. When I generate the Form I'm using a TableLayoutPanel to host the controls as well as display the data.

Maybe if you can visually see what I'm doing and you can show me an image of what you have in mind I can solve the problem.

Sergey Alexandrovich Kryukov 27-May-13 19:55pm    
You can find a way to contact me if you look at my CodeProject profile page...
Maciej Los 27-May-13 15:45pm    
Another very good solution/suggestion!
private void button1_Click(object sender, EventArgs e)
    Form form = new Form();
    form.Location = new Point(350, 350);
    form.Width = 200;
    form.Height = 200;
    int usedHeight = 0;
    Label label = new Label();
    label.Text ="lable1";
    label.Top = usedHeight + 7; label.Left = 5;
    TextBox textBox = new TextBox();
    textBox.Text =" ";
    textBox.Top = usedHeight + 5; textBox.Left = 80;
    textBox.Width = 115;
    textBox.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
    usedHeight += textBox.Height + 5;
Share this answer
MRM256 27-May-13 11:41am    
My subroutine generates the entire form dynamically. All Labels, buttons, images, and so on.
What I'm looking for is: I click on the Save button on the BindingNavigator control. The application will know which form's Save Button I clicked on and only show me the Form.Tag and Form.Name properties for THAT form only; not the other three as well.
[no name] 29-May-13 8:11am    
Can you describe more. Not clear your requirement.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900