Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# MDI parent form & mdi form childs questions...urgent help please!

C# MDI parent form & mdi form childs questions...urgent help please!

Scheduled Pinned Locked Moved C#
databasequestioncsharphelpannouncement
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GianlucaSeno
    wrote on last edited by
    #1

    I've written a C# winform app and I'm new on C#...but I've some experience in programming... I've some problems that I can't found anywhere a resolution... My program is designed to have a main MDI form and a lot of MDI childs...so my questions are: 1)I create dinamically menu on MDI form and I've assigned to each of the a shortcut...but for the first element (i.e. "&File" menu than I've assigned Shortcut.CTRLF) it doesn't work...why?!? the code is this: void menuBuilder(object sender, System.EventArgs e) { MainMenu mainMenu = new MainMenu(); MenuItem file = new MenuItem(); file.MergeOrder=0; file.MergeType=MenuMerge.MergeItems; file.Text="&File"; file.Index=0; file.Shortcut=Shortcut.CtrlF; mainMenu.MenuItems.Add(file); this.Menu=mainMenu; } 2)I have a MDI child open correctly in my app from MDI menu (that I'll call FORM1) and I want that from FORM1, when it's loaded, to open another MDI child (FORM2) with a datagrid inside it.To do this I do this... FORM2 newMDIChild = new FORM2(); newMDIChild.MdiParent = this.MdiParent; newMDIChild.Show(); I suppose that in this manner I can load FORM 2 as a MDI child but it not seems to work, becouse if I tested if the window is open it doesnt works...and open another window! 2a)I try to close FORM2 when I close FORM1 but I can never do it...how can I do?!?I try everything but never works... 2b)When I do some works on FORM1, I write something on an ACCESS DB and then I'd like FORM 2(that is active too) update datagrid inside it...but I try everything but nothing work!!!I try to call a method on FORM2 from FORM1 becouse I suppose that it updates my datagrid... The code is: public void ReadResults() { OleDbDataAdapter da = new OleDbDataAdapter(); OleDbConnection con =new OleDbConnection(); OleDbCommand cmd = new OleDbCommand(); DataTable dt =new DataTable(); dt.Clear(); con.ConnectionString=; con.Open(); cmd.Connection = con; cmd.CommandText = "SELECT * FROM TBL"; da.SelectCommand = cmd; da.Fill(dt); dataPreResult.SetDataBinding(dt,""); cmd.Cancel(); con.Close(); } I call this method on FORM2_Load and work fine...but if I want to reload datagrid when the FORM2 is displayed...what I've to do?!? Help me please...I know that this are very stupid questions...but I didnt find anything everywhere that can help me...

    S 1 Reply Last reply
    0
    • G GianlucaSeno

      I've written a C# winform app and I'm new on C#...but I've some experience in programming... I've some problems that I can't found anywhere a resolution... My program is designed to have a main MDI form and a lot of MDI childs...so my questions are: 1)I create dinamically menu on MDI form and I've assigned to each of the a shortcut...but for the first element (i.e. "&File" menu than I've assigned Shortcut.CTRLF) it doesn't work...why?!? the code is this: void menuBuilder(object sender, System.EventArgs e) { MainMenu mainMenu = new MainMenu(); MenuItem file = new MenuItem(); file.MergeOrder=0; file.MergeType=MenuMerge.MergeItems; file.Text="&File"; file.Index=0; file.Shortcut=Shortcut.CtrlF; mainMenu.MenuItems.Add(file); this.Menu=mainMenu; } 2)I have a MDI child open correctly in my app from MDI menu (that I'll call FORM1) and I want that from FORM1, when it's loaded, to open another MDI child (FORM2) with a datagrid inside it.To do this I do this... FORM2 newMDIChild = new FORM2(); newMDIChild.MdiParent = this.MdiParent; newMDIChild.Show(); I suppose that in this manner I can load FORM 2 as a MDI child but it not seems to work, becouse if I tested if the window is open it doesnt works...and open another window! 2a)I try to close FORM2 when I close FORM1 but I can never do it...how can I do?!?I try everything but never works... 2b)When I do some works on FORM1, I write something on an ACCESS DB and then I'd like FORM 2(that is active too) update datagrid inside it...but I try everything but nothing work!!!I try to call a method on FORM2 from FORM1 becouse I suppose that it updates my datagrid... The code is: public void ReadResults() { OleDbDataAdapter da = new OleDbDataAdapter(); OleDbConnection con =new OleDbConnection(); OleDbCommand cmd = new OleDbCommand(); DataTable dt =new DataTable(); dt.Clear(); con.ConnectionString=; con.Open(); cmd.Connection = con; cmd.CommandText = "SELECT * FROM TBL"; da.SelectCommand = cmd; da.Fill(dt); dataPreResult.SetDataBinding(dt,""); cmd.Cancel(); con.Close(); } I call this method on FORM2_Load and work fine...but if I want to reload datagrid when the FORM2 is displayed...what I've to do?!? Help me please...I know that this are very stupid questions...but I didnt find anything everywhere that can help me...

      S Offline
      S Offline
      sreejith ss nair
      wrote on last edited by
      #2

      You forgot to add click event & handler in your first block of code. You can rewrite your code and add required functionality. void menuBuilder(object sender, System.EventArgs e) { MainMenu mainMenu = new MainMenu(); MenuItem file = new MenuItem(); file.MergeOrder=0; file.MergeType=MenuMerge.MergeItems; file.Text="&File"; file.Index=0; file.Shortcut=Shortcut.CtrlF; **file.Click+=new EventHandler(file_Click);** mainMenu.MenuItems.Add(file); this.Menu=mainMenu; } private void file_Click(object sender, EventArgs e) { MessageBox.Show("Clicked"); } And i am sorry about your second queary. Because i can not figurout what you really want to achive. Sreejith Nair [ My Articles ]

      G 1 Reply Last reply
      0
      • S sreejith ss nair

        You forgot to add click event & handler in your first block of code. You can rewrite your code and add required functionality. void menuBuilder(object sender, System.EventArgs e) { MainMenu mainMenu = new MainMenu(); MenuItem file = new MenuItem(); file.MergeOrder=0; file.MergeType=MenuMerge.MergeItems; file.Text="&File"; file.Index=0; file.Shortcut=Shortcut.CtrlF; **file.Click+=new EventHandler(file_Click);** mainMenu.MenuItems.Add(file); this.Menu=mainMenu; } private void file_Click(object sender, EventArgs e) { MessageBox.Show("Clicked"); } And i am sorry about your second queary. Because i can not figurout what you really want to achive. Sreejith Nair [ My Articles ]

        G Offline
        G Offline
        GianlucaSeno
        wrote on last edited by
        #3

        But if File menu has submenus...what event I've to set for him? something like this... void menuBuilder(object sender, System.EventArgs e) { MainMenu mainMenu = new MainMenu(); MenuItem file = new MenuItem(); file.MergeOrder=0; file.MergeType=MenuMerge.MergeItems; file.Text="&File"; file.Index=0; file.Shortcut=Shortcut.CtrlF; file.Click+=new EventHandler(file_Click); mainMenu.MenuItems.Add(file); MenuItem campionato = new MenuItem(); campionato.MergeOrder=1; campionato.MergeType=MenuMerge.MergeItems; campionato.Text="&Campionato"; campionato.Click += new EventHandler(Championship_Open); campionato.Shortcut=Shortcut.CtrlC; campionato.ShowShortcut=true; file.MenuItems.Add(campionato); this.Menu=mainMenu; } private void file_Click(object sender, EventArgs e) { } Will works or...not? For second question...probably I'm confused... In brief...I've a MDI application with this MDI childs(at the moment all MDI childs are closed): 1)MDIFORM (MDI container) 2)FORM1 (MDI form child) 3)FORM2 (MDI form child) I press a menu on MDIFORM that opens FORM1. In FORM1 there is a texbox and 1 button(btnOk). On keyPress event of textbox I write in a MSAccess DB what I'm writing in a table. Then I press a button and on click event I call this method btnOk_Click: public class FORM1: System.Windows.Forms.Form { private void btnOk_Click(object sender, System.EventArgs e) { FORM2 newMDIChild = new FORM2(); newMDIChild.MdiParent = this.MdiParent; newMDIChild.Show(); } } In FORM2 I've a datagrid where I show results written before in last MSAccess table.The code is this: public class FORM2 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; private System.Windows.Forms.DataGrid dataPreResult; public FORM2() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.dataPreResult = new System.Windows.Forms.DataGrid(); ((System.ComponentModel.ISupportInitialize)(this.dataPreResult)).BeginInit(); this.SuspendLayout(); // // dataPreResult // this.dataPreResult.AllowSorting = false; this.dataPreResult.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.dataPreResult.DataMember = ""; this.dataPreResult.Dock = System

        D 1 Reply Last reply
        0
        • G GianlucaSeno

          But if File menu has submenus...what event I've to set for him? something like this... void menuBuilder(object sender, System.EventArgs e) { MainMenu mainMenu = new MainMenu(); MenuItem file = new MenuItem(); file.MergeOrder=0; file.MergeType=MenuMerge.MergeItems; file.Text="&File"; file.Index=0; file.Shortcut=Shortcut.CtrlF; file.Click+=new EventHandler(file_Click); mainMenu.MenuItems.Add(file); MenuItem campionato = new MenuItem(); campionato.MergeOrder=1; campionato.MergeType=MenuMerge.MergeItems; campionato.Text="&Campionato"; campionato.Click += new EventHandler(Championship_Open); campionato.Shortcut=Shortcut.CtrlC; campionato.ShowShortcut=true; file.MenuItems.Add(campionato); this.Menu=mainMenu; } private void file_Click(object sender, EventArgs e) { } Will works or...not? For second question...probably I'm confused... In brief...I've a MDI application with this MDI childs(at the moment all MDI childs are closed): 1)MDIFORM (MDI container) 2)FORM1 (MDI form child) 3)FORM2 (MDI form child) I press a menu on MDIFORM that opens FORM1. In FORM1 there is a texbox and 1 button(btnOk). On keyPress event of textbox I write in a MSAccess DB what I'm writing in a table. Then I press a button and on click event I call this method btnOk_Click: public class FORM1: System.Windows.Forms.Form { private void btnOk_Click(object sender, System.EventArgs e) { FORM2 newMDIChild = new FORM2(); newMDIChild.MdiParent = this.MdiParent; newMDIChild.Show(); } } In FORM2 I've a datagrid where I show results written before in last MSAccess table.The code is this: public class FORM2 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; private System.Windows.Forms.DataGrid dataPreResult; public FORM2() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.dataPreResult = new System.Windows.Forms.DataGrid(); ((System.ComponentModel.ISupportInitialize)(this.dataPreResult)).BeginInit(); this.SuspendLayout(); // // dataPreResult // this.dataPreResult.AllowSorting = false; this.dataPreResult.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.dataPreResult.DataMember = ""; this.dataPreResult.Dock = System

          D Offline
          D Offline
          david cohoon
          wrote on last edited by
          #4

          Instead of creating a new FORM2 everytime when the button is clicked, create a member variable of type FORM2 in FORM1, initializing it to null. In the event handler, test against null before creating the second form. public class FORM1: System.Windows.Forms.Form { private FORM2 _secondForm = null; private void btnOk_Click(object sender, System.EventArgs e) { if (this._secondForm == null) { this._secondForm = new FORM2(); this._secondForm.MdiParent = this.MdiParent; } this._secondForm.Show(); } } This is just a simple fix, usually I try to have my mdi container manage my form instances. In regards to your question regarding the second form accessing the first's textbox text, you could have the first form pass the second form a reference to itself. Again, this is much cleaner by having the MdiContainer manage interactions between child forms. -cursor

          G 1 Reply Last reply
          0
          • D david cohoon

            Instead of creating a new FORM2 everytime when the button is clicked, create a member variable of type FORM2 in FORM1, initializing it to null. In the event handler, test against null before creating the second form. public class FORM1: System.Windows.Forms.Form { private FORM2 _secondForm = null; private void btnOk_Click(object sender, System.EventArgs e) { if (this._secondForm == null) { this._secondForm = new FORM2(); this._secondForm.MdiParent = this.MdiParent; } this._secondForm.Show(); } } This is just a simple fix, usually I try to have my mdi container manage my form instances. In regards to your question regarding the second form accessing the first's textbox text, you could have the first form pass the second form a reference to itself. Again, this is much cleaner by having the MdiContainer manage interactions between child forms. -cursor

            G Offline
            G Offline
            GianlucaSeno
            wrote on last edited by
            #5

            Thanks for your answer...for the first question I try and it works great! :D And with the same method...I can close FORM2 from FORM1? For the second question, that's very important for me...could you give me an example how to solve this problem?I dont have so much experience with C# language to solve it... :D Someone told me that for solve this problem I've to create a static method in FORM2 and then I can call it from FORM2 but I try but it there is an error... the code is... public class FORM2 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; private System.Windows.Forms.DataGrid dataPreResult; public FORM2() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.dataPreResult = new System.Windows.Forms.DataGrid(); ((System.ComponentModel.ISupportInitialize)(this.dataPreResult)).BeginInit(); this.SuspendLayout(); // // dataPreResult // this.dataPreResult.AllowSorting = false; this.dataPreResult.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.dataPreResult.DataMember = ""; this.dataPreResult.Dock = System.Windows.Forms.DockStyle.Fill; this.dataPreResult.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.dataPreResult.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataPreResult.Location = new System.Drawing.Point(0, 0); this.dataPreResult.Name = "dataPreResult"; this.dataPreResult.ReadOnly = true; this.dataPreResult.RowHeaderWidth = 10; this.dataPreResult.Size = new System.Drawing.Size(232, 613); this.dataPreResult.TabIndex = 0; // // FORM2 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(232, 613); this.Controls.Add(this.dataPreResult); this.Location = new System.Drawing.Point(600, 10); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "FORM2";

            A 1 Reply Last reply
            0
            • G GianlucaSeno

              Thanks for your answer...for the first question I try and it works great! :D And with the same method...I can close FORM2 from FORM1? For the second question, that's very important for me...could you give me an example how to solve this problem?I dont have so much experience with C# language to solve it... :D Someone told me that for solve this problem I've to create a static method in FORM2 and then I can call it from FORM2 but I try but it there is an error... the code is... public class FORM2 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; private System.Windows.Forms.DataGrid dataPreResult; public FORM2() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.dataPreResult = new System.Windows.Forms.DataGrid(); ((System.ComponentModel.ISupportInitialize)(this.dataPreResult)).BeginInit(); this.SuspendLayout(); // // dataPreResult // this.dataPreResult.AllowSorting = false; this.dataPreResult.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.dataPreResult.DataMember = ""; this.dataPreResult.Dock = System.Windows.Forms.DockStyle.Fill; this.dataPreResult.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.dataPreResult.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataPreResult.Location = new System.Drawing.Point(0, 0); this.dataPreResult.Name = "dataPreResult"; this.dataPreResult.ReadOnly = true; this.dataPreResult.RowHeaderWidth = 10; this.dataPreResult.Size = new System.Drawing.Size(232, 613); this.dataPreResult.TabIndex = 0; // // FORM2 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(232, 613); this.Controls.Add(this.dataPreResult); this.Location = new System.Drawing.Point(600, 10); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "FORM2";

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              Nobody else can help me? I dont know how to solve my problem...

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups