This is a migrated thread and some comments may be shown as answers.

Close items directly from a MdiList?

2 Answers 67 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Eric Moreau
Top achievements
Rank 2
Iron
Iron
Veteran
Eric Moreau asked on 13 Aug 2020, 01:56 PM

Hi

I have one of my RadMenuItem having the MdiList set to true and it is working fine, no issues to report here.

Would it be possible to have a way to close one of the child form directly from that menu? Maybe a little X shown on the right like we see elsewhere?

2 Answers, 1 is accepted

Sort by
0
Accepted
Nadya | Tech Support Engineer
Telerik team
answered on 17 Aug 2020, 05:22 AM

Hello, Eric,

By default, RadMenuItems are not supposed to have a close button. However, it is possible to achieve your requirement. You can add a button to the Children collection of the RadMenuItem and get the form associated with this menu. I prepared a sample example. Feel free to extend or modify it as per your needs.

 public RadForm1()
 {
     InitializeComponent();
     this.IsMdiContainer = true;
     this.radMenuItem1.MdiList = true;
     Form child1 = new Form();
     child1.Text = "Standard Windows Form1";
     child1.MdiParent = this;
     child1.Show();

     Form child2 = new Form();
     child2.Text = "Standard Windows Form2";
     child2.MdiParent = this;
     child2.Show();

 }

 protected override void OnShown(EventArgs e)
 {
     base.OnShown(e);

     foreach (RadMenuItem item in this.radMenuItem1.Items)
     {
         Form f = typeof(RadMenuItem).GetField("mdiChildFormToActivate", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(item) as Form;
         if (f != null)
         {
             RadButtonElement button = new RadButtonElement();
             button.Text = "X";
             button.StretchHorizontally = false;
             button.Alignment = ContentAlignment.MiddleRight;
             button.Tag = f;
             button.Click += this.Button_Click;
             item.Children.Add(button);
         }
     }
 }

 private void Button_Click(object sender, EventArgs e)
 {
     RadButtonElement btn = sender as RadButtonElement;
     if (btn.Tag!=null)
     {
         ((Form)btn.Tag).Close();
     }
 }

I hope this helps. If you have other questions please let me know.

Regards,
Nadya
Progress Telerik

0
Eric Moreau
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 17 Aug 2020, 04:44 PM
Thanks Nadya. Instead of the OnShown event handler (which was not working for me as children forms are opened by the user at runtime), I have the DropDownOpening event of my Window menu.
Tags
Menu
Asked by
Eric Moreau
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Nadya | Tech Support Engineer
Telerik team
Eric Moreau
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or