checking the bool property of multiple objects simultaneously - c#

I have multiple check boxes on my GUI application that enables auto update for each object of the same type. So if the checkbox is checked, the isautoupdate property is set to true else set to false. I have a button that needs to enable/disable auto update on all checkboxes. How do I go about checking if isautoupdate property of all the objects is set to true or false.
my current implementation is using a foreach loop that iterates through each object and checks if the isautoupdate is set to true or false but I get a toggle effect where if some checkboxes are checked it will uncheck them and vise versa.
in .cs
foreach (MxL_GUI_ChannelSettingAndStatusItem item in theGUIManager.theDevice.channelCollection)
{
if (!item.IsAutoUpdated)
{
item.IsAutoUpdated = true;
}
else
{
item.IsAutoUpdated = false;
}
}

If you don't want your slave checkboxes to toggle then don't write code that toggles them. Instead, check the IsChecked property of the master checkbox and apply the value to all IsAutoUpdated properties of your items:
foreach (MxL_GUI_ChannelSettingAndStatusItem item in ...)
{
item.IsAutoUpdated = masterCheckbox.IsChecked.Value;
}

I'm not sure if I understand you requirement exactly. If you want to detect if all items are set to true or false then use:
var items = theGUIManager.theDevice.channelCollection;
// If you need to know if for all items IsAutoUpdated = true
bool allChecked = items.All(item => item.IsAutoUpdated);
// If you need to know if they're all false
bool noneChecked = !items.Any(item => item.IsAutoUpdated);
Then update your items, e.g:
foreach(var item in items) { item.IsAutoUpdated = !allChecked; }

Related

ToolStripMenuItem strange behavior on visible property [duplicate]

This question already has an answer here:
How to set a ToolStripMenuItem Visible in code?
(1 answer)
Closed 1 year ago.
I have encounter that issue a long time ago and again now. I cannot figure out what special thing had to be done to edit the ToolStripMenuItem control a Visible property while inside a form load.
in designer.cs
private System.Windows.Forms.ToolStripMenuItem mnuExport;
//
// mnuExport
//
this.mnuExport.Name = "mnuExport";
this.mnuExport.Size = new System.Drawing.Size(116, 22);
this.mnuExport.Text = "Export";
this.mnuExport.Visible = false;
this.mnuExport.Click += new System.EventHandler(this.mnuExport_Click);
in the form code
public partial class MainBuilder : Form
{
private void MainBuilder_Load(object sender, EventArgs e)
{
mnuExport.Visible = true;
}
}
In the code above export is a menu item of type ToolStripMenuItem which trough the property grid in the form design mode I have modified it's Visible property to false. So in the form load I want to switch to the visible state to true. I thought it be easy so I coded all the logic around and it failed. So I decided to remove all my code and simply hardcode the set to true and to my surprise this does not work.
When I put the breakpoint on the line in the form_load I clearly see it's false and if it let the line run the value is still false.
I recall seeing this issue in the past but cannot find anything about it. I also tried with 4-5 other menu item in that window and they all show the same behavior.
EDIT
just tried to put visible = true in the designer.cs instead and in the form_load still tells me the value is false. There is some major issues here.
As mentioned in the comments the property Visible getter does not reflect the actual inner property value until later in the lifecycle. It is still unavailable in the form_loaded event. My main problem is that one menu visibility state was based on if all sub menus are not visible then it should not either. To do so I was iterating on it's child and checking the Visible property. The problem is them having Visible to false due to the way it works the parent set it's own Visible to false as well.
I don't like the solution but that's the only way to make it work
// get all sub menus
var subMenus = mnuExport.DropDownItems.Cast<ToolStripItem>().ToList();
// create a list of bool that will store the visible state I want the controls to have. put them to true by default
var menusVisibleStates = Enumerable.Repeat(true, menus.Count).ToList();
// set the visibility state of each sub menus
for (int i = 0; i < menus.Count; i++)
{
// some logic is here that choose true or false but is irrelevant here
var visibleStateIWant = true;
// set the visible state stored
menusVisibleStates[i] = false;
// set the actual control value that will change later on
menus[i].Visible = false;
}
/// now here I use the state I have specified and NOT the menu object property
mnuExport.Visible = menusVisibleStates.Any(o => o);

Drop-down list in properties window to start at certain index

Background information:
We have a UserControl called Sensor.
Sensor has a property called SlaveSensor.
The type of the property SlaveSensor is Sensor.
public Sensor SlaveSensor;
{
get
{
return _slaveSensor;
}
set
{
//Some more code for checking various stuff...
_slaveSensor; = value;
}
}
As you can see, the type of the property is the same as the UserControl itself.
The property SlaveSensor is normally set via the properties window during design time.
Visual Studio automatically provides the editor as a drop-down list, from which one can select from all available Sensors on the form.
My question is:
How can I make the drop-down list start at a specified instance in the list,
to make it quicker to find the right Sensor to set for the property?
The name of the Sensor to set as the property is always nearly the same as the name of the Sensor for which the property is being set.
So if e.g. the drop-down list would simply auto scroll to the index in the list that has the name of the Sensor for which the property is being set,
I have achieved my goal.
What do I have so far:
I assume that I need to implement a custom property editor.
I might actually be able to create one with a drop-down list, and fill this with strings,
but the existing is OK as it is, I just need to tell it to drop-down to a certain index when clicked.
Thanks for any help in advance!
I'd try this.
string text = "SomeText";
var item = dropdown.Items.FindByText(text);
if(item!= null)
item.Selected = true;
Or by value:
string value = "SomeValue";
var item = dropdown.Items.FindByValue(value);
if (item != null)
item.Selected = true;
Taken from top answer here

How to disable menu items?

I had a problem about Menu. I add a MenuItem in asp.net. I'm using c#. I want to disable Menu Parents and Children with user's permission.
There are 3 users which are "User","Power-User" and "BT_User". "User" have worst permission and "BT_User" have best.
How can I do that? Can anyone answer me?
Implemente your logic from here..
if(UserType == "Power-User")
{
MenuItem mnuItem = Menu1.FindItem("MenuOption"); // If delete a specific item
//to remove
Menu1.Items.Remove(mnuItem);
//to disable and not remove
mnuItem.Enabled = false;
}
if (UserType == "BT_User")
{
Your other logic
}
try this
if (UserType == "Power-User")
{
Menu1.Items.Find("MenuToDelete1", true)[0].Enabled = false;
Menu1.Items.Find("MenuToDelete2", true)[0].Enabled = false;
//or
Menu1.Items.Remove(Menu1.FindItem("MenuToDelete"));
Menu1.Items.Remove(Menu1.FindItem("MenuToDelete2"));
}
if (UserType == "BT_User")
{
Menu1.Items.Find("DeletedItem1", true)[0].Enabled = true;
Menu1.Items.Find("DeletedItem2", true)[0].Enabled = true;
MenuItem item1 = new MenuItem();
item.Text = "DeletedItem1";
MenuItem item2 = new MenuItem();
item.Text = "DeletedItem2";
//or
menuStrip1.Items.Insert(index1, item1);
menuStrip1.Items.Insert(index2, item2);
}
Just leverage the Enabled property of the MenuItem. There is not enough information to tell you how to build the logic around it, but when you want to disable one just do this:
menuItem.Enabled = false;
One thing to note here is that you wouldn't need to continue disabling any children when you disable a parent because with Enabled set to false it's not going to be allowed to fly out any children.
From the MSDN documentation for the Enabled property:
Gets or sets a value that indicates whether the MenuItem object is enabled, allowing the item to display a pop-out image and any child menu items.

How to highlight node in TreeView ? Not only text area, but all row

TreeNode.Select() doesn't work. I want it to be highlighted like
All that I have is
You can use TreeView.FullRowSelect property for this. But remember, it is ignored if ShowLines is set to true.
TreeView.FullRowSelect Property
Gets or sets a value indicating whether the selection highlight spans the width of the tree view control.
public class CustomizedTreeView : TreeView
{
public CustomizedTreeView()
{`enter code here`
// Customize the TreeView control by setting various properties.
BackColor = System.Drawing.Color.CadetBlue;
FullRowSelect = true;
HotTracking = true;
Indent = 34;
ShowPlusMinus = false;
// The ShowLines property must be false for the FullRowSelect
// property to work.
ShowLines = false;
}
protected override void OnAfterSelect(TreeViewEventArgs e)
{
// Confirm that the user initiated the selection.
// This prevents the first node from expanding when it is
// automatically selected during the initialization of
// the TreeView control.
if (e.Action != TreeViewAction.Unknown)
{
if (e.Node.IsExpanded)
{
e.Node.Collapse();
}
else
{
e.Node.Expand();
}
}
// Remove the selection. This allows the same node to be
// clicked twice in succession to toggle the expansion state.
SelectedNode = null;
}
}
follow this link
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.treeview.padding?view=netframework-4.7.2

How to disable only the checkboxes (and not the full checkedboxlist) of devexpress?

In non-edit mode, I need the user to be able to browse the list (scroll etc.) but not be able to select the checkboxes.
If I do checkedboxlist.enabled = false, then the whole list becomes disabled. Only I need to disable checkboxes so that user doesn't interact (in edit way) in non-edit mode.
EDIT
I just assign the list of strings to the checkedboxlist's datasource.
this.UserSelectedMsgTypes.DataSource = userSelectedMsgs;
this.UserAvailableMsgTypes.DataSource = availableMsgTypeList;
currently enabling/disabling whole list by doing
this.UserSelectedMsgTypes.Enabled = true/false;
this.UserAvailableMsgTypes.Enabled = true/false;
I tried #James solution earlier, doesnt work. Because somehow the 'ItemCount' is 0 even though there are items. in the datasource it shows that there are 6 items, but in list it shows 0.
Try this:
foreach (DevExpress.XtraEditors.Controls.CheckedListBoxItem item in checkedListBoxControl1.Items)
{
item.Enabled = false;
}
It's a bit of a dirty work around but how about this?
private IEnumerable<DevExpress.XtraEditors.Controls.CheckedListBoxItem> GetCheckItems(string[] myStringArray)
{
foreach(string s in myStringArray)
{
DevExpress.XtraEditors.Controls.CheckedListBoxItem item = new DevExpress.XtraEditors.Controls.CheckedListBoxItem();
item.Description = s;
yield return item;
}
}
Call with:
checkedListBoxControl1.Items.AddRange(GetCheckItems(new string[] {"test1","test2","test3"}).ToArray());
Then apply the first answer with the foreach loop (or set the ENabled = false in the GetCheckItems method).

Categories