Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to build a very simple custom ComboBox with a method accessibility(bool), which, with a true argument shows a dropdown list, and when false shows no dropdown, but displays a tooltip.
using System.Windows.Forms;

namespace Test
{
    public partial class ZComboBox : ComboBox
    {
        private object[] itemList;
        private ToolTip toolTip = new ToolTip();
        private string msg = "Message";
        public ZComboBox()
        {
            InitializeComponent();
        }
        public ZComboBox(object[] itemList)
        {
            InitializeComponent();
            this.itemList = itemList;
            Items.AddRange(itemList);
        }

        private AutoScaleMode _autoscaleMode = AutoScaleMode.Dpi;
        public AutoScaleMode AutoScaleMode
        {
            get { return _autoscaleMode; }
            set { _autoscaleMode = value; }
        }
        public void accessibility(bool accessible)
        {
            if (accessible)
            {
                Items.Clear();
                Items.AddRange(itemList);
                toolTip.SetToolTip(this, "");
            }
            else
            {
                Items.Clear();
                toolTip.SetToolTip(this, msg);
            }
        }
    }
}


In my form, I visually dropped in a couple of ZComboBox objects, zComboBox1 and zComboBox2. The form code is
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            zComboBox1 = new ZComboBox(new object[] { "1", "2" });
            zComboBox1.accessibility(false);
            zComboBox2 = new ZComboBox(new object[] { "a", "b" });
            zComboBox2.accessibility(true); 
        }
    }
}

For a reason I cannot fathom, the controls are not showing any dropdown on accessibility(true), and don't have a tooltip on accessibility(false). Any help would be greatly appreciated.

What I have tried:

I have tried everything, but this eludes me.
It seems that the ZComboBox object I am modifying in the accessibility method is not the same one as is displayed on the form (the object with the same name).
Posted
Updated 6-Jan-21 6:15am
v7

Quote:
In my form I drop a couple of controls
isn't true.

What
zComboBox1 = new ZComboBox(new object[] { "1", "2" });

does is CREATE a ComboBox. It does not make it do anything, it does not make it part of your form; it will not show as you didn't add it to a collection of visible controls, e.g. that of your form. Try adding the line
this.Controls.Add(zComboBox1);


If WinForms is that new to you, I'd suggest you look inside the InitializeComponent method's code; that will show you how C# treats the controls you add with Visual Designer. When adding through your own code you basically need to do similar things.

BTW: your zComboBox1 variable isn't typed, does your code even compile?

BTW2: you also need to set a Location and maybe other properties (e.g. Font).

:)
 
Share this answer
 
v4
Comments
Maciej Los 6-Jan-21 6:01am    
Another 5!
hain 6-Jan-21 11:07am    
Thanks, Luc Pattyn, but I created the form visually, dragging on a couple of ZComboBox objects, named zComboBox1 and zComboBox2. Yes, the code compiles and runs.
Luc Pattyn 6-Jan-21 12:06pm    
If you created, positioned, and supplied properties to the form and the ZComboBoxes by the Visual Designer, then it is a mistake to immediately replace those ZComboBoxes with new ones, which will have default properties (such as Location zero,zero) independent of what you did while using Visual Designer.

:)
hain 7-Jan-21 9:13am    
That makes absolute sense! Thanks, Luc, for your help!
This is not an answer, but a tip...

I would:
1. add private variable to return Accessibility
C#
private bool bAcc = false;

2. add Accessibility property
C#
public bool Accessibility => bAcc;

3. change accessibility method to SetAccessibility
C#
public void SetAccessibility(bool accessible)
{
    bAcc = accessible;
    if (bAcc)
    {
        Items.Clear();
        Items.AddRange(itemList);
        toolTip.SetToolTip(this, "");
    }
    else
    {
        Items.Clear();
        toolTip.SetToolTip(this, msg);
    }
}

4. and also add in constructor
C#
public ZComboBox(bool accessible)
{
    InitializeComponent();
    bAcc = accessible;
}

which will help you to create custom combobox with predefined Accessible property in code.

[EDIT]
Another tip:
1. Change custom combobox properties (if you want)
2. Build project
3. Go to form design view,
4. Open Toolbox and drag & drop 2 zCombBox'es on that form,
5. Remove these lines from constructor:
C#
zComboBox1 = new ZComboBox(new object[] { "1", "2" });
zComboBox2 = new ZComboBox(new object[] { "a", "b" });

5. Use SetAccessibiity method (in form constructor) to change Accessibility property
C#
zComboBox1.SetAccessibility(false);
zComboBox2.SetAccessibility(true); 

7. Run project (F5).

That's all!
 
Share this answer
 
v2
Comments
hain 6-Jan-21 11:10am    
Thanks, Maciej Los. These are good tips, but I'm still stymied.
Maciej Los 6-Jan-21 11:56am    
Another tip:
1. Change custom combobox properties (if you want)
2. Build project
3. Go to form design view,
4. Open Toolbox and drop 2 zCombBox'es on that form,
5. Remove these lines from constructor:
zComboBox1 = new ZComboBox(new object[] { "1", "2" });
zComboBox2 = new ZComboBox(new object[] { "a", "b" });

5. Use SetAccessibiity method (in form constructor) to change Accessibility property
zComboBox1.SetAccessibility(false);
zComboBox2.SetAccessibility(true); 

7. Run project (F5).

That's all!
hain 7-Jan-21 17:04pm    
Perfect! Thanks, Maciej Los.
Maciej Los 8-Jan-21 0:14am    
You're very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900