Enable and Disable ToolStripMenu entries in C#? - c#

I have a Form that contains a Menu with two entries, which are Menu and Tools. The two Menues have some SubMenus.
Now i have a TextBox called txtSelect and a Button called btnVisible, if I enter 1,2 in the TextBox, the SubMenus in the Menu should not be visible. I written the following code, bit it is Hard-written.
ToolStripMenuItem[] mstrip = new ToolStripMenuItem[] { msO1, msO2, msO3, msP1, msP2, msP3 };
if (txtSelect.Text.Length > 2)
{
string word = txtSelect.Text;
string[] splt = word.Split(',');
for (int x = 0; x < mstrip.Length; x++)
mstrip[x].Visible = true;
for (int x = 0; x < splt.Length; x++)
{
int y = Convert.ToInt32(splt[x].ToString()) - 1;
if (y >= 0 && y < mstrip.Length)
mstrip[y].Visible = false;
textBox1.AppendText(mstrip[y].Text);
textBox2.AppendText(mstrip[y].OwnerItem.Text);
}
}
I want to use foreach loop instead in a Button Click Event and have attempted with the following, however the result is not the same as with the code above.
foreach (ToolStripMenuItem mnItem in msMenus.Items)
{
MessageBox.Show(mnItem.Text);
for (int i = 0; i < mnItem.DropDown.Items.Count; i++)
{
MessageBox.Show(mnItem.DropDown.Items[i].Text);
mnItem.DropDown.Items[i].Visible = true;
}
}

Well, may be you want something like :
List<Int32> lstindex = new List<Int32>();
String[] splt = txtSelect.Text.Split(',');
// initialize list of indexed for textbox
foreach (String str in splt)
{
lstindex.Add(Convert.ToInt32(str) - 1);
}
// for each menu
foreach (ToolStripMenuItem mnItem in msMenus.Items)
{
// for each menu item
foreach (ToolStripItem item in mnItem.DropDown.Items)
{
// if index of item is in the list of indexed, set visible to false, otherwise to true
item.Visible = lstindex.Contains(mnItem.DropDown.Items.IndexOf(item)) ? false : true;
}
}

Related

Randomly check items in checklistbox

I have a checklistbox and I want to randomly check a set number of the items in the textbox (CheckAmount.Text) - i.e. if the user enter 60%(0.60), I want 60% of the items in the checked listbox to be checked. Is this possible or even close?
int CA = Convert.ToInt32(CheckAmount.Text);
for (int i = 0; i <= CA; i++)
{
}
I can get close, but what you are saying is to:
Check blah amount of CheckBoxes in CheckBoxList foo
Code:
//The CheckBoxList name is boxes
int CA = Convert.ToInt32(CheckAmount.Text);
Random rng = new Random();
int x = 0;
for (int y = CA; y != 0; y--)
foreach (CheckBox i in boxes.Controls)
{
x = rng.Next(1, boxes.Length + 1); //have to add 1 or it will never pick the last box
if(boxes[boxes.Controls.IndexOf(i)] == x - 1)
{
i.Checked = true;
y--;
}
else
{
continue;
}
}
What this does is it looks through all of the checkboxes, and randomly selects blah checkboxes from boxes and checks them. blah is CA in your code.
Hope it helps!
Techcraft7
If you want to check exactly 60% of the rows (or the closest that rounding errors will let you get), you should sort the rows randomly then take the first 60% of them.
For example:
var r = new Random();
var checkboxes = this.Controls.OfType<CheckBox>();
float totalBoxes = checkboxes.Count();
var targetCount = (int)(totalBoxes * 0.60F);
var targetItems = checkboxes
.OrderBy( c => r.Next() ) //Sort randomly
.TakeWhile( (c,i) => i <= targetCount ); //take the first 60%
foreach (var c in targetItems) c.Checked = true;

C# Checkbox-Dialog How to uncheck

I have a Windows Forms Dialog in C# which shows Checkboxes for each Element in a Dictionary. The Dialog returns a List with all selected Elements(Checkboxes). However I noticed that if I select a Checkbox and then uncheck it again, the Element is still in the returned List of Selected Elements.
How can I fix this?
My Dialog looks like this:
public SelectDialog(Dictionary<string, string> Result)
{
int left = 45;
int idx = 0;
InitializeComponent();
for (int i = 0; i < Result.Count; i++)
{
CheckBox rdb = new CheckBox();
rdb.Text = Result.Values.ElementAt(i).Equals("") ? Result.Keys.ElementAt(i) : Result.Values.ElementAt(i);
rdb.Size = new Size(100, 30);
this.Controls.Add(rdb);
rdb.Location = new Point(left, 70 + 35 * idx++);
if (idx == 3)
{
idx = 0; //Reihe zurücksetzen
left += rdb.Width + 5; // nächste Spalte
}
rdb.CheckedChanged += (s, ee) =>
{
var r = s as CheckBox;
if (r.Checked)
this.selectedString.Add(r.Text);
};
}
}
//Some more Code
}
As per the comment:
You need to remove the items from the list if the raised event is unchecked, I think you have to check for already added items to avoid duplicates, and remove the items if exists. so the handler would be like this:
rdb.CheckedChanged += (s, ee) =>
{
var r = s as CheckBox;
var itemIndex = this.selectedString.IndexOf(r.Text)
if (r.Checked && itemIndex == -1)
this.selectedString.Add(r.Text);
else if(!r.Checked && itemIndex != -1)
{
this.selectedString.RemoveAt(itemIndex);
}
};

Populate TextBoxes from a List

I am trying to populate TextBoxes from a list. I have been able to populate ComboBoxes with comboList:
var comboList = new System.Windows.Forms.ComboBox[4];
comboList[0] = cmbSite1Asset;
comboList[1] = cmbSite2Asset;
comboList[2] = cmbSite3Asset;
comboList[3] = cmbSite4Asset;
List<CRCS.CAsset> assets = _rcs.Assets;
foreach (CRCS.CAsset asset in assets)
{
string id = asset.ID;
for (int i = 0; i < 4; ++i)
{
comboList[i].Items.Add(id);
}
}
But when I try and apply the same principle to TextBoxes
var aosList = new System.Windows.Forms.TextBox[8];
aosList[0] = txtAsset1;
aosList[1] = txtAsset2;
aosList[2] = txtAsset3;
aosList[3] = txtAsset4;
aosList[4] = txtAsset5;
aosList[5] = txtAsset6;
aosList[6] = txtAsset7;
aosList[7] = txtAsset8;
foreach (CRCS.CAsset asset in assets)
{
string id = asset.ID;
for (int n = 0; n < 8; ++n)
{
aosList[n].Items.Add(id);
}
}
TextBox does not like Items.Add ( aosList[n]Items.Add(id); )
I am looking fore a reference or guidance resolving this issue. Thanks!
You should use ComboBox for your problem,instead of iterating on each element,You simply use below lines to populate combobox.
comboList.DataSource=assets;
comboList.DisplayMember="ID";
comboList.ValueMember="ID";
However,if you want your values in TextBox,you can use TextBox.AppendText Method, but it will not work like ComboBox as it will contain texts+texts+texts, will not have indexes like ComboBox.
private void AppendTextBoxLine(string myStr)
{
if (textBox1.Text.Length > 0)
{
textBox1.AppendText(Environment.NewLine);
}
textBox1.AppendText(myStr);
}
private void TestMethod()
{
for (int i = 0; i < 2; i++)
{
AppendTextBoxLine("Some text");
}
}
A Combobox is a collection of items, and so has an Items property from which you can add/remove to change it's contents. A Textbox is just a control that displays some text value, so it has a Text property which you can set/get, and which denotes the string that is displayed.
System.Windows.Forms.TextBox[] aosList = new System.Windows.Forms.TextBox[8];
aosList[0] = txtAsset1;
aosList[1] = txtAsset2;
aosList[2] = txtAsset3;
aosList[3] = txtAsset4;
aosList[4] = txtAsset5;
aosList[5] = txtAsset6;
aosList[6] = txtAsset7;
aosList[7] = txtAsset8;
for (int n = 0; n < 8; ++n)
{
aosList[n].Text = assets[n].ID; // make sure you have 8 assets also!
}
int i = 1;
foreach (var asset in assets)
{
this.Controls["txtAsset" + i].Text = asset.ID;
i++;
}

Comparing checklistbox items to array

So, i have a checklistbox that contains string values like:
asdf-432-qwer-vcxz
rewq-123-qwer-vcxz
rety-323-qw65-vcyt
kjhf-232-ouyy-bjkl
...
And i have an array(onlineVaults) that contains some of the same values that in checklistbox like:
rety-323-qw65-vcyt
asdf-432-qwer-vcxz
Now i want to check only those values in checklistbox that are in the array. Others should be unchecked.
for (int i = 0; i < checklistbox.Items.Count; i++)
{
if (onlineVaults.Contains(checklistbox.Items[i]))
{
checklistbox.SetItemChecked(i, true);
}
}
I have tried it to figure out like this, but it doesn't work. It only checks one value and nothing else. What i should do?
Try this
var checklistbox = new List<string>
{
"asdf-432-qwer-vcxz",
"rewq-123-qwer-vcxz",
"rety-323-qw65-vcyt",
"kjhf-232-ouyy-bjkl"
};
var onlineVaults = new List<string>
{
"rety-323-qw65-vcyt",
"asdf-432-qwer-vcxz"
};
for (int i = 0; i < checklistbox.Items.Count; i++)
{
checklistbox.SetItemChecked(i, onlineVaults.Contains(checklistbox.Items[i]));
}
EDIT:
To ignore case and trim values, try this
for (int i = 0; i < checklistbox.Items.Count; i++)
{
checklistbox.SetItemChecked(i, IsValueExist(onlineVaults, checklistbox.Items[i]));
}
private bool IsValueExist(List<string> list, string value)
{
return list.Any(x => string.Compare(x.Trim(), value.Trim(), StringComparison.InvariantCultureIgnoreCase) == 0);
}

SubMenu in C# using WindowsForm?

I have a menustrip consists of Menu and Tools
in "Menu" i have subMenus like msO1,msO2,msO3......., and on "Tools" i have subMenus like msP1,msP2,msP3.......,
on Form load all the subMenus visible is false..., On button Click user want to select which subMenus he want...,
in the textBox(txtSelect) if user enter 1,3..., he get msO1, msO3.....,
my code is a hardcode..., if i have 20 subMenus means this code is not helpfull anybody have an idea...,
private void btnSelect_Click_1(object sender, EventArgs e)
{
msO1.Visible = false;//msO1 is a submenu
msO2.Visible = false;
msO3.Visible = false;
msP1.Visible = false;
msP2.Visible = false;
msP3.Visible = false;
string word = txtSelect.Text;
string[] splt = word.Split(',');
int[] arrayItms = new int[splt.Length];
for (int x = 0; x < splt.Length; x++)
{
arrayItms[x]=Convert.ToInt32(splt[x].ToString());
if (splt.Length > 0)
{
switch (arrayItms[x])
{
case 1:
msO1.Visible = true; break;
case 2:
msO2.Visible = true; break;
case 3:
msO3.Visible = true; break;
case 4:
msP1.Visible = true; break;
case 5:
msP2.Visible = true; break;
case 6:
msP3.Visible = true; break;
}
}
}
}
Create an array of your MenuStrip
MenuStrip[] mstrip = new MenuStrip[]
{
msO1,msO2, msO3, msP1, msP2, msP3 // add other menus here when needed
};
now you could work on the array as a Whole to make visible or not your menus
for(int x = 0; x < menus.Length; x++)
mstrip[x].Visible = false;
and your code could be simplified with
for (int x = 0; x < splt.Length; x++)
{
int menuIndex;
if(Int32.TryParse(splt[x], out menuIndex))
{
menuIndex--;
if(menuIndex >= 0 && menuIndex < mstrip.Length)
mstrip[menuIndex].Visible = true;
}
}
Remember, arrays indexes start at zero (while your user will probably start counting a 1).
You could use something like this
string word = txtSelect.Text;
string[] splt = word.Split(',');
for (int x = 0; x < splt.Length; x++)
{
Control myControl1 = FindControl("ms" + splt[x]);
if ( myControl1 != null )
(ToolStripMenuItem)myControl1.Visible = true;
}
Untested but this should get you half way there I hope.
Loop through each ToolStripMenuItem control in the menu strip items and set them to visible.
You can add further conditions inside the loop to define which of the menu items should be made visible based on the users choice..
foreach (ToolStripMenuItem mi in menuStrip1.Items)
{
mi.Visible = true;
}

Categories

Resources