I got a menu item, which when you press different toolstripmenu items show. But I want to add subtoolstripmenu items to a toolstripmenuitem. This is how I thought it would work:
ToolStripMenuItem[] items = new ToolStripMenuItem[10];
for (int i=0;i<10;i++)
{
items[i] = new ToolStripMenuItem();
items[i].Name = i;
items[i].Text = i;
items[i].Tag = i;
items[i].Click += new EventHandler(MenuItemClickHandler);
}
toolStripMenuItem1.DropDownItems[2].AddRange(items); //not possible
toolStripMenuItem1.DropDownItems.AddRange(items); // possible
Sadly it only works when I use toolStripMenuItem1.DropDownItems.AddRange(items); but not when I use toolStripMenuItem1.DropDownItems[2].AddRange(items);. Anyone any idea how to do this?
I dont want it to expand at the red cross, i want the green circle: http://imgur.com/a/mFWAz
LarsTech's comment is correct.
Replace the last 2 lines with:
ToolStripMenuItem subMenu = toolStripMenuItem1.DropDownItems[2] as ToolStripMenuItem;
subMenu.DropDownItems.AddRange(items);
Related
Premise:-
We have a list of key value pairs.
The Item count of this list will vary.
We have a form with a bunch of default buttons on it. (Edit - Built earlier in the designer, not at runtime.)
The buttons are named "button1, button2, ..."
We have more buttons than items in the list.
At runtime we want to transfer information from the list elements to the buttons and hide the unused buttons.
My question is how to address those buttons from withing a loop?
Using a for loop in VBA I could say this:-
Me.Controls("TB_Item" & Format(i, "00")).Visible = False
In C# I have this minimal example as a starting point (The form has 10 buttons):-
public UF_ButtonLoop()
{
InitializeComponent();
List<KeyValuePair<string, string>> MyItems = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Apple", "Green Fruit"),
new KeyValuePair<string, string>("Orange", "Orange Fruit"),
new KeyValuePair<string, string>("Sprout", "Spawn of the Devil"),
new KeyValuePair<string, string>("Hershey Bar", "A bit like chocolate"),
new KeyValuePair<string, string>("Beefburger", "Man Food")
};
//Loop through the 10 buttons
for (int i = 1; i < 11; i++)
{
if (i <= MyItems.Count )
{
//Transfer Data from list to button
//Pseudo code
Control("Button" + i).Text = (MyItems.ElementAt(i).Key);
Control("Button" + i).Tag = (MyItems.ElementAt(i).Value);
}
else
{
//Hide the button as we've reached the end of the list so have no use for it.
//Pseudo code
Control("button" + 1).Hide();
}
// Note, VBA methos is:-
// Me.Controls("TB_Item" & Format(i, "00")).Visible = False
}
}
Control("Button" + i) is not correct syntax.
Can I do this in C#, if so how?
If not what is the correct way?
Also I'm new here so if I'm asking things in the wrong way please don't be shy in telling me so!
Many thanks,
Owen S.
Firstly thanks for the help, it's appreciated, especially the nudge towards doing it via other methods, or even different controls.
The answer to the question is probably split into two options:-
(Answer 1) - Do it properly by creating just what you need at runtime, rather than using the static designer and then hiding what was over-built.
(Answer 2) - Quick "bodge" to get the code in my initial question working. (My C# experience can be measured in hours so I have plenty to learn but also need to get code working today for work...)
So one bodge method that works is to put the buttons in their own list and then reference that. Like so:-
public UF_ButtonLoop()
{
InitializeComponent();
//TransferListToButtone_Rev1();
List<KeyValuePair<string, string>> MyItems = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Apple", "Green Fruit"),
new KeyValuePair<string, string>("Orange", "Orange Fruit"),
new KeyValuePair<string, string>("Sprout", "Spawn of the Devil"),
new KeyValuePair<string, string>("Hershey Bar", "A bit like chocolate"),
new KeyValuePair<string, string>("Beefburger", "Man Food")
};
List<Control> ListOfButtons = new List<Control>
{
button1, button2, button3, button4, button5, button6, button7, button8, button9, button10
};
void A_Button_Click(object sender, System.EventArgs e)
{
Console.WriteLine((sender as Button).Text + " = " + (sender as Button).Tag);
}
//Loop through the 10 buttons
for (int i = 0; i < 10; i++)
{
if (i < MyItems.Count )
{
//Transfer Data from list to button
ListOfButtons.ElementAt(i).Text = (MyItems.ElementAt(i).Key);
ListOfButtons.ElementAt(i).Tag = (MyItems.ElementAt(i).Value);
//Set Click Event
ListOfButtons.ElementAt(i).Click += new EventHandler(A_Button_Click);
}
else
{
//Hide the button as we've reached the end of the list so have no use for it.
ListOfButtons.ElementAt(i).Hide();
}
}
}
Cheers all,
Owen S.
I would recommend to create those buttons when you read the list. Do not create the buttons statically in designer. You need to create a List of buttons in the main Form and then add the buttons programmatically.
List creation
List<Button> buttons = new List<Button>();
Then in some loop where you go thru your data list
Button MyButton = new Button();
Mybutton.Location = new Point(YourX, YourY);
Mybutton.Text = "AnyText";
Mybutton.AutoSize = false;
MyButton.Size = new Size(width, height);
Mybutton.BackColor = Color.LightBlue;
Mybutton.Padding = new Padding(6);
buttons.add(MyButton);
Ad1.
The best practice is to create your own "SpecialButton" class which is Inheritted from original Button class. Then you can add some special attributes to the button as is reference on some other element which should the SpecialButton control and etc...
First of all please excuse my lousy English :(
I’m trying to copy the Outlook-SendTo-Context-Menu.
When you type in a EMail address, it will ShowUp and „Auto-Complete-Menu“ with an X to delete entries out of the list.
And there is my Problem, i can create a Conext Menu like:
foreach (string item in hist)
{
if (String.IsNullOrWhiteSpace(item)) continue;
MenuItem mI = new MenuItem();
Button mB = new Button();
mB.Content = "X";
mB.Click += new RoutedEventHandler(menuItemClick_Delete);
mI.Header = item;
mI.Tag = item;
mI.Click += new RoutedEventHandler(menuItemClick_SelectFolder);
//mI.Icon = mB;
mB.Tag = mI;
// mB.IsMouseOver +=
//submI.Header = "Löschen";
//submI.Tag = item;
///submI.Click += new RoutedEventHandler(menuItemClick_Delete);
cm.Items.Add(mI);
}
return cm;
I tryed to add a Icon/Image, but it looks ugly. Because the button is in-front of the text and not behind.
I will post you an Image of what i mean ->
Maybe there is already a Component for, or its not working like i thought.. i'm open for almost everything :)
Thanks for far!
I'm still new at coding. I'm making a calculator but I also want a lot of other things in it. like a conversion calculator, cook book, and kanji radical dictionary in c# WindowsFormsApplication I want to change from one to the next using a comboBox so I was going to make a array with all the control I wish to hide/show
string[] numList = {"button0","button1","button2", "button3"};//this will have all number and .
for (int i = 0; i < numList.Length; i++)
{
numList[i].Hide();
}
But it tell me there no definition for 'Hide' but when I switch numList[i] to button0 it work but I don't wish to wright the same 11 control for every time i add something to the comboBox anyway to fixes this or any other method
If you want to hide all Buttons then try this:
foreach (Button control in Controls.OfType<Button>())
{
(control).Hide();
}
This iterate through all Buttons of the form and hide them. But if you want to just hide a specific buttons then you can set the Tag property of that buttons to something like OP then to hide only that Buttons:
foreach (Button control in Controls.OfType<Button>())
{
if (control.Tag.ToString() == "OP")
{
(control).Hide();
}
}
Or with linq:
foreach (Button control in Controls.OfType<Button>().Where(control => control.Tag.ToString() == "OP"))
{
(control).Hide();
}
try below code
private void btnHide_Click(object sender, EventArgs e)
{
string[] buttonList = { "button1", "button2", "button3" };
for (int i = 0; i < buttonList.Length; i++)
{
Control[] ctrl = this.Controls.Find(buttonList[i], true);
((Button)ctrl[0]).Visible = false;
}
}
You are keeping a list of strings, you should actually add the buttons in to the list in order to have the Hide method visible
Control[] numList = {button0, button1, button2, button3 };
I'm having a problem in interacting with a custom dropdown control. It works fine the 1st 6 times, but after that, since the screen is resized, it could no longer locate and click the option in the dropdown control, returning an exception - can't click on hidden control. I tried putting in a itemField.DrawHighlight(); on the control I'm looking for, and it finds it, however it can't click on it. I also tried a to scroll down, but it seems to be not working.
bool addItemCheck = false;
int scrollCheck = 0;
while (Check == false)
{
var addItem= new HtmlButton(window);
addItem.SearchProperties.Add(HtmlButton.PropertyNames.Id, "add-new-item");
Mouse.Click(addItem);
scrollCheck = scrollCheck + 1;
if (scrollCheck > 6)
{
Mouse.MoveScrollWheel(window, -100);
}
var itemDropDown = new HtmlSpan(window);
itemDropDown .SearchProperties.Add(HtmlSpan.PropertyNames.Class, "item-dropdown");
itemDropDown .SearchProperties.Add(HtmlSpan.PropertyNames.InnerText, "Select an Item");
Mouse.Click(itemDropDown );
addItemCheck = itemDropDown.Exists;
}
bool itemBoxCheck = false;
HtmlCustom itemBox = null;
while (itemBoxCheck == false)
{
itemBox = new HtmlCustom(window);
itemBox.SearchProperties.Add(HtmlCustom.PropertyNames.Id, "item-listbox");
var itemField = new HtmlCustom(itemBox);
itemField .SearchProperties.Add(HtmlCustom.PropertyNames.InnerText, item);
Mouse.Click(itemField);
itemBoxCheck = itemBox.Exists;
}
I would really appreciate any help. Thank you.
Try calling the method InsureClickable() on the control before attempting to click on it.
for example:
itemDropDown.EnsureClickable();
Mouse.Click(itemDropDown);
Edit:
if this doesn't work you'll have to scroll down to the item.
try using:
Mouse.MoveScrollWheel()
if that doesn't work also you'll have to map the scroll control and click on it.
I'm currently working on something that is probably done in plenty of examples out there. But after some searching I can't find anything.
I'm working with WPF tab control and I'm trying to recreate some basic functionality (which you see in all internet browsers nowadays) to add a new tab by clicking a '+' tab which is the last listed tab.
I already have the '+' tab which adds a new tab. My problem is, I want to move the '+' tab after the new tab (so its the end tab again) and switch view to the new tab that has just been created.
I thought something like:
void tiNewTab_Add(object sender, EventArgs e)
{
int idx = tabControl1.Items.Count;
tabControl1.SelectedIndex = idx - 1;
TabItem ti = new TabItemKPI();
tabControl1.Items.Add(ti);
tabControl1.Items.MoveCurrentToLast();
}
...would work but no luck :(
Try something like this:
tabControl1.Items.Insert(tabControl1.Items.Count-1,ti);
This will do because you always have at least one TabItem (the + one)
Then select the second last one by
tabControl1.SelectedIndex=tabControl1.Items.Count-2;
Not tested, but following should work:
int idx = tabControl1.Items.Count;
tabControl1.SelectedIndex = idx - 1;
TabItem ti = new TabItem();
tabControl1.Items.Insert(tabControl1.Items.IndexOf(tabControl1.Items.Last()), ti);