Dynamically add SubMenuItems to a SubMenu - c#

I have a c# menu strip with top-level menu items (TLM items). I am dynamically adding items to one of the TLM items as follows, which works great.
DataRowCollection DRC = DataAccessClass.GetData("SELECT * FROM company ORDER BY CompanyName");
ToolStripMenuItem[] items = new ToolStripMenuItem[DRC.Count];
int itemCounter = 0;
foreach (DataRow dr in DRC)
{
string nm = dr["companyname"].ToString();
int id = Convert.ToInt16(dr["companyid"].ToString());
items[itemCounter] = new ToolStripMenuItem();
items[itemCounter].Name = string.Format("menuitem{0}", itemCounter);
items[itemCounter].Text = nm;
items[itemCounter].Click += new EventHandler(MenuItemClickHandler);
itemCounter++;
}
CompanyToolStripMenuItem.DropDownItems.AddRange(items);
Once this TLM has been populated, I want to dynamically add sub-menu items to each of the dynamic menu items created above. I am similarly creating an array of ToolStripMenuItems as above, and I am trying to add them to a menu item using this, shown here for the first menu item:
CompanyToolStripMenuItem.DropDownItems[0].DropDownItems.AddRange(submenuitems);
But it isn't working. Any ideas?
When I add CompanyToolStripMenuItem.DropDownItems[0] to a watch window, it is showing a "DropDownItems" property. When I try to type it in, the auto-complete drop-down isn't exposing the property as an option.

Try casting the selected DropDownItem item to a ToolStripMenuItem:
((ToolStripMenuItem)CompanyToolStripMenuItem.
DropDownItems[0]).DropDownItems.AddRange(submenuitems);

Related

WinAppDriver how to select item in ComboBox

I have problem with selecting item from ComboBox,
I able to unfold ComboBox but can't select one of the items
Simply
var comboBEle = winDriver.FindElementByName("Combo1");
comboBEle.Click(); // unfold comboBox
winDriver.FindElementByName("Item1").Click(); // don't work
I tried also:
var comboB = sapSession.FindElementByClassName("Combo1");
var selEle= new SelectElement(comboB);
selEle.SelectByIndex(1);
Why winDriver don't see this element?

UWP - Update listview source dynamic from code behind

I have a group item. Then, each group in group item, i put it into a listview
var Groups = query.GroupBy(query => query.Name);
foreach (var group in Groups)
{
if (group.records.Count() > 2)
{
ListView listview = new ListView();
var itemsource = new ObservableCollection<FileProperties>();
var header = "";
foreach (var item in group.records)
{
header = item.Name;
itemsource.Add(new FileProperties(item.Name, item.Size, item.DateModified, item.Hash, item.Path, item.IsOrigin));
}
listview.ItemsSource = itemsource;
var itemsTemplate = (DataTemplate)this.Resources["Show"];
listview.ItemTemplate = itemsTemplate;
//test is mother listview
test.Items.Add(listview);
}
}
Now, i have a question, how can i update listview UI if i change value in group items without reset mother listview
The default ListView can be grouped by using CollectionViewSource. There is no need to create a child ListView for each group of the parent ListView.
My answer here shows how to create a grouped ListView, you may take a look. Or there are a bunch of demos on internet, you can googling them.
But the most important point here is that by using the default grouped ListView, we can simply create one data collection for the whole ListView, modify the items source collection to update the grouped children automatically, we don't need to create ObservableCollections for each group any more.

How to Add Items From One ListView to Another

I am trying to parse the data in the first column of one ListView called raw and then if the data is correct, add that item to a second ListView called result.
However, when I go to run my program I get
the error:
"Cannot add or insert the item 'Collected' in more than one place.".
My Code
ListView result = new ListView();
for (int i = 0; i < raw.Items.Count; i++)
{
if (raw.Items[i].SubItems[0].Text.ToUpper() == "COLLECTED")
{
MessageBox.Show("confirm");
result.Items.Add(raw.Items[i]); // generating erros
}
}
printUsingLView(result);
clone original item:
result.Items.Add((ListViewItem)raw.Items[i].Clone());
or, if you want to make some adjustments
ListViewItem newItem = new ListViewItem();
newItem.Text = raw.Items[i].Text;
//enter other properties here and then add it to new listView
result.Items.Add(newItem);

binding selected items from one list box to another listbox

I have two list boxes in my form. I bound some data in the first list box. Now I have to select the items from the first lis tbox and should bind those selected items to the second list box when I press the button which is between these two list boxes. I am able to bind a single item at a time but I am having problem binding multiple selected items.
I am using the following code:
Hashtable ht = new Hashtable();
ht.Add(lbCATallSubcat.SelectedValue.ToString(),lbCATallSubcat.Text.ToString());
int i = 0;
foreach (string ent in ht.Values) {
string[] name = new string[lbCATallSubcat.Items.Count];
for (i = 0; i < lbCATallSubcat.SelectedItems.Count; i++) {
name[i] = lbCATallSubcat.Text;
this.lbCATSelectedSubcat.Items.Add(name[i]);
}
lbCATSelectedSubcat.DisplayMember = ht.Values.ToString();
lbCATSelectedSubcat.ValueMember = ht.Keys.ToString();
}

Using string for control objects

For my current project i made a MDIform with "menuStrip" and a couple of "ToolStripMenuItem".
a couple of buttons and a devexpress "NavbarControl"
The intention is that the user logs in with a userID
the application will get a datarow for a specific "Control"
in this row theirs a bool, if its true the Item must be visible, otherwise the item must be invisible.
the Datarow also contains the name of the item.
so i uses:
this.Controls[item].Visible = true;
item = string(name of item)
if i use this to hide the menustrip itself, it works
if i try it on the MenuStipItems, it gives a null reference exception.
how can i control the items INSIDE the MenuStip, only by name of the item???
Code:
DataTable dt = GetData();
foreach (DataRow row in dt.Rows)
{
string item = row["ItemNaam"].ToString();
foreach (string rol in Rollen)
{
DataRow dr = GetDataByItemNaam(item);
if (Convert.ToBoolean(dr[rol]) == true)
{
this.Controls[item].Visible = true; //Show Item
}
}
}
The MenuStrip control has it's own collection. So to reference the menu strip items, reference the items from the menustrip parent:
if (this.menuStrip1.Items.ContainsKey(item))
this.menuStrip1.Items[item].Visible = true;
I've solved the problem:
I created a foreach loop within a foreach loop where
each loop looks for the name of the item, and then for the name of the item in the previous item.
If the name matches the given name, it sets the visibility to true.
This is for 2 levels, I created an additional two extra foreach loops to go even deeper (inception) to 4 levels of items in the menu.
Perhaps its not the right/fastest way, but it works like it should.

Categories

Resources