Radio menu check - c#

Using Winform, is it possible to "radio check" a menu item?

Yes see MSDN
// This method is called from the constructor of the form to set up the menu items.
public void ConfigureMyMenus()
{
/* Set all of these menu items to Radio-Button check marks so the user can see
that only one color can be selected at a time. */
menuItemRed.RadioCheck = true;
menuItemBlue.RadioCheck = true;
menuItemGreen.RadioCheck = true;
}

Related

C# WinForm TabControl Formatting Issue

When programmatically adding controls to a tab control, I have been using the Form_Load event to create and embed things like datagridviews into my UI. I made a class that inherits from DataGridView
class DBDataGridView : DataGridView
{
public DBDataGridView()
{
DoubleBuffered = true;
AllowUserToAddRows = false;
AllowUserToDeleteRows = false;
AllowUserToResizeRows = false;
AllowUserToOrderColumns = false;
AllowUserToResizeColumns = false;
RowHeadersVisible = false;
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
ReadOnly = true;
Dock = DockStyle.Fill;
SelectionMode = DataGridViewSelectionMode.FullRowSelect;
TabStop = false;
}
}
And I call it later in the Form_Load event like so
private void MainDesignerForm_Load(object sender, EventArgs e)
{
DBDataGridView _DGV = new DBDataGridView();
var listOfOverlays = new List<OverlaySelectionList>()
{
new OverlaySelectionList { Description = "Description 1", PartNumber = "123-R1"},
new OverlaySelectionList { Description = "Description 2", PartNumber = "456-R1"}
};
var overlayList = new BindingList<OverlaySelectionList>(listOfOverlays);
_DGV.DataSource = overlayList;
Tab_Overlay.Controls.Add(_DGV);
_DGV.ClearSelection();
}
This gridview is on the THIRD tab of the TabControl, and everything works as expected except the ClearSelection(). No matter where I call it, it does not clear the initial row selection of the DGV. However, if I fire the same code block from a button ON the third tab, the formatting AND the ClearSelection() behave as expected.
What is causing this behavior?
Thanks to 41686d6564 and Jimi for the insight into the specifics on why this was happening.
Reiterating what they said in the comments: Assignment of properties appear to be cached regardless of whether the control they belong to is active or not (Hence why all the sizing and formatting properties were present at run time). However, actions that require a handle, like ClearSelection() require the control to be shown and active for the intended behavior to be observed.
Setting the selected tab to where the DataGridView before calling ClearSelection() was the solution (Or in my case, I had nested tabs, so I had to follow the tab tree to get to the specific tab that the DataGridView was on)
So now, part of the Load_Form logic is to check WHERE the control is located, make that tab active, THEN format and clear selections for each control that is being added. This allowed ClearSelection() to work as intended.

How to highlight node in TreeView ? Not only text area, but all row

TreeNode.Select() doesn't work. I want it to be highlighted like
All that I have is
You can use TreeView.FullRowSelect property for this. But remember, it is ignored if ShowLines is set to true.
TreeView.FullRowSelect Property
Gets or sets a value indicating whether the selection highlight spans the width of the tree view control.
public class CustomizedTreeView : TreeView
{
public CustomizedTreeView()
{`enter code here`
// Customize the TreeView control by setting various properties.
BackColor = System.Drawing.Color.CadetBlue;
FullRowSelect = true;
HotTracking = true;
Indent = 34;
ShowPlusMinus = false;
// The ShowLines property must be false for the FullRowSelect
// property to work.
ShowLines = false;
}
protected override void OnAfterSelect(TreeViewEventArgs e)
{
// Confirm that the user initiated the selection.
// This prevents the first node from expanding when it is
// automatically selected during the initialization of
// the TreeView control.
if (e.Action != TreeViewAction.Unknown)
{
if (e.Node.IsExpanded)
{
e.Node.Collapse();
}
else
{
e.Node.Expand();
}
}
// Remove the selection. This allows the same node to be
// clicked twice in succession to toggle the expansion state.
SelectedNode = null;
}
}
follow this link
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.treeview.padding?view=netframework-4.7.2

choosing to hide or show tooltips based on bool

so I figured I'm making just a stupid mistake here. In the first of what will be many controls, I need to either show a balloon tooltip when a bool is true or not show them when the bool is false. I know that ShowAlways is not what I need to modify and I've tried various solutions already. Does anyone spot the problem? The bool is set by a checked dropdown item in a Help Menu Strip Item.
It will open with the application with the correct display, but as soon as I check that option to show it, it always shows there after.
public void changeBalloonProperties(bool boolSet)
{
ToolTip helpDeskInfoButtonToolTip = new ToolTip();
if (boolSet)
{
helpDeskInfoButtonToolTip.ToolTipTitle = "HelpDesk Information Button";
helpDeskInfoButtonToolTip.UseFading = true;
helpDeskInfoButtonToolTip.UseAnimation = true;
helpDeskInfoButtonToolTip.IsBalloon = true;
helpDeskInfoButtonToolTip.ShowAlways = true;
helpDeskInfoButtonToolTip.AutoPopDelay = 5000;
helpDeskInfoButtonToolTip.InitialDelay = 1000;
helpDeskInfoButtonToolTip.ReshowDelay = 500;
helpDeskInfoButtonToolTip.SetToolTip(helpDeskButton, "Click to launch HelpDesk user info page in default browser.");
}
else
{
helpDeskInfoButtonToolTip.RemoveAll();
}
}
You are creating a new ToolTip instance each time the changeBalloonProperties is called so the code isn't removing the caption associated with the original ToolTip that was used with the helpDeskButton. Try moving the ToolTip declaration outside of your changeBalloonProperties method so the same ToolTip object is used with RemoveAll().
Also note you can use that same ToolTip object to add captions for multiple controls (as shown in the sample here) and it's probably better to set helpDeskInfoButtonToolTip.Active = false to disable them all at once instead of setting and removing the captions (and other properties) each time you toggle.

Jumping to next tab

I have a tab control in my WPF application with multiple tabs. Each tab gives access to several buttons, text boxes, drop downs. Now before moving to the next tab valid entries in each of the controls in the tab is to be checked or jumping to the next tab should not be allowed. How can this be done?
I was able to use IsEnable property to do this. But I want it like, when I click on the next tab it should, without entering the next tab, display a warning that such and such entry in the present tab is not valid.
If you adhere to the Selected event you can do something like this:
// Keep a global variable for the previous index
int prevIndex = 0;
private void tabControl_Selected(object sender, TabControlEventArgs e)
{
TabControl tc = sender as TabControl;
if (tc != null)
{
bool letSwitchHappen = validateTabControls(tc.SelectedIndex);
if (!letSwitchHappen)
{
tc.SelectedIndex = prevIndex;
}
prevIndex = tc.SelectedIndex;
}
}
Where validateTabControls is something like:
private bool validateTabControls(int tabIndex)
{
bool validEntries = false;
// Some code here to set validEntries according to the control at tabIndex
return validEntries;
}
Take a look at this example from Josh Smith.
It shows explicitly how to do this, and Josh is well-known (and respected) in the WPF world.

Disabling a ToolStripMenuItem vs. disabling a MenuStrip.Item

When a user logs into my application, there are some menu items that I don't want every user to see. So I would like to either disable or make invisible the menu item. For example fileToolStripMenuItem is the first item in my menuStrip, when I try:
fileToolStripMenuItem.Enabled = false; - this does not work
menuStrip.Items[0].Enabled = false; - this does work
Can anyone enlighten me as to the difference here?
Also, I would like to be able to disable a drop down item from one of the menu items, but I cannot do that either.
Here's the code:
public Form1()
{
InitializeComponent();
// bunch of other code here
if (!login.ValidatedUser)
{
menuStrip1.Items[0].Visible = false; // this works
toolsToolStripMenuItem.Visible = false; // this does not
btnStartResourceManager.Enabled = false;
listAvailableSizes.Enabled = true;
panelPicSet.Enabled = true;
}
}
fileToolStripMenuItem.Enabled = false; works as expected. I think you trying to disable it before InitializeComponent(); call.
public form()
{
InitializeComponent();
fileToolStripMenuItem.Enabled = false;//disables all file menu
saveasToolStripMenuItem.Enabled = false; //disables save as menu item in file menu list
}
Use the specific name of your menu item and change its Visible property. i.e.
private void toggleToolStripMenuItem_Click(object sender, EventArgs e)
{
if (shown)
saveToolStripMenuItem.Visible = false;
else
saveToolStripMenuItem.Visible = true;
shown = !shown;
}
For Sub Items, just right click on the item and see its name In Design Section in Properties Window. In my case below addNewToolStripMenuItem1.
public Form()
{
InitializeComponent();
menuStrip1.Items[1].Visible = false; // For Main Item // Bold Letters
addNewToolStripMenuItem1.Visible = false; //For Sub Items
}

Categories