C# Get a selected TabControl tab in Visual Studio WinForm - c#

I'm looking for the method which returns a bool on whether or not a tab in a TabControl is selected. I would think there would be something like tabPage1.IsSelected() but there isn't. I found this: TabControl.SelectedTab Property However, this SelectedTab property is absent from my WinForms class for some reason. Not sure if it's been taken out or what. Thanks.

I tried below code to know which tab is selected.
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabControl1.TabPages["operationstabPage"])
{
//add your code here
}
}

To get the selected tab in tab control, you can use -
var selectedTab = this.tabControl1.SelectedTab;
So you can do -
bool tabSelected = this.tabControl1.SelectedTab == this.tabPage1;

Related

C# contextmenustrip doesn't show any indication if any item is selected

I have added a context menu strip 'View' on my listview with some menu items, like Large icons/ Small icons/ Tiles.
Now whenever I select any of the options the respective view changes, but the menu doesn't get any Mark/ indication like that happens in Windows file explorer, where it shows bullet/ dot against the selected menu item.
Can someone please show, how I can get the similar dot/ bullet for my context menu?
I have tried CheckOnClick property which gets me a tick mark, but is there any other way that I could get that dot there?
Thanks in advance!
I couldn't find a way to get bullets/ dots like Windows File Explored's View options, but I used below logic and used checked states to indicate the selections made.
private void toolStripViewOptions_Click(object sender, EventArgs e)
{
ToolStripMenuItem selectedOption = sender as ToolStripMenuItem;
SetIndicationForSelectedOption(selectedOption);
}
private void SetIndicationForSelectedOption(ToolStripMenuItem selectedMenuItem)
{
ToolStripItemCollection menuItems = (contextMenuStrip.Items[(Int32)toolStripView.Tag] as ToolStripMenuItem).DropDownItems;
// Set checked state for only the selected view option and disable same for others.
foreach (ToolStripMenuItem item in menuItems)
{
if (selectedMenuItem == item)
selectedMenuItem.Checked = true;
else
item.Checked = false;
}
}
This worked for my requirement.

RadioButton.IsChecked Not Going False

I have a UWP app in C#. I have a Radio Button that the user can turn check or uncheck. It is checked by default and the user can uncheck it when they wish. However when they uncheck it the bool value for RadioButton.IsChecked never returns false making impossible for the user to then recheck the Radio Button if required. My code is below.
private void usingmltBtn_Click(object sender, RoutedEventArgs e)
{
if (RadioButton.IsChecked == true)
{
RadioButton.IsChecked = false;
i2cerrRec.Visibility = Visibility.Collapsed;
i2cerrTxt.Visibility = Visibility.Collapsed;
mltI2Cnck = 0;
}
else if (RadioButton.IsChecked == false)
{
RadioButton.IsChecked = true;
mlttempLbl.Text = "N/C";
}
}
Thanks for your help.
As other's have described, RadioButtons are designed to work in a group. This means that you will have multiple RadioButtons for a user to select from but they can only have one checked.
When a RadioButton is checked, it cannot be unchecked by the user by clicking it again without using a custom behaviour that checks if the user has tapped the RadioButton but this would not be recommended.
I suggest that you use the CheckBox for this particular functionality. You can even re-template a CheckBox control to look like a RadioButton if you wish.
Go through the url below
https://learn.microsoft.com/en-us/windows/uwp/controls-and-patterns/radio-button
You might wrong in XAML code!
Use this
private void usingmltBtn_Click(object sender, RoutedEventArgs e)
{
if (RadioButton.Checked == true)
{
RadioButton.Checked = false;
i2cerrRec.Visibility = Visibility.Collapsed;
i2cerrTxt.Visibility = Visibility.Collapsed;
mltI2Cnck = 0;
}
else if (RadioButton.Checked == false)
{
RadioButton.Checked = true;
mlttempLbl.Text = "N/C";
}
}
I can see 2 problems in here:
1- I might be wrong but the method you posted is triggered by a control whose name is usingltBtn (Not descriptive by the way) if this is the case you might be running this code with the trigger of another control.
2- Either the name of the RadioButton is usingltBtn or not you are not referencing the code to your control RadioButton is the name of a class not a specific control.
How to fix this? If the name of your control is usingltBtn use usingltBtn.IsChecked instead of RadioButton.IsChecked if not find the name of your control and use it

C# WPF DataGrid ScrollIntoView not working with touch

I try to scroll to the last selected item in a WPF DataGrid on the Loaded event. The DataGrid sits in a Tab. Everything is working when I test it in a normal Windows environment. But as soon as I touch the TabPage on a tablet instead of clicking it, it doesn't scroll to my last selected item.
This is my code so far:
private void dataGrid_Loaded(object sender, RoutedEventArgs e)
{
var currentItem = dataGrid.SelectedItem;
dataGrid.ItemsSource = sh.GetDataTable(<SQL Select statement>).DefaultView;
if (!(currentItem == null))
{
dataGrid.ScrollIntoView(currentItem);
}
}
I've also tried the solution I found here but it didn't work.
Edit:
For testing purposes I completely removed the dataGrid_Loaded event. Now I'm only loading data into the DataGrid at the start of the program. Even now it keeps the scroll position when I switch between tabs with mouse clicks but not with touch! Is this a bug in the .NET Framework?
With the help of the MSDN Community I was able to solve the problem.
I had to scroll to the end of the DataGrid, do a UpdateLayout() and then scroll to the Item I want.
Additionally I can't just set the ItemsSource every time because then the Item I saved before is not a valid Item of the DataGrid anymore.
So finally my dataGrid_Loaded method looks like this:
private void dataGrid_Loaded(object sender, RoutedEventArgs e)
{
object currentPos = dataGrid.SelectedItem;
if (dataGrid.ItemsSource == null)
{
dataGrid.ItemsSource = sh.GetDataTable("<SQL query>").DefaultView;
}
else
{
dataGrid.Items.Refresh();
}
if (currentPos != null)
{
dataGrid.ScrollIntoView(dataGrid.Items[dataGrid.Items.Count - 1]);
dataGrid.UpdateLayout();
dataGrid.ScrollIntoView(currentPos);
}
}
I hope this will help someone else who has the same problem.
For reference here is my german MSDN thread in which my problem was solved.

C# Populating a TreeView with TabControl

So I've been able to populate a TreeView with the tabnames in WPF/XAML binding but haven't done this before with C# Windows Forms.
I want to have the treeview display the project name based on what file is open and then tabcontrol names below it (these are static -- one is called editor and the other fields).
I'll add a context menu later, but the sole purpose would be to make the tabs visible based on their state with click events from the treeview.
My problem is I can't figure out how to associate them in the treeview. I found this code, can anyone tell me if I'm on the right track here?
private void treeView1_AfterSelect(Object sender, TreeViewEventArgs e)
{
// Set the visibility of the tabpages from the treeview
if ((e.Action == TreeViewAction.ByMouse))
{
if (e.Node.Name == "Editor")
{
this.editForm.tabControl1.SelectedTab = editForm.Editor;
}
if (e.Node.Name == "Fields")
{
this.editForm.tabControl1.SelectedTab = editForm.Fields;
}
}
}
You could use the TreeNodes's Tag property to hold the associated Tab Name.
if (e.Action == TreeViewAction.ByMouse)
{
TabPage p = tabControl1.TabPages[e.Node.Tag]
tabControl1.SelectedTab = p;
}

CheckedListBox Control - Only checking the checkbox when the actual checkbox is clicked

I'm using a CheckedListBox control in a small application I'm working on. It's a nice control, but one thing bothers me; I can't set a property so that it only checks the item when I actually check the checkbox.
What's the best way to overcome this?
I've been thinking about getting the position of the mouseclick, relative from the left side of the checkbox. Which works partly, but if I would click on an empty space, close enough to the left the current selected item would still be checked. Any ideas regarding this?
I know this thread's a bit old, but I don't think it's a problem to offer another solution:
private void checkedListBox1_MouseClick(object sender, MouseEventArgs e)
{
if ((e.Button == MouseButtons.Left) & (e.X > 13))
{
this.checkedListBox1.SetItemChecked(this.checkedListBox1.SelectedIndex, !this.checkedListBox1.GetItemChecked(this.checkedListBox1.SelectedIndex));
}
}
(With the value of CheckOnClick = True).
You could use that thingy with the rectangle, but why make it more complex the it needs to.
Well, it is quite ugly, but you could calculate mouse hit coordinates against rectangles of items by hooking on CheckedListBox.MouseDown and CheckedListBox.ItemCheck like the following
/// <summary>
/// In order to control itemcheck changes (blinds double clicking, among other things)
/// </summary>
bool AuthorizeCheck { get; set; }
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if(!AuthorizeCheck)
e.NewValue = e.CurrentValue; //check state change was not through authorized actions
}
private void checkedListBox1_MouseDown(object sender, MouseEventArgs e)
{
Point loc = this.checkedListBox1.PointToClient(Cursor.Position);
for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
{
Rectangle rec = this.checkedListBox1.GetItemRectangle(i);
rec.Width = 16; //checkbox itself has a default width of about 16 pixels
if (rec.Contains(loc))
{
AuthorizeCheck = true;
bool newValue = !this.checkedListBox1.GetItemChecked(i);
this.checkedListBox1.SetItemChecked(i, newValue);//check
AuthorizeCheck = false;
return;
}
}
}
Another solution is to simply use a Treeview.
Set CheckBoxes to true, ShowLines to false, and ShowPlusMinus to false and you have basically the same thing as a CheckedListBox. The items are only checked when the actual CheckBox is clicked.
The CheckedListBox is much more simplistic, but the TreeView offers a lot of options that can potentially be better suited for your program.
I succesfully used this property:
CheckedBoxList.CheckOnClick
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.checkedlistbox.checkonclick?view=netframework-4.7.2
The text for a checkbox in a CheckedListBox is rendered by default is to place an HTML label after the checkbox input and set the label's "for" attribute to the ID of the checkbox.
When a label is denoting an element that it is "for," clicking on that label tells the browser to focus on that element, which is what you're seeing.
Two options are to render your own list with separate CheckBox controls and text (not as the Text property of the CheckBox, as that does the same thing as the CheckBoxList) if the list is static or to use something like a Repeater if the list is dynamic.
Try this. Declare iLastIndexClicked as a form-level int variable.
private void chklst_MouseClick(object sender, MouseEventArgs e)
{
Point p = chklst.PointToClient(MousePosition);
int i = chklst.IndexFromPoint(p);
if (p.X > 15) { return; } // Body click.
if (chklst.CheckedIndices.Contains(i)){ return; } // If already has focus click anywhere works right.
if (iLastIndexClicked == i) { return; } // native code will check/uncheck
chklst.SetItemChecked(i, true);
iLastIndexClicked = i;
}
Just checking to see if the user clicked in the leftmost 15 pixels of the checked list (the check box area) works at all times except re-checking a currently selected item. Storing the last index and exiting without changing lets the native code handle that properly, trying to set it to checked in that case turns it on and it just turns back off when the "ItemCheck" code runs.

Categories