Accessing a combobox inside a ToolStrip - c#

I have a ContextMenuStrip for a right-click context menu. Inside is a ToolStripMenuItem that contains a ToolStripItem array. Inside the array is a dynamically created ToolStripComboBox.
The issue I am having is that I cannot access the meat of the combobox. It has been linking to a databinding through the ComboBox. A general version of the code is below.
myToolStripComboBox.ComboBox.DataSource = enumList;
ToolStripItem[] toolStripItems = new ToolStripItem[1];
toolStripItems[0] = myToolStripComboBox;
ToolStripMenuItem sortOrder = new ToolStripMenuItem("Sorter", null, toolStripItems);
rightClickPopupMenu.Items.Add(sortOrder);
Looking through the debug shows that the datasource is indeed still set and I have all the required values, but the right-click context menu refuses to show anything but a blank combobox.

Try this :
private void chData_MouseMove(object sender, MouseEventArgs e)
{
try
{
//your combobox binding Code
tooltip.SetToolTip(cmdfoo, tipInfo);
}
catch { }
}
This is just a simple example.

Had the local c# wizard at work look at it. Something along the lines of the databinding not being called by a proper parent object.
The solution was to just directly add the times to the combo box items.
dropDown.ComboBox.Items.Add(enum);
So what I have done is make a list of the enum objects and done a foreach to add them all.

Related

Selecting an item in windows form listbox by typing?

I have a listbox with country names. I´m using Windows Forms in VS2015 (C#).
While selecting a name in listBox by typing, it only allows one letter. So if I type "A" it will jump to the first item starting with "A" but if I press "As", listbox viewing the items starting with "s".
I found this answer for combobox and textbox:
Selecting an item in comboBox by typing
but look's like listbox doesn't support AutoCompleteMode.
Is there any solution ?
Please, consider implementing your own searching method. ListBox doesn't support required functionallity by design. Anyway, you can prepare a method on TextChanged event for TextBox which at the time searches for results in collection.
Here's some sample code. Drop a TextBox above your ListBox. Wire up the TextChanged event appropriately, and this should mimic the autocomplete behaviour of a ComboBox (for example)...
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.AddRange(new[] { "Tom", "Dick", "Harry", "Henry" });
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
var textBox = (TextBox)sender;
listBox1.SelectedIndex = textBox.TextLength == 0 ?
-1 : listBox1.FindString(textBox.Text);
}
}
Check this one maybe:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/a8e31841-1ba9-4990-b0e3-bdee489555af/autocomplete-textbox-on-listbox?forum=csharpgeneral
Or this one:
http://vbcity.com/blogs/xtab/archive/2012/09/22/windows-forms-combining-autocomplete-and-listbox-selection.aspx
You should use a ComboBox with DropDownStyle.Simple. The ListBox was never intended to have this functionality, and forcing it to do so is usually a waste of time better spent.
You may also want to consider a third party control. Telerik, for example, has the DropDownList which extends a ComboBox and makes it do exactly what you want to do, with options on how it does it.

Accessing the data of the selected item of a RadListView on doubleclick (WinForms)

I've got a question I've got a RadListView which I populate with a custommade list of ListViewDataItems (all under WinForms).
this.listView.Items.Add(new ListViewDataItem(myCustomId, new string[] { fileName, fileSizeInMB});
Then I added a doublclick event:
listView.DoubleClick += mainFormListView_DoubleClick;
And declared the event:
void listView_DoubleClick(object sender, EventArgs eventArgs)
I then access the currently selected item with listView.Items[listView.SelectedIndex].
So far so good.
But when I try to access the data I had put into that Item the problems start.
In total I want to get the "myCustomId" of the selected item when an item is doubleclicked,
but all I manage to get is a ListViewDataItem that holds not a single Data, and only contains format properties.
Can anyone tell me what I'm doing wrong there or how exactly I can access the previously stored ID?
thanks
When you created the ListViewDataItem, you set myCustomId as the Text property.
... new ListViewDataItem(myCustomId, ...
So see if the Text property on the SelectedItem has the value you're looking for.
private void radListView1_DoubleClick(object sender, EventArgs e)
{
var myCustomId = radListView1.SelectedItem.Text;
}
You may also want to test radListView1.SelectedItem to make sure it's not null before trying to access the Text property.
From your comment:
the Debugger still says that neither function exists (only format functions when I get the mouse over "listView.SelectedItem" on the line var a = listView.SelectedItem
When a place a break point at runtime and inspect SelectedItem, I see both properties have a value:

use custom control in windows forms checked list box

The Items property of a checked list box control in Windows forms is of type object, so my naive hope was that I can add a customized User control as item. (Since, usually, my task is to write logic for background tasks I'm not too familiar with UI programming, so this may be a stupid idea..)
More precisely I want to display two labels and a button in each line of the the checked list box. The first label is supposed to display the name of an object the user can select (so that later on a specific operation will be performed on all checked items). For any item checked, the button is supposed to allow the user to choose a file from which custom settings can be read for performing that operation and the second label should display the choice the user has made using the button (i.e. the file name or something like the string "default settings").
So, in the forms designer, I created a custom control CustomControl1 with label1, label2, button1, and methods to set the text properties, set autosize of the labels and the button to false, defined their size manually. Then in the main window I created the checked list box, to which I added custom controls. The constructor of my main window now looks as follows:
InitializeComponent();
UserControl1 uc1 = new UserControl1();
uc1.setLabel1("label1_text");
uc1.setLabel2("label2_text");
uc1.setButtonText("button_text");
this.checkedListBox1.Items.Add(uc1);
uc1.Visible = true;
This compiles without any error and also runs, but the checked list box shows an empty field. I also experimented with the size of the list box. If I reduce the height so that the check box just fits into it then I do see fragments of the button in the corresponding line, but no label.
Is it possible to use a custom form in a checked list box and if yes, what am I missing?
No, you can't do this.
The listbox only shows a list of elements. The listbox uses the property .ToString() for each objects in the list to show the items.
You need to look for a custom listbox
private void Form1_Load(object sender,EventArgs e)
{
checkedListBox1.Items.Add("IIT");
checkedListBox1.Items.Add("CSE");
checkedListBox1.Items.Add("EEE");
checkedListBox1.Items.Add("ICT");
checkedListBox1.Items.Add("URP");
checkedListBox1.Items.Add("ENGLISH");
checkedListBox1.Items.Add("BANGLA");
checkedListBox1.Items.Add("MATH");
}
private void checkedListBox1_SelectedIndexChanged(object sender,EventArgs e)
{
//var item=checkedListBox1.SelectedItem;
label1.Text=checkedListBox1.SelectedItem.ToString();
}

How to programmatically enable \ disable nested sub-menu items in a ToolStripMenuItem?

In my Winforms application I have a ToolStripMenuItem with nested sub-items, the structure of which is shown below.
File
.+...Add As....+.....File
............................Folder
............................Root Folder
Under 'Add As' I want to be able to programmatically enable and disable 'File', 'Folder', and 'Root Folder' as required. How can I access these nested items in code?
I have tried ToolStripMenuItem.DropDownItems[0].Enabled = true\false; but this affects 'Add As' and everything below it in the menu hiearachy.
If I use an index greater than zero in the code above I get an 'index out of range' error. How do I go about achieving this functionality?
Simply reference the sub-items by their own names eg:
FileToolStripMenuItem.Enabled = false;
FolderToolStripMenuItem.Enabled = false;
RootFolderToolStripMenuItem.Enabled = false;
Unless I'm missing something, this seems like the simplest answer.
As Hans' hinted in his comment, you are referencing the wrong DropDownItems collection.
To do this using indexes will get ugly quickly.
It's simpler to just reference the parent menu and loop through "its" menu collection:
private void toggleMenu_Click(object sender, EventArgs e) {
foreach (ToolStripMenuItem toolItem in addAsToolStripMenuItem.DropDownItems) {
toolItem.Enabled = !toolItem.Enabled;
}
}
Here is the ugly method, which would be difficult to maintain if you decided later to rearrange your menu structure:
foreach (ToolStripMenuItem toolItem in ((ToolStripMenuItem)((ToolStripMenuItem)menuStrip1.Items[0]).DropDownItems[0]).DropDownItems) {
toolItem.Enabled = !toolItem.Enabled;
}

How to disable ToolStripMenuItem in Context menu Dynamically?

In my windows application i have a context menu with a grid the problem is that I want to disable the ToolStripMenuItem in context menu according to the user previlages.How can i do that. i have done like this but it is not working
private void contextMenuStrip_Machine_Opening(object sender, CancelEventArgs e)
{
toolStripAuthorize.Enabled = INFOpermission.accessAuthorize;
}
but it is not working
You need to set toolStripAuthorize.Enabled to either true or false.
I have no idea what INFOpermission.accessAuthorize is because you didn't show the code that defines that (enum?), but if it's anything other than false, this isn't going to work out like you expect.
I can guarantee that setting the Enabled property of the ToolStripMenuItem that you want to disable to false in the Opening event handler will work. If it's not working for you, you're doing something else wrong, and you need to give us some more information to go on.
If you're stuck, see the sample code here: How to: Handle the ContextMenuStrip Opening Event
EDIT: Armed with new information provided in the comments, I've now isolated the source of the problem. You've assigned the ContextMenuStrip to the RowTemplate of a DataGridView control, and are therefore not able to modify items contained in that context menu in its Opening event handler method.
It turns out that this is a known bug that someone decided was "by design". You can see the original bug report here on Microsoft Connect. The explanation given is that whenever a new row is created based on the RowTemplate (which is how the RowTemplate works), the ContextMenuStrip that you've assigned gets cloned as well. That means the same context menu instance is not used for each row, and whatever properties that you try to set on the original menu items have no effect.
Fortunately, it also gives us a workaround. Like all events, the Opening event passes the actual instance of the ContextMenuStrip that is about to be opened as its sender parameter. This is the context menu whose items you need to modify in order for your alterations to be visible.
So what's the code? It looks like this:
private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
{
ContextMenuStrip cmnu = (ContextMenuStrip)sender;
cmnu.Items[1].Enabled = false;
}
Notice, though, that you'll have to reference the individual menu item that you want to modify by its index. This is just the zero-based position of the item in the menu that you want to modify. You can't use the toolStripAuthorize object like you were trying to do before because a new instance of it has been cloned for each new context menu instance.

Categories

Resources