i have a combobox with a list of SQLusers, which have permissions granted from beforehand.
a table PERMISSION_DETAIL with columns username, permission_name consists the details of permissions granted or denied
now when a user selects a SQLuser from combobox then the permissions are shown as a checkbox, like if permission is already granted, then the checkbox appears and its tick is checked, else UNCHECKED.
for this i use:
if(permission is previously granted)
checkbox1.checked = true; // here the Checkbox_CheckChanged event is called, but i dont want to call it.
checkbox1.enabled = false;
now, besides the checkbox, there is a button, on clicking it, the checkbox gets enabled, that is, to modify the permission, the user will click the button.
now the user will tick or untick the checkbox to grant or deny permission and the checkbox change event
will be called, this would be fine.
i want that as the checkbox appears, its tick is automatically checked, but this calls the
checkbox_Checkchanged event, but i dont want to call that event.
i advice you to change Checkbox_CheckChanged event to Checkbox_Click event.
private void Checkbox_Click(object sender, EventArgs e)
{
if ((sender as CheckBox).Checked)
{
MessageBox.Show("checked");
// add role to user
}
else
{
MessageBox.Show("un_checked");
/remove role ffrom user
}
}
You could check if the checkbox is enabled, before doing anything in the EventHandler:
protected void Checkbox_CheckChanged (object sender, ..EventArgs e)
{
//return if not enabled
if(!((CheckBox)sender).IsEnabled) return;
//DO THE REST
}
I think that if you make an event handler for CheckChanged, it should be called always when "check is changed", and you should handle logic thereafter.
The event is going to be called regardless, what matters is how you handle that event. An event has occurred, you can't pretend it hasn't. The only thing you can do is decide whether that event means anything to you at a particular point in time.
For example:
public void chkPermission_CheckChanged(object sender, EventArgs e)
{
bool isChecked = chkPermission.Checked;
if (this.user.HasPermission() == isChecked) { return; }
// otherwise, we need to change some permissions!
}
Analyse whether or not you need to do anything, and go from there. Don't just blindly assume something needs to change because an event fired.
Add a boolean variable, IsCheckboxEventShouldFire, to your form. Set to true initially. In the code that sets the checkbox to checked automatically, also now set IsCheckboxEventShouldFire to false. In the checkchanged event, first check if the IsCheckboxEventShouldFire is true. If it is, run your code. If not, then don't. Set IsCheckboxEventShouldFire to true at the end of the event's code.
Related
I'm trying to check if any value in any cells has been changed in DataGridview (WinForms C#) before I update the code in a button click.
To accomplish this task, I created a global boolean variable gridCellChanged and I set it to true in CurrentCellDirtyStateChanged event.
private void GvTieredPricing_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
gridCellChanged = true;
}
And in the button event I check
private void btnUpdate_Click_1(object sender, EventArgs e)
{
if (gridCellChanged)
{
//execute my code
}
}
This solution works for most of the time. However, if user changes value in Row 1, Column 1, gridCellChanged will be set to true. However, when user decides to revert the change and set the value to whatever it was the gridCellChanged will still get set to true and code will get executed in a button event.
How can I prevent this?
I have tried using CellValueChanged event. However, every time I load the grid on form load it takes me to that event and always sets my boolean variable to true even though there're has been no change to any cells.
Any suggestions?
When I usually work with checkboxes, I check to see if the box is checked with the code below:
if (checkBox1.Checked)
{
Label1.BackColor = Color.Red;
}
This code is usually attached to a button that sets into motion when it's clicked by the user. This time, however, I want to do something, like change the color of a label, the very SECOND the checkbox is checked by the user. That is, I don't want to wait until the user pushes some other button to check if the checkbox is checked in order for the label's color to change.
How do I do this?
Sounds like you need a CheckedChanged event handler. That one's for ASP.Net, but there's a version for winforms as well (and xaml, and so on.)
Then you have to write code inside CheckedChanged Event of checkBox as below:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
label1.ForeColor = Color.Red;
}
Why don't you hook up to the CheckChanged event and implement your ideas there?
I have a rather complicated form with several interchangeable sub-panel classes, each of which has several user controls of its own. One of the sub-panels has a checkbox that, depending on what options are selected in other sub-panels may not be allowed to uncheck.
Ideally I would handle this by making the checkbox readonly when it's not allowed to uncheck, but that would entail detecting any change that could potentially enable/disable the checkbox, calculating whether it should be enabled, and then telling the sub-panel to enable/disable the checkbox. It seems like it would be easier to detect an attempt to uncheck the checkbox, calculate whether the current state allows unchecking it, and then accept or reject the attempt.
The convention we're using is to attach an event handler to a page's data table's ColumnChanged or ColumnChanging event and then have that handler call appropriate helper methods based on which column changed. I put in the code below:
private void MyTable_ColumnChanging(object sender, DataColumnChangeEventArgs e) {
...
if (e.Column == MyTrueFalseColumn) {
CheckboxChanging(e);
}
...
}
private void CheckboxChanging(DataColumnChangeEventArgs e) {
if (!(bool)e.ProposedValue && MustRemainChecked())
e.ProposedValue = true;
}
I also tried e.Row.SetColumnError(e.Column, "error message"), e.Row.CancelEdit(), and e.Row.RejectChanges() in place of e.ProposedValue = true and confirmed those lines executed, but the checkbox still unchecks without any indication anything happened behind the scenes. I also tried all 4 of those cancellation methods followed by MyCheckBox.DataBindings[0].ReadValue(), again with no success.
I've used breakpoints to step through and confirm that the proposed value is being set to true, but the checkbox still unchecks when the event is complete. What am I doing wrong?
Is the data column properly storing true but the checkbox doesn't update to reflect that? Is there something else I need to do to have the data column store the changed ProposedValue?
I eventually solved this by messing with the checkbox directly in the ColumnChanged event rather than the ColumnChanging event:
private void MyTable_ColumnChanged(object sender, DataColumnChangeEventArgs e) {
if (e.Column == MyTrueFalseColumn)
CheckboxChanged(e);
}
private void CheckboxChanged(DataColumnChangeEventArgs e) {
if (!(bool)e.ProposedValue && MustRemainChecked()) {
MyCheckbox.Checked = true;
MyCheckbox.DataBindings[0].WriteValue();
}
}
If anyone has a solution that only messes with the associated DataTable I'd be happy to accept that.
e.Row.CancelEdit();
This will cancel the proposed value from updating the the current row of data.
I have a ListBox with a "list of servers" that has AutoPostBack enabled and an SelectedIndexChanged event attached to it:
protected void lbServerList_SelectedIndexChanged(object sender, EventArgs e)
{
if ( lbServerList.SelectedValue.ToString() != "")
{
Response.Redirect("detail.aspx?Server=" + lbServerList.SelectedValue.ToString());
}
}
Then I have a textbox to add a "server" with a button "btnServertoAdd" (to execute the addition)
protected void btnServertoAdd_Click(object sender, EventArgs e)
{
Response.Redirect("add.aspx?Server=" + tbServertoAdd.Text);
}
Scenario: If I select an item from the ListBox it will go to detail.aspx showing the server specs: Awesome.
Now, If I click back (browser button) and then type something in the TextBox and click btnServerToAdd it will still go to detail.aspx and not to add.aspx as it should....
How can I fix this?
Let me know if more code is needed.
This is occurring because when you click the button, the selected server is also different from the original value (as stored in the view state). Both events are fired, but evidently the SelectedIndexChanged event is fired first and the Redirect skips the rest of the processing.
I can't think of how to not make the SelectedIndexChanged event fire the second time around, so instead what you could do is, instead of Redirecting in the events themselves:
Have a pair of bool member variables in your page class.
Set one to true in each event handler.
In the page OnLoadComplete event, check each and redirect as necessary:
If both are true, redirect to add.aspx.
If one is true, redirect to the corresponding page.
Otherwise don't redirect at all.
I'm working on a simple WinForms application for a public school where users can identify themselves by entering either their network IDs (which are not protected information) or their system IDs (which are protected information). I want to switch to a password character when the program detects a system ID (which is working just fine); however, when I do this, my application also fires the textbox's Leave event, which tells users to fix a problem with the login data...before there's even a problem.
Here's my code:
void login_TextChanged(object sender, EventArgs e)
{
login.UseSystemPasswordChar = login.Text.StartsWith(<prefix-goes-here>);
}
private void login_Leave(object sender, EventArgs e)
{
if (login.Text.StartsWith(<prefix-goes-here>) && login.Text.Length != 9)
{
signInError.SetError(login, "Your System ID must be nine digits.");
login.BackColor = Color.LightPink;
}
else if (login.Text.IsNullOrWhiteSpace())
{
signInError.SetError(login, "Please enter your username or System ID.");
login.BackColor = Color.LightPink;
}
else
{
signInError.SetError(login, string.Empty);
login.BackColor = Color.White;
}
}
Ultimately, I don't know that this will cause a ton of problems, and I could move this validation step to the Click event of the sign in button on my form, but I'd rather do validation piece-by-piece if possible.
Putting the TextBox inside a GroupBox does reproduce that behavior-- which is odd.
If you want to keep your GroupBox, here is a work around:
private void login_TextChanged(object sender, EventArgs e)
{
login.Leave -= login_Leave;
login.UseSystemPasswordChar = login.Text.StartsWith(<prefix-goes-here>);
login.Leave += login_Leave;
}
For whatever reason, the Leave event fires when the login TextBox is inside a GroupBox control. Replacing the GroupBox with a simple Label control prevented the code within the TextChanged event from firing the Leave event.
Yes, this is a quirk of the UseSystemPasswordChar property. It is a property that must be specified when the native edit control is created (ES_PASSWORD). Changing it requires Winforms to destroy that native control and recreate it. That has side-effects, one of them is that the focus can't stay on the textbox since the window disappears. Windows fires the WM_KILLFOCUS notificaiton.
Being inside a GroupBox is indeed a necessary ingredient, Winforms doesn't suppress the Leave event when it gets the notification. Bug.
Many possible fixes. You could set a flag that the Leave event handler can check to know that it was caused by changing the property.