locked
Trying to change the size of the "listbox" and "propertyBrowser" controls on a Custom CollectionEditor RRS feed

  • Question

  • Hi,

    I have a Custom Collection Editor and I would like to change the sizes of the "listbox" and "propertyBrowser" controls. Both have Size() properties, but they don't change when you set them to something else. Any ideas how to do this?


    Tim

    Friday, October 9, 2020 4:19 PM

All replies

  • Would be prudent to show existing code attempts.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, October 9, 2020 6:36 PM
  • Karen,

    Sorry about that. Here's the code for my CustomCollectionEditor:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Drawing;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Security;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.VisualBasic;
    using System.ComponentModel.Design;
    using System.Windows.Forms;
    
    namespace FirstPassYield
    {
        public class MyCollectionEditor : CollectionEditor
        {
            public static bool bEditingCollection = false;
    
            public delegate void MyFormClosedEventHandler(object sender, FormClosedEventArgs e);
    
            public event MyFormClosedEventHandler MyFormClosed;
    
            public delegate void MyFormLoadEventHandler(object sender, System.EventArgs e);
    
            public event MyFormLoadEventHandler MyFormLoad;
    
    
            public delegate void MyFormControlAddedEventHandler(object sender, System.EventArgs e);
    
            public event MyFormControlAddedEventHandler MyFormControlAdded;
    
            public MyCollectionEditor(Type type)
                : base(type)
            {
            }
    
            protected override string GetDisplayText(object value)
            {
                Defect item = new Defect();
                item = (FirstPassYield.Defect)value;
    
                if (String.IsNullOrEmpty(item.DefectCode))
                    return base.GetDisplayText(" ");
                else
                {
                    String sDisplay = item.DefectCode.ToString().Substring(0, item.DefectCode.ToString().IndexOf(":"));
                    return base.GetDisplayText(string.Format("{0}", sDisplay));
                }
            }
    
            protected override CollectionForm CreateCollectionForm()
            {
                CollectionForm collectionForm = base.CreateCollectionForm();
                Font myDialogFont = new Font("Microsoft San Seraf", 9.0f, FontStyle.Bold); // Form's Font
                Font myFont = new Font("Microsoft San Seraf", 9.0f, FontStyle.Bold);       // Individual Item's Font
    
                // Set the Dialog Font
                collectionForm.Font = myDialogFont;
    
                // Turn OFF Help Button
                collectionForm.HelpButton = false;
    
                // Set the BackColor, StartPosition and Size of the form
                collectionForm.BackColor = Color.FromName("SteelBlue");
                collectionForm.StartPosition = FormStartPosition.CenterParent;
                collectionForm.Size = new Size(660, 500);
    
                // Add Form Handlers for trapping Load and Close
                collectionForm.FormClosed += this.collection_FormClosed;
                collectionForm.Load += this.collection_FormLoad;
    
                var overArchingTableLayoutPanel = collectionForm.Controls["overArchingTableLayoutPanel"];
    
                overArchingTableLayoutPanel.Controls["listbox"].Size = new Size(overArchingTableLayoutPanel.Controls["listbox"].Width / 4, overArchingTableLayoutPanel.Controls["listbox"].Height);
                overArchingTableLayoutPanel.Controls["propertyBrowser"].Size = new Size(overArchingTableLayoutPanel.Controls["propertyBrowser"].Width / 4, overArchingTableLayoutPanel.Controls["propertyBrowser"].Height);
    
                var addRemoveTableLayoutPanel = overArchingTableLayoutPanel.Controls["addRemoveTableLayoutPanel"];
                // Set the BackColor of the Buttons - It appears that they get set by default to the form's BackColor
                addRemoveTableLayoutPanel.Controls["addButton"].BackColor = Color.FromName("Control");
                addRemoveTableLayoutPanel.Controls["removeButton"].BackColor = Color.FromName("Control");
                // Set the Font for the Add, Remove buttons
                addRemoveTableLayoutPanel.Controls["addButton"].Font = myFont;
                addRemoveTableLayoutPanel.Controls["removeButton"].Font = myFont;
                // Add Handlers for trapping Add and Remove Buttons
                addRemoveTableLayoutPanel.Controls["addButton"].Click += AddButton_Click;
                addRemoveTableLayoutPanel.Controls["removeButton"].Click += RemoveButton_Click;
    
                var okCancelTableLayoutPanel = overArchingTableLayoutPanel.Controls["okCancelTableLayoutPanel"];
                // Set the BackColor of the Buttons - It appears that they get set by default to the form's BackColor
                okCancelTableLayoutPanel.Controls["okButton"].BackColor = Color.FromName("Control");
                okCancelTableLayoutPanel.Controls["cancelButton"].BackColor = Color.FromName("Control");
                // Set the Font for the OK, Cancel buttons
                okCancelTableLayoutPanel.Controls["okButton"].Font = myFont;
                okCancelTableLayoutPanel.Controls["cancelButton"].Font = myFont;
                // Add Handlers for trapping OK and Cancel Buttons
                okCancelTableLayoutPanel.Controls["okButton"].Click += OKButton_Click;
                okCancelTableLayoutPanel.Controls["cancelButton"].Click += CancelButton_Click;
    
                // Change the Member Labels' Text, Color and Font
                overArchingTableLayoutPanel.Controls["membersLabel"].Text = "&Defect List:";
                overArchingTableLayoutPanel.Controls["membersLabel"].ForeColor = Color.FromName("White");
                overArchingTableLayoutPanel.Controls["membersLabel"].Font = myFont;
                // Change the Properties Labels' Text, Color and Font
                overArchingTableLayoutPanel.Controls["propertiesLabel"].Text = "&Properties:";
                overArchingTableLayoutPanel.Controls["propertiesLabel"].ForeColor = Color.FromName("White");
                overArchingTableLayoutPanel.Controls["propertiesLabel"].Font = myFont;
                // Set the BackColor of the Buttons - It appears that they get set by default to the form's BackColor
                overArchingTableLayoutPanel.Controls["upButton"].BackColor = Color.FromName("Control");
                overArchingTableLayoutPanel.Controls["downButton"].BackColor = Color.FromName("Control");
                // Add Handlers for trapping Up and Down Buttons
                overArchingTableLayoutPanel.Controls["upButton"].Click += UpButton_Click;
                overArchingTableLayoutPanel.Controls["downButton"].Click += DownButton_Click;
    
                var propertyBrowser = overArchingTableLayoutPanel.Controls["propertyBrowser"] as PropertyGrid;
                if (propertyBrowser.Controls.Count != 0)
                {
                    // Set the PropertySorting and ToolbarVisibilty
                    propertyBrowser.PropertySort = PropertySort.NoSort;
                    propertyBrowser.ToolbarVisible = false;
                }
    
                // Do something based on which Collection Editor was called
                switch (collectionForm.Text)
                {
                    case "Defect Collection Editor":
                        {
                            break;
                        }
                }
    
                // Hide Add, Remove, Up and Down Buttons
                //collectionForm.Controls["overArchingTableLayoutPanel"].Controls["addRemoveTableLayoutPanel"].Controls["addButton"].Visible = false;
                //collectionForm.Controls["overArchingTableLayoutPanel"].Controls["addRemoveTableLayoutPanel"].Controls["removeButton"].Visible = false;
                //collectionForm.Controls["overArchingTableLayoutPanel"].Controls["upButton"].Visible = false;
                //collectionForm.Controls["overArchingTableLayoutPanel"].Controls["downButton"].Visible = false;
    
                return collectionForm;
            }
    
            private void collection_FormLoad(object sender, System.EventArgs e)
            {
                bEditingCollection = true;
            }
    
            private void AddButton_Click(object sender, EventArgs e)
            {
                //MessageBox.Show("Add Button Clicked");
            }
    
            private void RemoveButton_Click(object sender, EventArgs e)
            {
                //MessageBox.Show("Remove Button Clicked");
            }
    
            private void OKButton_Click(object sender, EventArgs e)
            {
                //MessageBox.Show("OK Button Clicked");
            }
    
            private void CancelButton_Click(object sender, EventArgs e)
            {
                //MessageBox.Show("Cancel Button Clicked");
            }
    
            private void UpButton_Click(object sender, EventArgs e)
            {
                //MessageBox.Show("Up Button Clicked");
            }
    
            private void DownButton_Click(object sender, EventArgs e)
            {
                //MessageBox.Show("Down Button Clicked");
            }
    
            private void collection_FormClosed(object sender, FormClosedEventArgs e)
            {
                bEditingCollection = false;
            }
        }
    }

    The two lines marked in bold are where I changed the size, but it gets ignored.


    Tim


    • Edited by Tim8w Monday, October 12, 2020 2:36 PM
    Monday, October 12, 2020 2:34 PM
  • Hi Tim8w,
    when you use " var overArchingTableLayoutPanel = collectionForm.Controls["overArchingTableLayoutPanel"]" to get "overArchingTableLayoutPanel" control, this control doesn't have AutoSize property.
    And you can't change the size of the "listbox" and "propertyBrowser" controls in "overArchingTableLayoutPanel" .
    I made a simple test without a Custom CollectionEditor and it occured the same situation.
    So when I use panel control directly instead of "var t = f1.Controls["panel1"]", I can change the size of listBox.

    Form1 f1 = new Form1();
    var t = f1.Controls["panel1"];     
    t.Controls["listBox1"].Size = new Size(panel1.Width/4,panel1.Height/2);
     // panel1.Controls["listBox1"].Size = new Size(panel1.Width / 4, panel1.Height/2);  it worked fine. 

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, October 13, 2020 8:12 AM
  • Daniel,

    I don't understand what you are suggesting. There is no "Form1" data type. Is this some custom data type you created?


    Tim

    Tuesday, October 13, 2020 2:08 PM
  • This forum is for C#-specific questions only. Please post questions related to Winforms in the Microsoft Q&A forums

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, October 13, 2020 4:58 PM
  • Hi Tim8w,
    In my code, "Form1"refers to the Form class which represents a window or dialog box that makes up an application's user interface.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 14, 2020 6:40 AM