How to change the checked state of a ToolStripItem in Winforms? - c#

When I look it up, they list it as having a .Checked property. But both in Visual Studio and on msdn, it doesn't list any kid of Checked property.
ContextMenuStrip menu = new ContextMenuStrip ( );
var menuItem = menu.Items.Add ( "CheckedItem" );
//menuItem.Checked?
Is there a way to do this?

You need to cast to ToolStripMenuItem:
((ToolStripMenuItem)menuItem).Checked = true;

I had 2 checked items on or off, so I used this format:
private void onToolStripMenuItem_Click(object sender, EventArgs e)
{
offToolStripMenuItem.Checked = false;
}
private void offToolStripMenuItem_Click(object sender, EventArgs e)
{
onToolStripMenuItem.Checked = false;
}

This code will change StripMenuItem checked state after every mouse click.
Note: Tool Strip menu item name is: uruchomZSystememToolStripMenuItem
private void uruchomZSystememToolStripMenuItem_Click(object sender, EventArgs e)
{
uruchomZSystememToolStripMenuItem.Checked = !uruchomZSystememToolStripMenuItem.Checked;
}

Related

C# WPF System.Windows.Controls.TreeView how to change selected node

This treeview class works differently from the windows forms class. Every example I see to change selected node uses items.selectednode or nodes.selectednode.
However, I am struggling to find a method of doing this for this class:
https://msdn.microsoft.com/en-us/library/system.windows.controls.treeview(v=vs.110).aspx
I wanted to enable right click to popup my context menu. Could not find another solution, so here's what I did, and it works:
private void TreeSetup_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
((TreeViewItem)sender).IsSelected = true;
e.Handled = true;
}
private void TreeSetup_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
ContextMenu PopupMenu = this.FindResource("cmButton") as ContextMenu;
if (TreeSetup.SelectedItem != null)
{
PopupMenu.PlacementTarget = sender as TreeViewItem;
PopupMenu.IsOpen = true;
}
}

Edit Items in a ListBox

I am creating a program using WinForms so users can input info into textboxes on one form which then are saved into a Listbox on another form. I would like to be able to edit the items saved in the listbox by opening the original form on a button click. Really struggling with it as I can't think of the code and I can't seem to find a solution.
My Code:
private void btnAdd_Click(object sender, EventArgs e)
{
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.ShowDialog();
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
listBoxRooms.Items.Add(newRoomDisplayForm.value);
}
newRoomDisplayForm.Close();
}
private void btnRemove_Click(object sender, EventArgs e)
{
this.listBoxRooms.Items.RemoveAt(this.listBoxRooms.SelectedIndex);
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
So i've got a Add and Remove button which work perfectly just need a solution to the edit button.
Thanks in advance
I'm guessing newRoomDisplayForm.value is a property or a public member inside the form. You just need to do something like this:
private void btnEdit_Click(object sender, EventArgs e)
{
if(listBoxRooms.SelectedIndex < 0) return;
var tmpValue = listBoxRooms.Items[listBoxRooms.SelectedIndex].ToString();
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.value = tmpValue;
newRoomDisplayForm.ShowDialog();
//TODO: inside "newRoomDisplayForm" set the value to the textbox
// ie.: myValueTextBox.Text = this.value;
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
// replace the selected item with the new value
listBoxRooms.Items[listBoxRooms.SelectedIndex] = newRoomDisplayForm.value;
}
}
Hope it helps!
You can simply remove the listitem in that specific position, create a new item and add it again. it's kind of replacement.

Telerik RadMenu dropdown submenus on mouseover

You know, after clicking on RadMenuItem, submenus are opening. But this not effective for me. I want that, when onmouseover event, submenus are opened automatically. Norally in winforms below codes realize my wish:
private void menuStrip_MouseEnter(object sender, EventArgs e)
{
var menu = (ToolStripMenuItem)sender;
menu.ShowDropDown();
}
Could you help me, how do I this in RadMenu?
EDIT: I tried like this:
private void rmiAna_MouseEnter(object sender, EventArgs e)
{
var menu = (RadMenu)sender;
menu.IsSubmenuOpen = true;
}
When I tried above codes, I got error like this:
Could you help me?
Kind regards.
To show the sub menu items of root items, you can iterate all menu items and subscribe to the MouseEnter event of the root ones. In the handler, call the following method:
private void radButton1_Click(object sender, EventArgs e)
{
IterateItems(radMenu1.Items);
}
void IterateItems(RadItemOwnerCollection items)
{
foreach (RadMenuItemBase item in items)
{
if (item.IsRootItem)
{
item.MouseEnter += item_MouseEnter;
}
if (item.HasChildItemsToShow)
{
IterateItems(item.Items);
}
}
}
void item_MouseEnter(object sender, EventArgs e)
{
RadMenuItem hoveredItem = (RadMenuItem)sender;
hoveredItem.DropDown.Show();
}

Open the last used RibbonTab when the wpf RibbonWindow is loaded

How can I return to the last used RibbonTab that was in focus when the window was last closed?
You could create a variable to hold the reference to the RibbonTab and listen to the SelectionChanged event on your Ribbon object.
MyRibbonObj.SelectionChanged += delegate(object sender, SelectionChangedEventArgs args)
{
RibbonTab rt = ((sender as Ribbon).SelectedItem as RibbonTab);
MyReferenceToRibbonTab = rt;
}
This way you keep track of the latest selected RibbonTab within your Ribbon.
PS: code might needs tweaks. I didn't test it.
Create a setting LastRibbonTab
Save the last tab used in MainWindow_Closed
MainWindow_Closed(object sender, EventArgs e)
{
Properties.Settings.Default.LastRibbonTab = (MyRibbon.SelectedItem as RibbonTab).Header.ToString();
Properties.Settings.Default.Save();
}
Select the last tab in MainWindow_Loaded
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Init();
foreach (RibbonTab r in MyRibbon.Items)
if (r.Header.ToString() == Properties.Settings.Default.LastRibbonTab)
{
MyRibbon.SelectedItem = r;
break;
}
}

C# ListView with CheckBoxes, automatic checkbox checked when multi select rows

I'm using a ListView control with multirow and fullrow select on. When I'm selecting multiple rows at once, some of my rows magically become checked. This happens when dragging the mouse over and also when selecting one, and shift clicking another.
See image describing issue here:
What in the grapefruit is going on? Anyone?
Unfortunately there are bugs in the ListView class, this is one of them. The following code is a fix that worked for me.
Edit: Sorry, this doesn't work quite right, although it does prevent the error that you show in your question. This prevents selecting multiple items and then checking them by clicking the check box.
void SetupListView()
{
listView.ItemCheck += new ItemCheckEventHandler(listView_ItemCheck);
listView.MouseDown += new MouseEventHandler(listView_MouseDown);
listView.MouseUp += new MouseEventHandler(listView_MouseUp);
listView.MouseLeave += new EventHandler(listView_MouseLeave);
}
bool mouseDown = false;
void listView_MouseLeave(object sender, EventArgs e)
{
mouseDown = false;
}
void listView_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
void listView_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
void listView_ItemCheck(object sender, ItemCheckEventArgs e)
{
if(mouseDown)
{
e.NewValue = e.CurrentValue;
}
}
I answered this in another forum:
http://www.codeproject.com/Messages/3417741/Re-how-to-disable-multi-select-of-checkbox-in-list.aspx
HTH - hground
it`s simple question
just try this
private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (ModifierKeys == Keys.Control || ModifierKeys == Keys.Shift)
{
e.NewValue = e.CurrentValue;
}
}

Categories

Resources