Hi all!

I'm creating a program that opens a small form for each row in a database (each form is an instance of Notebox). The user can create a new Notebox by clicking on a button (noteboxSibling) at the bottom of each existing Notebox. All Noteboxes exist inside Form1 which has been expanded to fill the entire screen.

My problem is that right now when you click on the button to make a new Notebox (noteboxSibling), it creates the new Notebox INSIDE the existing Notebox. My attempts to get the new Notebox to appear inside Form1 have so far all been failures. I can get a new Notebox to appear outside of both Form1 and the 'parent' Notebox, or inside of the 'parent' Notebox, but not inside of Form1 (as a 'peer' to the existing Noteboxes).

I hope I haven't made a mess of explaining that... I'm obviously doing something very silly. I've searched many forums for help with this problem and could only find a few tertiary related solutions.

Here's the code I'm currently using (I'm running .NET4 and VisualStudio 2010):

For Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Kimpl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Notebox noteboxSibling = new Notebox();
            noteboxSibling.TopLevel = false; // Renders the new Notebox instance a non top level control, making it sit inside Form1
            this.Controls.Add(noteboxSibling); // Adds new Notebox instance to form as a control
            noteboxSibling.Show(); // Shows the new Notebox instance

        }

    }
}

And for Notebox.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Kimpl
{
    public partial class Notebox : Form
    {
        public Notebox()
        {
            InitializeComponent();
        }

        private void boxenBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.boxenBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.noteboxDataSet);

        }

        private void Notebox_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'noteboxDataSet.boxen' table. You can move, or remove it, as needed.
            this.boxenTableAdapter.Fill(this.noteboxDataSet.boxen);

        }

        private void noteBoxSibling_Click(object sender, EventArgs e)
        {

            Notebox noteboxSibling = new Notebox();
            noteboxSibling.TopLevel = false; // Renders the new Notebox instance a non top level control, making it sit inside Form1
            this.Controls.Add(noteboxSibling); // Adds new Notebox instance to form as a control
            noteboxSibling.Show(); // Shows the new Notebox instance
            noteBoxSibling.BringToFront(); // Focuses on the new Notebox instance

        }

    }
}

Thank you so much for reading this. I'm still trying to learn enough to not be dangerous ;)

What I can make up out of your explanations, is that you need some sort of MDI interface. For a Starter look here: http://msdn.microsoft.com/en-us/library/ms644908(VS.85).aspx

commented: Good answer! Could've used a little more information on how it would've helped (just a line of summary would've been great), but I was able to dig through and find what I needed. +1

What I can make up out of your explanations, is that you need some sort of MDI interface. For a Starter look here: http://msdn.microsoft.com/en-us/library/ms644908(VS.85).aspx

ddanbe,
I just spent a wonderful hour reading up on MDI. It's quite robust and would definitely be a solution to my problem. I have one question: it seems like overkill for a simple program that's just going to display a few textfields from a database per each small form. Is there a simpler way to solve my problem, or is implementing MDI the only realistic option?

Thank you so much for replying!

ddanbe,

I went with the MDI and everything is now working perfectly. Thank you so much for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.