I have set visible property of my menuStrip1 items to false as
foreach (ToolStripMenuItem itm in menuStrip1.Items)
{
itm.Visible = false;
}
Now I know the Names of toolStripMenuItem and dropDownItem of the menustrip1. How to can I activate the required toolStripMenuItem and dropDownItem.
I have
string mnItm = "SalesToolStripMenuItem";
string ddItm = "invoiceToolStripMenuItem";
Now I want to set visible true to these two(toolStripMenuItem and dropDownItem) items. How can I do that? I know those names only.
Simply use those names to get the actual item via MenuStrip.Items indexer:
ToolStripMenuItem menuItem = menuStrip1.Items[mnItm] as ToolStripMenuItem;
ToolStripDropDownMenu ddItem = menuStrip1.Items[ddItm] as ToolStripDropDownMenu;
You can use
menuStrip1.Items[mnItm].Visible = true;
menuStrip1.Items[ddItm].Visible = true;
or if you want to set Visible to multiple toolstrip items:
string [] visibleItems = new [] {"SalesToolStripMenuItem", "invoiceToolStripMenuItem"};
foreach (ToolStripMenuItem item in menuStrip1.Items)
{
if (visibleItems.Contains(item.Name))
{
item.Visible = false;
}
}
Hope it helps
You're looking for ToolStripItemCollection.Find method.
var items = menustrip.Items.Find("SalesToolStripMenuItem", true);
foreach(var item in items)
{
item.Visible = false;
}
second parameter says whether or not to search the childrens.
You should try something like this:
string strControlVal ="somecontrol"; //"SalesToolStripMenuItem" or "invoiceToolStripMenuItem" in your case
foreach (ToolStripMenuItem item in menuStrip1.Items)
{
if (strControlVal == item.Name)
{
item.Visible = false;
}
}
Initialize strControlVal string on your discretion where you need it.
If i get your question you are trying to disable other than the above two mentioned toolstrip items. Since you know the name of the menu items a slight change in code can get you along
foreach (ToolStripMenuItem itm in menuStrip1.Items)
{
if(itm.Text !="SalesToolStripMenuItem" || itm.Text !="invoiceToolStripMenuItem")
{
itm.Visible = false;
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
string MenuItemName = sender.ToString()
}
Related
I have a function which clears all Comboboxes on the form and then try to update the text.
The new text is only shown if no Items are in the Item list.
Does anyone have an idea whats wrong?
I deleted all functions from the project so that only the necessary part is available.
https://www.dropbox.com/s/ibst7enrteyk9jb/Digitales_Auftragsformular.zip?dl=0
The project is attached. Simply start, go to tab "Werkzeuganfrage" there are two Comboboxes in red. One without Items = working, one with Items which is not working.
public Oberflaeche()
{
InitializeComponent();
List<ComboBox> myComboBoxes = GetControlsByType<ComboBox>(this, typeof(ComboBox));
foreach (ComboBox txt in myComboBoxes)
{
//txt.Text = "";
txt.SelectedIndex = -1;
}
Werkzeuganfrage_Combobox_rhino1_1.Text = "Rhino1_1";
Werkzeuganfrage_Combobox_rhino1_2.Text = "Rhino1_2";
comboBox1.Text = "Rhino1_1";
comboBox2.Text = "Rhino1_2";
}
public List<T> GetControlsByType<T>(Control container, Type type)
{
List<T> result = new List<T>();
foreach (Control c in container.Controls)
{
if (c.GetType() == type)
{
result.Add((T)Convert.ChangeType(c, typeof(T)));
}
if (c.HasChildren)
{
result.AddRange(GetControlsByType<T>(c, type));
}
}
return result;
}
I need to to use a checkboxlist because there is a unique case the user is allowed to select 3 items at the same time. Problem is that I can't even manage to get the single selection to work. I'm doing this is codebehind with an updatepanel to not refresh the page.
protected void cblCodeRequest_OnSelectedIndexChanged(object sender, EventArgs e)
{
int count = 0;
int i = 0;
int maxObjects = cblCodeRequest.Items.Count;
string[] checkObjects = new string[maxObjects];
ListItem selectedItem = new ListItem();
foreach (ListItem item in cblCodeRequest.Items)
{
checkObjects[i] = item.Text;
i++;
}
foreach (ListItem item in cblCodeRequest.Items)
{
if (item.Selected)
{
count++;
selectedItem = item;
cblCodeRequest.ClearSelection();
}
foreach (ListItem itm in cblCodeRequest.Items)
{
if (item.Equals(selectedItem))
{
item.Selected = true;
}
}
}
}
I'm not sure how to proceed from here, i already save the selected item and clear the whole selection and then set it again but it doesn't do it, it just selects itself again after even if i click a different checkbox. I think my logic is messed up
Try this, Your foreach statements are not arranged correctly
protected void cblCodeRequest_OnSelectedIndexChanged(object sender, EventArgs e)
{
ListItem selectedItem = cblCodeRequest.Items[cblCodeRequest.SelectedIndex]
cblCodeRequest.ClearSelection();
int x = 0;
for(x; x<cblCodeRequest.Items.Count; x++)
{
if (cblCodeRequest.Items[x].Equals(selectedItem))
{
item.Selected = true;
}
}
}
Ok, I finally solved this problem. I had to dig through the problem with the debugger and follow the logic. I did this in jQuery so I can remove the update panel. If Anyone ever gets the same problem as me, this is the solution for you.
This allows you to use a checkboxlist like a radiobuttonlist but also allows you to check multiple checkboxes that belong to a special selection group.
$(function () {
$('[id*=cblCodeRequest] input').on('click', function () {
var checkboxlist = $('[id*=cblCodeRequest]'); // CheckBoxList
var checkboxArray = $('[id*=cblCodeRequest] input'); // CheckBoxList Items
var current = $(this); // Get Selected Checkbox
var label = $(current).next().text(); // Get CheckBox label name
// Uncheck every checkBox that don't match the unique multi selection combo
if (label != "Recâmbio" && label != "Série" && label != "Embalagem Alternativa") {
// Loop through checkboxArray and uncheck every item that does not meet the condition
$(checkboxArray).each(function (i) {
$(this).prop("checked", false);
});
// Check the selected item again
$(current).prop("checked", true);
} else {
// Get the current checkbox name
var chk = $(current).next().text();
// If the checkbox that matches the unique combo selection was click uncheck all invalid checkboxes that don't match
if (chk == "Série" || chk == "Recâmbio" || chk == "Embalagem Alternativa") {
// loop through the checkboxlist again
$(checkboxArray).each(function () {
// Get the looped item name
var ck = $(this).next().text();
// if checkbox does not belong to the unique combo selection, uncheck it
if (ck != "Recâmbio" && ck != "Série" && ck != "Embalagem Alternativa") {
$(this).prop("checked", false);
}
});
}
}
});
});
I have a listbox, which I fill with a list via ItemsSource = list.
Now, I have another list. And what I want to do is loop through the ListBox, to see if the ListBoxItem.Name is the same. If so, then the ListBoxItem should be selected.
My idea:
List<string> firstList = new List<string>();
List<string> secondList = new List<string>();
Listboxx.ItemsSource = firstList;
foreach (string striing in secondList)
{
foreach (ListBoxItem iitem in Listboxx)
{
if (striing == iitem.Name)
{
iitem.IsSelected = true;
}
}
}
Or is there a way in the ListboxItemTemplate to set the IsChecked bool to {Binding IsCheckedOrNot}?
Nested loops generally should be avoided when possible. Why not do something like this?
foreach(var iitem in Listboxx.Items.Where(i => secondList.Contains(i.Name)))
{
iitem.IsSelected = true;
}
I've been looking for a solution since this morning, and even after reading tons of other threads on this subject it doesn't work for me. Without further ados let's check this code sample:
// Create Dictionary, Keys = Ids, Values = Names
Dictionary<int, string> ff_names = new Dictionary<int, string>();
ff_names.Add(0, "Cloud");
ff_names.Add(1, "Barret");
ff_names.Add(2, "Tifa");
ff_names.Add(3, "Aerith");
ff_names.Add(4, "Red XIII");
// Populating ListView
foreach( KeyValuePair<int, string> dict in ff_names )
{
ListViewItem lvi = new ListViewItem(dict.Key.ToString());
lvi.SubItems.Add(dict.Value);
listView1.Items.Add(lvi);
}
// Test Item Selection
listView1.Focus();
listView1.Select();
listView1.Items[0].Focused = true;
listView1.Items[0].Selected = true;
string s = listView1.SelectedItems.Count.ToString();
label1.text = s; // sadly, it's equal to 0;
textBox1.Text = listView1.SelectedItems[0].SubItems[0].Text; // program will crash
Technically, I would like to selection an item of the ListView and display one of its element in a textbox. It works when I select an item manually, but when I try to select programmatically like shown above it doesn't want to select anything, the SelectedItems count is equal to zero...
Thank you for you help and hope someone can find a solution to what I'm missing!
Here you go. You'll have to make the event handler for listView1_SelectedIndexChanged.
public Form1() {
InitializeComponent();
listView1.View = View.Details;
listView1.Columns.Add("Key");
listView1.Columns.Add("Value");
LoadListView();
}
private void LoadListView() {
// Create Dictionary, Keys = Ids, Values = Names
Dictionary<int, string> ff_names = new Dictionary<int, string>();
ff_names.Add(0, "Cloud");
ff_names.Add(1, "Barret");
ff_names.Add(2, "Tifa");
ff_names.Add(3, "Aerith");
ff_names.Add(4, "Red XIII");
// Populating ListView
foreach (KeyValuePair<int, string> dict in ff_names) {
ListViewItem lvi = new ListViewItem(new string[] { dict.Key.ToString(), dict.Value });
listView1.Items.Add(lvi);
}
// Test Item Selection
listView1.Focus();
listView1.Select();
listView1.Items[0].Focused = true;
listView1.Items[0].Selected = true;
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e) {
if (listView1.SelectedItems.Count > 0) {
label1.Text = (string)listView1.SelectedItems[0].Text; // sadly, it's equal to 0;
textBox1.Text = (string)listView1.SelectedItems[0].SubItems[1].Text;
}
}
I think drankin2112's answer is useful, but you say it doesn't work, so I complement it, hope this could be helpful to you.
To finish your work, you need to do three things as follow:
1.load data and fill in listview;
2.define the process method when listview's selected item changed;
3.give a test method to programmatically select different item, the you can see result.
my sample code is below:
public MainWindow()
{
InitializeComponent();
listView1.View = View.Details;
listView1.Columns.Add("Key");
listView1.Columns.Add("Value");
this.listView1.FullRowSelect = true;
//register the process event
this.listView1.SelectedIndexChanged += this.listView1_SelectedIndexChanged;
//load data
LoadListView();
//test item selection
ToSelectItem(0);
}
void ToSelectItem(int itemIndex)
{
if (itemIndex > listView1.Items.Count - 1)
return;
listView1.Focus();
listView1.Select();
listView1.Items[itemIndex].Focused = true;
listView1.Items[itemIndex].Selected = true;
}
private void LoadListView()
{
// Create Dictionary, Keys = Ids, Values = Names
Dictionary<int, string> ff_names = new Dictionary<int, string>();
ff_names.Add(0, "Cloud");
ff_names.Add(1, "Barret");
ff_names.Add(2, "Tifa");
ff_names.Add(3, "Aerith");
ff_names.Add(4, "Red XIII");
// Populating ListView
foreach (KeyValuePair<int, string> dict in ff_names)
{
ListViewItem lvi = new ListViewItem(new string[] { dict.Key.ToString(), dict.Value });
listView1.Items.Add(lvi);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
label1.Text = (string)listView1.SelectedItems[0].Text; // sadly, it's equal to 0;
textBox1.Text = (string)listView1.SelectedItems[0].SubItems[1].Text;
}
}
I have a list-box, I want to loop through all the selected items and get each selected items text value.
for (int i = 0; i < lstFieldNames.selectedItems.Count; i++)
{
string s = lstFieldNames.SelectedItems[i].ToString();
}
the value of s is "{ Item = ADDR }"
I don't need the { Item = }, I just want the text "ADDR".
What am I doing wrong, I tried a few things and nothing seems to work for me.
Well, this is a Winforms question because an ASP.NET ListBox has no SelectedItems property(notice the plural). This is important since a Winforms ListBox has no ListItems with Text and Value properties like in ASP.NET, instead it's just an Object.
You've also commented that the datasource of the ListBox is an anonymous type. You cannot cast it to a strong typed object later.
So my advice is to create a class with your desired properties:
class ListItem {
public String Item { get; set; }
}
Create instances of it instead of using an anonymous type:
var items = (from i in xDoc.Descendants("ITEM")
orderby i.Value
select new ListItem(){ Item = i.Element("FIELDNAME").Value })
.ToList();
Now this works:
foreach (ListItem i in lstFieldNames.SelectedItems)
{
String item = i.Item;
}
Note that my ListItem class is not the ASP.NET ListItem.
string s = lstFieldNames.SelectedItems[i].Text.ToString();
Note the Text property
This would fetch Selected values only.
EDIT:
foreach (object listItem in listBox1.SelectedItems)
{
string s = listItem.ToString();
}
Use this code :
string s = "";
foreach(var item in lstFieldNames.SelectedItems)
{
s += item.ToString() + ",";
}
textBox1.Text = s;
If need on one selected item from your listbox try this cod :
string s = "";
foreach(var item in lstFieldNames.SelectedItems)
{
s = item.ToString();
}
EDIT :
Try this one code
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string s = "";
foreach (var item in listBox1.SelectedItems)
{
s += item.ToString() + ",";
}
textBox1.Text = s;
}