Redraw Toolstrip based on selections - c#

I have been asked to write c# winforms app that will give users the ability to select options from a checkbox list and have it automatically redraw/repaint a toolstrip with the selected items.
I am new to winforms so I am not sure how to approach it. Should I be using the BackgroundWorker Process? Invalidate()?
Just alittle confused.
Any assistence of pointing in the right direction would be appreciated.

You probably don't want a BackgroundWorker as that's run on a non-UI thread and would cause problems when you try to modify the toolstrip (you can only work with the UI on the thread the UI was created on). Handle the CheckedChanged events on the checkboxes and then add or remove items from the toolstrip. The repainting should be automatic.

You need to keep tooltips for all options some where (if Tag property of checkboxes is free the put it there). Then when an option is selected or deselected, you need to update tooltips.
Let's suppose you are adding all the checkboxes in a IList. then things will work as follows:
private IList<CheckBox> options= new List<CheckBox>();
private void UpdateTTip()
{
toolTip1.RemoveAll();
foreach (CheckBox c in options)
{
if (c.Checked)
toolTip1.SetToolTip(c, c.Tag.ToString());
}
}
Now you need to call this on checkedchanged event of options check boxes:
private void chk_CheckedChanged(object sender, EventArgs e)
{
UpdateTTip();
}

A toolstrip contains controls by itself - it does not just "paint" buttons you can press. In order to have the toolstrip display different buttons depending on different conditions, you can:
Clear the toolstrip items and re-create the ones that are needed in the current context in code when items are checked in the list you mentioned
Add all the items and design time (with property Visible = false) and only set the necessary ones to Visible = true upon selection in your check listbox
No need to do any painting :-)

Related

Issue while iterating through User Controls in a Windows Form

I am facing an issue while running through all the User Controls in my Windows form.
I am creating a Windows Form that has the following features:
The Main form has 3 User Controls embedded in it
The Main form also has a combo box. Selecting a particular value in the Combo box will bring the corresponding User Control to the front.
Each User Control has two Check boxes as well as two Combo boxes.
The User can summon each User Control through the Main Form's combo box and check the check boxes and/or modify the combo boxes inside each User Control
Once this is done, there is a button, which on being pressed, executes the following code. This code is supposed to check which check boxes have been checked from every User Control, and execute some functionality :
private void button1_Click(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
{
if (c is UserControl)
{
foreach (Control ctl in c.Controls)
{
if (ctl is CheckBox && (ctl as CheckBox).Checked)
{
Indicator.Text = "It's in";
}
}
}
}
//Some other code after this
}
Here, I have included a Text Box called "Indicator" that shows whether the compiler has entered a particular "for" loop or "if" block. And I'm observing that the innermost "if" alone is not getting executed.
Could someone point out why exactly this is happening?
You need a recursive algorithm,
void ProcessControls(Control ctrlContainer)
{
foreach (Control ctrl in ctrlContainer.Controls)
{
if (ctrl is CheckBox && (ctrl as CheckBox).Checked)
{
Indicator.Text = "It's in";
}
if (ctrl.HasChildren)
ProcessControls(ctrl);
}
}
I do think you might be better off adding some functionality to your user control so it can describe the state of its own checkboxes rather than going digging inside it to find it and do logic. Generally in OO programming, when we encapsulate things within a class, we also provide general purpose accessors "visible to the outside" to describe the internal state of affairs, rather than letting external code interests go poking around inside class to find out what they want
At some point in time you've added these usercontrols to the form either directly in the designer, or programmatically. In the first case they will have their own name:
var u1 = usercontrol1.GetCheckboxStateArray();
var u2 = usercontrol2.GetCheckboxStateArray();
Etc
Or maybe you added them programmatically, in which case it would make sense to keep track of them in a list as you're adding them:
protected List<UserControl> _ucList = new List<UserControl>();
...
foreach(var result in somedatabasequery){
var uc = new UserControl(result.Whatever);
this.Controls.Add(uc);
_ucList.Add(uc);
}
Then this list can be iterated. Sure you could argue that "well .Controls is a collection too, so why add them to another list when they're already in an accessible collection" - for the reasons you're here; .Controls is a general purpose description of the hierarchy of all controls on a form, it contains stuff we don't want and is hard to iterate. This List is purely and simply all and only the stuff we're interested in
As an aside, the UI you have described is atypical. The more usual way of hiding and showing controls under the selection of something that holds a bit of text would be a TabControl. It might be easier to loop through too, if you will persist with this "search for UserControls in a collection of controls" method - tabcontrols have tabpages, tabpages would probably have a .Controls that just contains your UserControl. The tabpage intrinsically takes care of showing and hiding controls as pages are clicked on which could simplify your code
Thanks to everyone for the answers. As it happens, the issue was hiding in plain sight, right under my nose. In each of the User Controls, I had placed the Checkboxes and Combo Boxes inside a Group Box. It completely slipped my mind, so much so that I didn't even mention them in my question.
Thus, as #Caius had suggested in the comments, the code wasn't functioning because I had not addressed the Group Box Container holding these Controls. Once I removed the Group Boxes (used only for aesthetic purpose), the code started functioning properly.

How to group Groups of radio buttons in Windows forms

I have a form with a number of radiobuttons where only one should be selected at a time.
Some of the radiobuttons are connected and need to bee explained by a header. For this i put them in a Groupbox. But then is the radiobuttons inside the groupbox no longer connected to the ones outside and its possible to select two off them.
Is there a way to connect the radiobuttons so they all react on eachother? Or is there a better way to group and explain the radiobuttons then using a groupbox?
It is the designed behavior of a RadioButton to be grouped by its container according to MSDN:
When the user selects one option button (also known as a radio button)
within a group, the others clear automatically. All RadioButton
controls in a given container, such as a Form, constitute a group. To
create multiple groups on one form, place each group in its own
container, such as a GroupBox or Panel control
You could try using a common eventhandler for the RadioButtons that you want linked and handle the Checking/UnChecking yourself, Or you can place your RadioButtons on top of your GroupBox, not adding them to the GroupBox then BringToFront.
This is possible, but it comes at a high price. You'll have to set their AutoCheck property to false and take care of unchecking other buttons yourself. The most awkward thing that doesn't work right anymore is tab stops, a grouped set of buttons has only one tabstop but if you set AutoCheck = false then every button can be tabbed to.
The biggest problem no doubt it is the considerable confusion you'll impart on the user. So much so that you probably ought to consider checkboxes instead.
In windows forms I don't think there is an easy way (as in setting a property such as GroupName) to group radiobuttons. The quickest way would be to simply group the radiobuttons in a collection and listen to the checkedchanged event. For example:
var rbuttons = new List<RadioButton>{ radioButton1, radioButton2, radioButton3, radioButton4 }; //or get them dynamically..
rbuttons.ForEach(r => r.CheckedChanged += (o, e) =>
{
if (r.Checked) rbuttons.ForEach(rb => rb.Checked = rb == r);
});
If any of the radiobuttons in the list is checked, the others are automatically unchecked
I solved this by handling the CheckChanged event.
private void radio1_CheckedChanged(object sender, EventArgs e)
{
if (radio1.Checked)
{
// do stuff
radio2.Checked = false;
}
}
private void radio2_CheckedChanged(object sender, EventArgs e)
{
if (radio2.Checked)
{
// do stuff
radio1.Checked = false;
}
}

How to disable CheckedListBox but enable scrolling winforms

I know that the question might seems to easy, but I can't find solution. I have CheckdListBox in my form. I have list of checkboxes inside. If I do that:clbxMyControl.Enabled = false; then I can't scroll to see all items. How to allow scrolling on disabled CheckedListBox?
Thanks!
Instead of disabling the control you should change it's SelectionMode like this:
checkedListBox1.SelectionMode = SelectionMode.None;
The user won't be able to select an item but will be allowed to scroll
You can prevent the user from checking items with the ItemCheck event:
bool listEnabled = true;
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
if (!listEnabled) e.NewValue = e.CurrentValue;
}
But do make sure that it is obvious that checking no longer works:
private void setListEnabled(bool enabled) {
listEnabled = enabled;
if (listEnabled) checkedListBox1.BackColor = Color.FromKnownColor(KnownColor.Window);
else checkedListBox1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
The ListBox has only one handle in the Windows API which is set to enabled or disabled. Therefore there is no native way to enable the scrolling but the items.
You might be able to build your own control that has the requested behavior but you will have to paint it your self from scratch which might be a lot of work or you find a third party control that supports the behavior.
Or you can not use the enabled property but change the background/font color to make it look disabled and set:
checkedListBox1.SelectionMode = SelectionMode.None;
Another alternative might be the DataGridView. It is much more powerful, I'm not sure if it disables the scrollbar, too but if it does you are able to make the cells readonly and color them.
You can use this code to make CheckedListBox scrollable.
clbxMyControl.SelectionMode = SelectionMode.None;
Because Enable=false; make control unscrollable because this make the container of checkbox disabled.

Binding between controls in Windows Forms

Is there a way to make a control dependent on another control? I have a combo box and a button, and I need the button to be enabled if and only if there is an item selected in the combo box.
I know I can set the Enabled property of the button inside the SelectedIndexChanged callback, but then it will require some code, and besides there's an issue with what initial state the button would have. So I'm looking for something that wouldn't require manually handing events, is this possible?
Thanks!
No, there is no way in winforms to do this without code. What I usually do is to collect all such state-setting code into one specific method:
private void SetControlStates()
{
theButton.Enabled = theComboBox.SelectedIndex >= 0;
// code for other controls follow here
}
Then I trigger this method from all over the place, as soon as there is an interaction that may lead to the state changing (including the last thing I do when the form has finished loading; that takes care of initial state). If you want to avoid unnecessary assignments, just add code to check the value first:
private void SetControlStates()
{
bool buttonEnabled = theComboBox.SelectedIndex >= 0;
if (theButton.Enabled != buttonEnabled) theButton.Enabled = buttonEnabled;
// code for other controls follow here
}

Odd behavior when toggling CheckedListBox item's checked state via MouseClick when clicking on the same selection

The WinForms CheckedListBox control has 2 default behaviors when clicking with a mouse:
In order to check/uncheck an item you're required to click an item twice. The first click selects the item, and the second toggles the check state.
In addition, one subsequent click of the same item will toggle that item's checked state.
As a convenience feature I needed to allow users to toggle the selection in one click. I have achieved this, so now default behavior #1 above is achieved in one click. The problem is behavior #2 no longer works correctly when clicking the same (i.e., currently selected) item. It works fine when jumping between items, which is desired, but it requires up to 4 clicks on the same item.
My workaround for this is to call the toggling logic twice if the user selects the same item repeatedly. So on to my questions:
This works, but why? What's the real underlying issue?
Is there a better way to achieve this so I can get it working like default behavior #2 without calling the method twice and keeping track of my last selection?
Oddly enough debugging the code reveals that the checked state has changed but it doesn't appear on the UI side till it's called twice. I thought it might be threading related but it's not a re-entrant event being triggered that might need BeginInvoke usage.
Here's my code:
using System.Linq;
using System.Windows.Forms;
namespace ToggleCheckedListBoxSelection
{
public partial class Form1 : Form
{
// default value of -1 since first item index is always 0
private int lastIndex = -1;
public Form1()
{
InitializeComponent();
CheckedListBox clb = new CheckedListBox();
clb.Items.AddRange(Enumerable.Range(1, 10).Cast<object>().ToArray());
clb.MouseClick += clb_MouseClick;
this.Controls.Add(clb);
}
private void clb_MouseClick(object sender, MouseEventArgs e)
{
var clb = (CheckedListBox)sender;
Toggle(clb);
// call toggle method again if user is trying to toggle the same item they were last on
// this solves the issue where calling it once leaves it unchecked
// comment these 2 lines out to reproduce issue (use a single click, not a double click)
if (lastIndex == clb.SelectedIndex)
Toggle(clb);
lastIndex = clb.SelectedIndex;
}
private void Toggle(CheckedListBox clb)
{
clb.SetItemChecked(clb.SelectedIndex, !clb.GetItemChecked(clb.SelectedIndex));
}
}
}
To reproduce my problem comment out the lines mentioned in the code comments and follow these steps:
Click the item at index 2 - state changes to checked.
With the current item selected, click it again - state does not change. Expected: unchecked. Click it a few times and it finally switches.
Thanks for reading!
As a convenience feature I needed to allow users to toggle the selection in one click.
I'm not sure what's happening with the code, but setting CheckOnClick to true will do this:
CheckOnClick indicates whether the check box should be toggled whenever an item is selected. The default behavior is to change the selection on the first click, and then have the user click again to apply the check mark. In some instances, however, you might prefer have the item checked as soon as it is clicked.

Categories