Check Box changes - c#

I have a check box, which when it is checked, fires a call to a web service.
This works fine as it is using a toggle function in a database which is updating as expected.
However my problem being I need the toggle function to be activated when a user unchecks the checkbox.
For some reason this does not seem to fire the toggle function in the database. I am using the following code -
private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
if (checkCounter1 == 0)
{
}
else
{
//WebService call
}
checkCounter1 = 1;
}
I tried the checkBox_Changed event however this did not work. How can I do this?

Since SL is more or less WPF. That's why i think in SL just like WPF there should be Checked and UnChecked event.
Assign Single event code to both these events (Since both take same arguments) like this
private void checkBox1_Checked_Unchecked(object sender, RoutedEventArgs e)
{
if (checkBox1.IsChecked)
{
//WebService call
}
else
{
}
}

Try the CheckBox1.IsChecked or CheckBox1.Checked property of the checkbox and see if they are checked in the event functions.

Related

Button isn't disabling after I click it

I have created a button that resets a textbox value to the default value, as shown:
<Button x:Name="DefaultButton"
Grid.Row="0"
Grid.Column="3"
Click="OnDefaultClicked">
Here is the Click method:
private void OnDefaultClicked(object sender, RoutedEventArgs e)
{
DefaultButton.IsEnabled = false;
displayedData = defaultData;
//rest of method code
}
When I click the button, the data resets to its default value automatically, but the button does not get disabled until I click it a second time. I am not sure why this is happening.
I could implement the IsEnabled property in the xaml code and bind it to a method that determines whether the button should be enabled based on the value of displayedData, but since the button is not re-enabled/disabled anywhere else in my application or used for any other purpose, this seems kind of like overkill... as far as I know, the Click event should be able to handle this alone. Regardless, my main problem is that I just don't understand why the button wouldn't get disabled until the 2nd click since the OnDefaultClicked method explicitly states it should be disabled when clicked.
Am I missing something?
Turns out the problem was in the rest of my code. The snippet posted above worked perfectly fine. I realized I had created a method that re-enables the button when the text in the box changes, to allow the user to again reset it to default after making a change. So when the default button is clicked, the text in the box changes and therefore both the OnTextChanged and OnDefaultClicked methods are triggered, which causes simultaneous enabling and disabling of the button.
Here's how I fixed it:
private bool DefaultClicked;
private void OnDefaultClicked(object sender, RoutedEventArgs e)
{
DefaultButton.IsEnabled = false;
DefaultClicked = true;
displayedData = defaultData;
//rest of method code
}
private void OnTextChanged(object sender, RoutedEventArgs e)
{
if(!DefaultClicked)
{
DefaultButton.IsEnabled = true;
}
DefaultClicked = false;
//rest of method code
}

Winforms C# Disable/ Enable Button on Treenode Click

I have a treenode which displays a checklist from a SQL database. I have a method to get the selected workflows.
I want to enable the run button if a checkbox is checked and disable the button if nothing is checked and on load.
I'm not sure where to put this if statement. I have tried putting it under the run button on the click action but it is not working correctly.
Any help is appreciated.
List<WorkflowViewModel> workflowViewList = new List<WorkflowViewModel();
var workflowList = GetSelectedWrokflows();
if (workflowList.Count == 0)
{
button.enabled = false;
}
else
{
button.enabled = true;
}
One way to do this is to create a method that will do the work of determining the selected workflow items and enabling or disabling the button. By putting the code in a single method, it allows you to call it from multiple places, and if you need to change the behavior, you only have one place to make the modifications.
Then you can just call this method from the Form_Load event, and from the checked list box's ItemCheck event:
public partial class Form1 : Form
{
List<WorkflowViewModel> workflowViewList = new List<WorkflowViewModel>();
private void SetRunButtonState()
{
workflowViewList = GetSelectedWorkflows();
button.Enabled = workflowViewList.Count > 0;
}
private void Form1_Load(object sender, EventArgs e)
{
SetRunButtonState();
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
SetRunButtonState();
}
// Rest of class code omitted...
}

How to react to a onclick in an ultragrid

I have code that creates an UltraGrid with a column that has the ColumnStyle.Button as it's style.
private void Grid_InitializeLayout(object sender, InitializeLayoutEventArgs e){
Grid.ResetEmptySelectedAppearance();
var column = Grid.SetColumn("Draw Lines", "Draw Lines", 30);
column.Style = ColumnStyle.Button;
Grid.HideOtherColumns();
}
Now I would like too make it react to being clicked on. I have found this but it does not show me how to bind the variable, usually I can either double click in the visual studio editor (but in this case this only directs me too the Grid_InitializeLayout) or I can go the the item in question and add the function to an OnClick variable, but this one doesn't exist.
private void Grid_InitializeRow(object sender, InitializeRowEventArgs e)
var buttoncell= e.Row.Cells["Draw Lines"];
//something here?
this is what I want to call
private void OnDrawLine(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs e)
{
Debug.Print("test test");
}
It's probably something trivial but I'm stuck.
The UltraGrid responds to clicks on a cell button as explained in the link posted using an event called ClickCellButton. You need to subscribe to this grid event in the usual ways. (Designer or code doesn't matter)
private void Grid_ClickCellButton(object sender, ClickCellEventArgs e)
{
// Check if the click happens on the required column
if (e.Cell.Column.Key == "Draw Lines")
{
... your code ...
}
}

ToolStripCombobox.SelectionChangeCommitted not found

I have two ToolStripCombobox controls, each with SelectedIndexChanged listeners attached.
I'm facing a problem when I modify the item collection programmatically. I end up triggering the SelectedIndexChanged unwillingly.
When searching online for a solution I found OnSelectionChangeCommitted and the corrensponding event, but Visual studio says:
'System.Windows.Forms.ToolStripComboBox.OnSelectionChangeCommitted(System.EventArgs)' is inaccessible due to its protection level.
If it is impossible to make use of SelectionChangeCommitted, what other ways are there to avoid triggering events when manually updating ToolStripComboBox items?
Im using .Net 4.0 and the ToolStripComboBox is configured with DropDownStyle = DropDownList.
You can access it from underlying ComboBox.
toolStripComboBoxExample.ComboBox.SelectionChangeCommitted += ComboBoxOnSelectionChangeCommitted;
private void ComboBoxOnSelectionChangeCommitted(object o, EventArgs eventArgs)
{
\\Your code goes here.
}
You can do your stuff inside SelectedIndexChanged event itself. By declaring a global bool variable and checking it from SelectedIndexChanged event to verify the type of trigger, you can achieve this. That is something as like,
bool isManualFire = true;
private void Form1_Load(object sender, EventArgs e)
{
//Clear isManualFire flag in case of programatical changes
isManualFire = false;
//Do programatic changes on toolStripComboBox1
//Set it back to get manual triggerings
isManualFire = true;
}
private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (isManualFire)
{
//DO some operations
}
}
Hope this helps...

C# checkbox and mouse click event

I am self teaching myself C# and ran into a problem I haven't seem to find an answer too. I have a Form that when I mouse click the check box the state goes to true but also immediately triggers the mouse click event I have code follows:
private void uxCheckBoxMouseClick(object sender, MouseEventArgs e)
{
//MouseEventArgs me = (MouseEventArgs) e;
if (uxMouseCopyCheckBox.Checked)
{
MessageBox.Show("Test");
uxMouseCopyCheckBox.Checked = false;
}
}
I have searched the stack overflow and Google and found similar items but not in C# but no luck on the fixed solution. What I want to do it use the first click to change the check box to true without triggering the mouse click event. I want to delay the event to the 2nd mouse click and not the first.
I have tried the following:
for loop
Clicks == 2 with if statement
subscribing but at a loss on what to use
Instead of the Click event you could subsribe to the CheckedChanged event :
The Handler will look look exactly like yours :
private void uxMouseCopyCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (!uxMouseCopyCheckBox.Checked)
{
MessageBox.Show("Test");
uxMouseCopyCheckBox.Checked = false;
}
}
The only difference is that we want the Message box to be shwon only on the second click so when you will uncheck the checkbox.
Be careful though, if you change the default state of the checkbox, it will no longer work.
If you want a really robust solution Grant's one is IMHO the best, mine was just here to show you how to adapt your code for it to work
Just use a boolean variable as a flag.
private bool wasAlreadyClickedOnce;
private void uxCheckBoxMouseClick(object sender, MouseEventArgs e)
{
if (!wasAlreadyClickedOnce)
{
wasAlreadyClickedOnce = true;
return;
}
if (uxMouseCopyCheckBox.Checked)
{
MessageBox.Show("Test");
uxMouseCopyCheckBox.Checked = false;
}
}
Try using the Click event instead of CheckedChanged event to check or uncheck the CheckBox and then you can use the MouseClick event for other stuff.

Categories