CommandBar designer code automatically changing

1 Answer 133 Views
CommandBar Form
John
Top achievements
Rank 1
John asked on 19 Jul 2021, 04:15 PM

Hi there,

Using control version 2021.2.615

VS2019 16.10.3

 

I have some code using a CommandBar and CommandBarStrips as below:

            // 
            // CommandBar
            // 
            this.CommandBar.Dock = System.Windows.Forms.DockStyle.Top;
            this.CommandBar.Location = new System.Drawing.Point(0, 25);
            this.CommandBar.Name = "CommandBar";
            this.CommandBar.Rows.AddRange(new Telerik.WinControls.UI.CommandBarRowElement[] {
            this.CommandBarRow});
            this.CommandBar.Size = new System.Drawing.Size(1241, 31);
            this.CommandBar.TabIndex = 4;
            this.CommandBar.ThemeName = "FluentDark";
            // 
            // CommandBarRow
            // 
            this.CommandBarRow.MinSize = new System.Drawing.Size(25, 25);
            this.CommandBarRow.Name = "CommandBarRow";
            this.CommandBarRow.Strips.AddRange(new Telerik.WinControls.UI.CommandBarStripElement[] {
            this.CommandBarToolStrip,
            this.CommandBarModelStrip});
            // 
            // CommandBarToolStrip
            // 
            this.CommandBarToolStrip.Alignment = System.Drawing.ContentAlignment.TopCenter;
            this.CommandBarToolStrip.AutoSize = true;
            this.CommandBarToolStrip.DisplayName = "commandBarStripElement1";
            this.CommandBarToolStrip.EnableDragging = false;
            this.CommandBarToolStrip.Name = "CommandBarToolStrip";
            // 
            // 
            // 
            this.CommandBarToolStrip.OverflowButton.Enabled = false;
            this.CommandBarToolStrip.OverflowButton.Visibility = Telerik.WinControls.ElementVisibility.Hidden;
            ((Telerik.WinControls.UI.RadCommandBarOverflowButton)(CommandBarToolStrip.GetChildAt(2))).Enabled = false;
            ((Telerik.WinControls.UI.RadCommandBarOverflowButton)(CommandBarToolStrip.GetChildAt(2))).Visibility = Telerik.WinControls.ElementVisibility.Hidden;
            // 
            // CommandBarModelStrip
            // 
            this.CommandBarModelStrip.Alignment = System.Drawing.ContentAlignment.TopRight;
            this.CommandBarModelStrip.AutoSize = true;
            this.CommandBarModelStrip.DisplayName = "commandBarStripElement1";
            this.CommandBarModelStrip.Items.AddRange(new Telerik.WinControls.UI.RadCommandBarBaseItem[] {
            this.lblCurrModel,
            this.CurrentModelList});
            this.CommandBarModelStrip.Name = "CommandBarModelStrip";
            // 
            // 
            // 
            this.CommandBarModelStrip.OverflowButton.Enabled = false;
            this.CommandBarModelStrip.OverflowButton.Visibility = Telerik.WinControls.ElementVisibility.Hidden;
            this.CommandBarModelStrip.Text = "";
            ((Telerik.WinControls.UI.RadCommandBarOverflowButton)(CommandBarModelStrip.GetChildAt(2))).Enabled = false;
            ((Telerik.WinControls.UI.RadCommandBarOverflowButton)(CommandBarModelStrip.GetChildAt(2))).Visibility = Telerik.WinControls.ElementVisibility.Hidden;
            // 
            // lblCurrModel
            // 
            this.lblCurrModel.Alignment = System.Drawing.ContentAlignment.MiddleLeft;
            this.lblCurrModel.DisplayName = "commandBarLabel1";
            this.lblCurrModel.Name = "lblCurrModel";
            this.lblCurrModel.Text = "Current Model : ";
            // 
            // CurrentModelList
            // 
            this.CurrentModelList.AutoEllipsis = true;
            this.CurrentModelList.AutoSize = false;
            this.CurrentModelList.Bounds = new System.Drawing.Rectangle(0, 0, 300, 24);
            this.CurrentModelList.DisplayName = "commandBarDropDownList1";
            this.CurrentModelList.DropDownAnimationEnabled = true;
            this.CurrentModelList.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
            this.CurrentModelList.MaxDropDownItems = 0;
            this.CurrentModelList.Name = "CurrentModelList";
            this.CurrentModelList.Text = "";
            this.CurrentModelList.SelectedIndexChanged += new Telerik.WinControls.UI.Data.PositionChangedEventHandler(this.CurrentModelList_SelectedIndexChanged);

The above code compiles and runs with no issue.

 

If I open the designer for this form, then the above code is automatically replaced by the code below:

            // 
            // CommandBar
            // 
            this.CommandBar.Dock = System.Windows.Forms.DockStyle.Top;
            this.CommandBar.Location = new System.Drawing.Point(0, 25);
            this.CommandBar.Name = "CommandBar";
            this.CommandBar.Size = new System.Drawing.Size(1241, 31);
            this.CommandBar.TabIndex = 4;
            this.CommandBar.ThemeName = "FluentDark";
            ((Telerik.WinControls.UI.CommandBarRowElement)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0))).Name = "CommandBarRow";
            ((Telerik.WinControls.UI.CommandBarRowElement)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0))).MinSize = new System.Drawing.Size(25, 25);
            ((Telerik.WinControls.UI.CommandBarStripElement)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(0))).Alignment = System.Drawing.ContentAlignment.TopCenter;
            ((Telerik.WinControls.UI.CommandBarStripElement)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(0))).Name = "CommandBarToolStrip";
            ((Telerik.WinControls.UI.RadCommandBarOverflowButton)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(2))).Enabled = false;
            ((Telerik.WinControls.UI.RadCommandBarOverflowButton)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(2))).Visibility = Telerik.WinControls.ElementVisibility.Hidden;
            ((Telerik.WinControls.UI.CommandBarStripElement)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(1))).Alignment = System.Drawing.ContentAlignment.TopRight;
            ((Telerik.WinControls.UI.CommandBarStripElement)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(1))).Name = "CommandBarModelStrip";
            ((Telerik.WinControls.UI.CommandBarLabel)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(0))).Text = "Current Model : ";
            ((Telerik.WinControls.UI.CommandBarLabel)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(0))).Alignment = System.Drawing.ContentAlignment.MiddleLeft;
            ((Telerik.WinControls.UI.CommandBarLabel)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(0))).Name = "lblCurrModel";
            ((Telerik.WinControls.UI.CommandBarDropDownList)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(1))).AutoSize = false;
            ((Telerik.WinControls.UI.CommandBarDropDownList)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(1))).Name = "CurrentModelList";
            ((Telerik.WinControls.UI.RadCommandBarOverflowButton)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(1).GetChildAt(2))).Enabled = false;
            ((Telerik.WinControls.UI.RadCommandBarOverflowButton)(CommandBar.GetChildAt(0).GetChildAt(0).GetChildAt(0).GetChildAt(1).GetChildAt(2))).Visibility = Telerik.WinControls.ElementVisibility.Hidden;
            // 


This code compiles but on running the first line containing all the .GetChildAt calls throws IndexOutOfRange.

Sometimes it deletes the code completely, leaving just this:

            // 
            // CommandBar
            // 
            this.CommandBar.Dock = System.Windows.Forms.DockStyle.Top;
            this.CommandBar.Location = new System.Drawing.Point(0, 25);
            this.CommandBar.Name = "CommandBar";
            this.CommandBar.Size = new System.Drawing.Size(1241, 30);
            this.CommandBar.TabIndex = 4;
            this.CommandBar.ThemeName = "FluentDark";

Any idea what's happening here?

 

Many thanks,

 

John.

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 20 Jul 2021, 10:30 AM

Hello, John,

Indeed, the described behavior sounds weird. I was not able to observe this behavior on my end. Are you editing the code in InitializeComponent() programmatically? 

Before proceeding further I would like to note something. Usually, InitializeComponent() method in Visual Studio is a method that is automatically created and managed by the Windows Forms designer and it defines everything you see on the form. Everything done on the form in VS using designers generates code. Every single control added and property set at design time will generate code and that code goes into InitializeComponent() method. Any other code that you have added programmatically should be after the InitializeComponent() in the form file - RadForm.cs, not in the Designer file. It is not to recommended to modify the Designer.cs file manually. More information you can find here: https://stackoverflow.com/questions/33348930/editing-the-initializecomponent-method-c-sharp 

Note, if you had any code in the designer file there is no guarantee that it will be saved when performing further customizations to your form. I would recommend you to use this code after InitializeComponent() in your form or in the form's Load event. Thus, you will ensure that the code wouldn't disappear. The Designer.cs file should contain just the auto-generated code in order to ensure that it wouldn't get lost after reopening the form.

I hope this information is useful. Please let me know if you need further assistance.

Regards,
Nadya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

John
Top achievements
Rank 1
commented on 21 Jul 2021, 08:13 AM

Morning Nadya and thanks for the reply.

All of the code I included above has been generated automatically by adding/modifying controls in the VS designer, to my knowledge none of it has been added or edited manually.

Yesterday I briefly tried removing the offending controls and re-adding them but I ran into errors doing that. I'll give it another go today and let you know the outcome.

Many thanks,

John.
Nadya | Tech Support Engineer
Telerik team
commented on 21 Jul 2021, 08:38 AM

Hello, John,

Thank you for clarifying that changes are made only by the VS designer.

Take as much time as needed to isolate the problem. It would be greatly appreciated if you can isolate the problematic case in a runnable project so that you can provide it and I can test it locally. Thus, it would be possible to investigate the case precisely and assist you further. Feel free to submit a support ticket from your Telerik account where you can upload files up to 20MB.

I am looking forward to your reply.

John
Top achievements
Rank 1
commented on 22 Jul 2021, 05:17 PM

Hi Nadya,

I've submitted a ticket as suggested. https://www.telerik.com/account/support-tickets/view-ticket/1528908

Many thanks,

John.
Nadya | Tech Support Engineer
Telerik team
commented on 23 Jul 2021, 12:05 PM

Hi, John,

Thank you for submitting a ticket with project attached. Please see our answer there for more information.

In case you have any other questions do not hesitate to ask.

Tags
CommandBar Form
Asked by
John
Top achievements
Rank 1
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or