Weird checkbox behaviour on c# winform - c#

I have a c# winform with tabControl (it has 5 Tabs), textboxes, datetimepicker and checkboxes, connected via Entity Framework to a table.
All my checkboxes have their property Checked set to False, CheckState set to Unchecked and ThreeState set to False.
When I'm adding a new record, my checkboxes encounter a very strange behaviour. At adding a new record, my program calls a subroutine called EmptyCheckBoxes to make sure, that they are all set to false and their texts are set to "Ne". So far so good --> they are set to false and texts are correct(see image1).
Here are 2 scenarios with my problem:
1.
I click one checkbox to make him checked (set to true and set its text to "Da" --> look at Ponedeljek on image2). Till here, everything went as planned.
But, if I use my mouse and try click other checkbox (look at Torek on image3), this checkbox remains unchecked and all other checkboxes on my form have become grayed and checked!
2.
At adding a new record checkboxes are set to false and texts are correct(see image1). But, this time I'm not going to touch my checkboxes, I just click into my datetimepicker and then into some other textbox. Bam!
All checkboxes on my form have become grayed and checked!
How can this be, if their ThreeState property is set to False?
This is really annoying and I'm totaly lost, where to look to find the cause of this problem. Because, if I save this new record, all checkboxes lose their gray color and stay checked with texts "Ne" (values true are saved into table!).
All checkboxes have this code (this is from one of them):
private void checkBox49_Click(object sender, EventArgs e)
{
if (checkBox49.Checked == true)
{
checkBox49.Text = "Da";
}
else
{
checkBox49.Text = "Ne";
}
}
private void checkBox49_CheckedChanged(object sender, EventArgs e)
{
if (checkBox49.Checked == true)
{
checkBox49.Text = "Da";
}
else
{
checkBox49.Text = "Ne";
}
}
This is a code for setting checkboxes to false and texts to "Ne":
private void EmptyCheckBoxes()
{
TabPage tabPage1 = tabControl1.TabPages[0];
TabPage tabPage2 = tabControl1.TabPages[1];
TabPage tabPage3 = tabControl1.TabPages[2];
TabPage tabPage4 = tabControl1.TabPages[3];
TabPage tabPage5 = tabControl1.TabPages[4];
tabControl1.SelectedTab = tabPage1;
UncheckCheckBoxes(tabPage1);
tabControl1.SelectedTab = tabPage2;
UncheckCheckBoxes(tabPage2);
tabControl1.SelectedTab = tabPage3;
UncheckCheckBoxes(tabPage3);
tabControl1.SelectedTab = tabPage4;
UncheckCheckBoxes(tabPage4);
tabControl1.SelectedTab = tabPage5;
UncheckCheckBoxes(tabPage5);
}
private void UncheckCheckBoxes(Control ctrl)
{
CheckBox chkBox = ctrl as CheckBox;
if (chkBox == null)
{
foreach (Control child in ctrl.Controls)
{
UncheckCheckBoxes(child);
}
}
else
{
chkBox.Checked = false;
chkBox.Text = "Ne";
}
}
Thank you for any hint, clue or solution to this problem.
Vladimir

Fabio, you were right about _Click eventhandler. But it did not solve my problem.
My problem are causing groupboxes on my winform. Why, I don't know.
This is my workaround for CheckStateChanged event that works like charm:
private void My_checkBox_CheckStateChanged(object sender, EventArgs e)
{
if (_addnew == true)
{
CheckBox my_cb = (CheckBox)sender;
if ((my_cb.CheckState == CheckState.Indeterminate) && (my_cb.Text == "Da"))
{
my_cb.Checked = false;
my_cb.Text = "Ne";
}
}
}

Related

How to select one Button at a time in a Button Group in .NET Winforms

I have made a custom button control and want it to behave like RadioButtons in a group. The button will be placed in a group or collection of its type on the same container or panel (button dragged_dropped onto the container). When i click any of these buttons, i need the following to happen:
Change the BackColor of the clicked button to a predefined color: Color.Cycan;
Set the checked state of the clicked button from false to true
Ensure that all other buttons of the same type on the same panel/container are set to their default settings which are:
BackColor = Color.Gray;
checked = false;
I have tried the below code but i am obtaining this result:
The BackColor of the clicked button is changing successfully to Color.Cycan;
The default BackColor of all other buttons of the same type on the same container is being set to default successfully: Color.Gray;
But the checked state of the clicked button is not changing to true. It remains false.
I have attached the code i have tried, below:
What could be the issue or where am i missing the point?
This is a Winform / Windows Forms button control and i am programming in C#.
I do not want to use a radio button for this because i have custom designed MyButton.
Thank you.
THIS IS THE CODE I HAVE TRIED:
// Calling 'ClickedButton();' on click event.
private bool checked = false;
private void ClickedButton()
{
do(
BackColor = Color.Cycan;
checked = true;
if(Parent == null)
break;
foreach(MyButton button in Parent.Controls.OfType<MyButton>())
{
if(button == this) { continue }
else{
BackColor = Color.Gray;
checked = false;
}
}
)
while(false)
}
How about use this method?
private bool isSelected = false;
private Color activeColor = Color.FromArgb(117, 117, 117);
private Color defaultColor = Color.FromArgb(66, 66, 66);
private void ClickedButton()
{
foreach (Control parentControl in Parent.Controls)
{
if (parentControl is MyButton parentButton)
{
parentButton.isSelected = false;
parentButton.BackColor = parentButton.isSelected ? parentButton.activeColor : parentButton.defaultColor;
}
}
this.isSelected = true;
this.BackColor = isSelected ? activeColor : defaultColor;
}
It works like this.

Enabling button when checkbox in datagridview is clicked?

As the title suggests, my code is designed to detect if there's at least one checkbox checked on datagridview. If there is, a button will be enabled. If not, enabled property becomes false.
public void validatecheck(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn && e.RowIndex >= 0)
{
foreach (DataGridViewRow row in dtgeducation.Rows)
{
if (Convert.ToBoolean(row.Cells[e.ColumnIndex].Value) == true)
{
btnaddclass.Enabled = true;
break;
}
else
{
btnaddclass.Enabled = false;
}
}
}
This code still not working. Maybe I'm overlooking a essential part. Any help is greatly appreciated. :)
I would bind a bool property in your view-model to the IsChecked property on the checkbox, then assign that property's value to update the Enabled property of the button.

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

hide and unhide context strip menu item c#

if(e.Button == MouseButtons.Right)
{
string signatureDate = dataGridView3.CurrentRow.Cells[8].Value.ToString();
// MessageBox.Show(signatureDate);
if(signatureDate.Length > 5)
{
contextMenuStrip1.Items[0].Visible = false;
contextMenuStrip1.Items[1].Visible = true;
}else
{
contextMenuStrip1.Items[0].Visible = true;
contextMenuStrip1.Items[1].Visible = false;
}
}
I have a context strip menu that is working in my datagridview. And I selected it as Row Context Strip Menu.
What I am trying to do is to get if selected row of datagridview and control signature column is null or not. If it has signature date I want to hide or unhide "Sign" and if it doesn't have signature date hide "Unsign" item on context menu strip.
You can see in picture I enclosed.Context menu Strip
EDIT: Name of the event is MouseDown.
EDIT 2: With editing this code I can get columns data and show them on messageBox. But I can not use those data as a condition. Therefore it is not working. For example, when I select a row that is without "Signature Date" and show it on messageBox, it is working. But when I use Signature Date data as a condition It is not working. I know it is so strange and too easy to overcome but I coundn't because of that I didn't catch anything.
EDIT 3: Event
EDIT 4 (SOLVED) : I created to Context Strip Menu and specify no one of them
as Context strip Menu of Datagridview.
With Datagridview_MouseDown event, I am getting Signature Date column data and check if it is null/empty or not. If it is null/empty I specify first Context Menu strip as Context Strip Menu of Datagridview or not I do revise. I figured out the solution in this way :)
I think your problem is in the instance of context menu strip use this one see if it helps.
if(e.Button == MouseButtons.Right)
{
string signatureDate = dataGridView3.CurrentRow.Cells[8].Value.ToString();
// MessageBox.Show(signatureDate);
if(signatureDate.Length > 5)
{
dataGridView3.ContextMenu.Items[0].Visible = false;
dataGridView3.ContextMenu.Items[1].Visible = true;
}else
{
dataGridView3.ContextMenu.Items[0].Visible = true;
dataGridView3.ContextMenu.Items[1].Visible = false;
}
}
Probably your event is not firing.
Instead of using mouse down you could also use the Opening event of the contextMenuStrip
This should solve your problem
private void Form1_Load(object sender, EventArgs e)
{
dataGridView3.ContextMenu = contextMenuStrip1;
contextMenuStrip1.Opening += contextMenuStrip1_Opening;
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
string signatureDate = dataGridView3.CurrentRow.Cells[8].Value.ToString();
// MessageBox.Show(signatureDate);
if (signatureDate.Length > 5)
{
contextMenuStrip1.Items[0].Visible = false;
contextMenuStrip1.Items[1].Visible = true;
}
else
{
contextMenuStrip1.Items[0].Visible = true;
contextMenuStrip1.Items[1].Visible = false;
}
}

Popup with StaysOpen to false is not closing

I have a telerik grid view and when I right click the header, I'm showing a with a ListBox inside containing the list of columns.
The item template is redefined to show a check box so I can set the column visible or not.
I can also drag/drop columns to reorder them.
So here is how I create my Popup:
var view = new ColumnsOrderer.ColumnsOrderer
{
DataContext = new ColumnsOrderer.ViewModelColumnsOrderer(Columns)
};
var codePopup = new Popup
{
Child = view,
MaxHeight = 400,
StaysOpen = false,
Placement = PlacementMode.Mouse
};
codePopup.IsOpen = true;
Now everything seems to work correctly, but it's not.
If I set columns visible or hidden and then click outside the popup, it closes correctly.
Though if I drag an item to reorder it, the popup seems to lose focus and then it won't close if I click outside the popup. I have to click back in the list box inside the popup and then it closes by clicking outside.
Here is my drag/drop events:
public ColumnsOrderer()
{
InitializeComponent();
InitialiazeListBoxDragDrop();
}
private void InitialiazeListBoxDragDrop()
{
var itemContainerStyle = new Style(typeof(ListBoxItem));
itemContainerStyle.Setters.Add(new Setter(AllowDropProperty, true));
itemContainerStyle.Setters.Add(new EventSetter(PreviewMouseMoveEvent, new MouseEventHandler(OnMouseMove)));
itemContainerStyle.Setters.Add(new EventSetter(DropEvent, new DragEventHandler(OnDrop)));
listColumns.ItemContainerStyle = itemContainerStyle;
}
void OnMouseMove(object sender, MouseEventArgs e)
{
if (e.OriginalSource is CheckBox || e.LeftButton == MouseButtonState.Released)
return;
if (sender is ListBoxItem)
{
var draggedItem = sender as ListBoxItem;
draggedItem.IsSelected = true;
DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
}
}
void OnDrop(object sender, DragEventArgs e)
{
if (!(sender is ListBoxItem))
return;
}
An interesting thing is that if I remove the OnDrop handler, the problem is not there.
I tried many ways to set back the focus to the popup, but it's not working.
Could anyone help me on that?
How about trying to re-focus your Popup control after the drag and drop operation?
void OnDrop(object sender, DragEventArgs e)
{
if (!(sender is ListBoxItem))
return;
codePopup.Focus();
}

Categories