Disable repositoryItemCheckEdit after it's checked - c#

I have a repositoryItemCheckEdit in a column of my grid. The task I want to do is :
Once the user pressed the CheckEdit , this cell become disable so that the user can not make click again.
To do this task I'm using the CheckedChanged event, in the following way :
private void repositoryItemCheckEdit1_CheckedChanged(object sender, EventArgs e)
{
var obj = sender as CheckEdit;
if (obj.Checked)
{
repositoryItemCheckEdit1.Enabled = false;
}
}
With the above event the only thing I get is that the cell becomes clearer , but not is disabled. Even if I make click again it allows me to do it.
Any help is appreciated.

You will probably have more luck/an easier time dealing with this using the brute force method... at least i find this a lot easier than dealing with the crazyness of DataGridView controls scheme.
Use the Tag attribute of your control to set a flag on it, and then when someone tries to un-check it/change it, force it back to checked. Like so:
private void repositoryItemCheckEdit1_CheckedChanged(object sender, EventArgs e)
{
var obj = sender as CheckEdit;
if(obj.Tag != null)
{
obj.Checked = true;
repositoryItemCheckEdit1.Enabled = false;
}
else
{
if (obj.Checked)
{
obj.Tag = true;
repositoryItemCheckEdit1.Enabled = false;
}
}
}

Related

Circular loop when using selection changed datagrdivew

I am using a datagradview and I am wanting to prompt the user to save the current row before moving off I have been trying to use the following event but I seem to be in a circular loop when I hit my save event.
private void dgStock_SelectionChanged(object sender, EventArgs e)
{
if (isDirty == true)
{
isSavedFromRow = true;
btnSaveDetails_Click(sender, e);
isDirty = false;
}
}
The problem with the selection changed event is this happens once the row has changed so the user could think their saving the new row and not the current row.
I also seem to be caught in a circular loop some how has the messagebox box is getting fired numerious times I am only setting the isDirty to true if the user enters key down on my textboxes.
if (isDirty == true)
{
DialogResult _result = MessageBox.Show("Are you sure you wish to upate Live Product Information", "Save Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (_result == DialogResult.Yes)
{
updateStock();
_trackChanges.Clear();
isDirty = false;
}
}
This is me setting my dirty flag on key down felt this was best way to avoid the problem I seem to be having.
private void txtDescription_KeyDown(object sender, KeyEventArgs e)
{
isDirty = true;
btnSaveDetails.Enabled = true;
}
Your code sample suggests that your save stuff is defined in your save button click. If you change your structure to something like this
private void SaveClick(object sender, EventArgs e)
{
DoSaveStuff();
}
private void DoSaveStuff()
{
// Do your save stuff
}
i.e. pull your save stuff out into a method. You can call DoSaveStuff() whenever you need to save, e.g.
private void SelectionChanged(object sender, EventArgs e)
{
// Do stuff
if (condition)
{
DoSaveStuff();
}
}
The advantage of this approach is you're capturing the aspect of the behaviour you're interested in - the save stuff - rather than the whole button click. Furthermore, you click a button to save, your application doesn't, it simply saves in certain circumstances, e.g. when you click a button or when something changes.
As MSDN says the SelectionChangedEvent occurs whenever the selection has changed. So, whenever this happens you could check your original selection to see if it has changed and then save if it has. Maybe doing something like this
private void SelectionChanged(object sender, EventArgs e)
{
bool hasContentsChanged = // determine if your content has changed
if (hasContentsChanged)
{
DoSaveStuff();
}
}
An advantage of this approach is that you'd only have to save if it really had changed, i.e. if the original text != new text, rather than in all cases.

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...
}

datagridview CellEndEdit fires twice

I am having a bit of an issue with the datagridview's CellEndEdit event. While I understand the concept of what the issue actually is, any attempt at circumventing it seems to fail.
Basically, I have a datagridview, in the CellEndEdit event, I make a check against the database to make sure the entry is not a duplicate. If it is, I prompt the user with a messagebox to tell them they can't enter duplicates, I then change the value back to its original state/value programmatically, and return the cell to an "Edit" state.
My understanding is that the fact that i'm changing the value programatically is why the event fires twice. To circumvent, I set a flag upon first entering the event, then prompt + set + re-edit, then set the flag to false. This does not work... can anyone tell me why or how I can make this happen?
Here's the event code:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if(e.ColumnIndex == this.dataGridView1.Columns["Name"].ColumnIndex)
{
if(!this.CellBeingEdited)
{
string NewName = this.dataGridView1.Rows[e.RowIndex].Cells["Name"].Value.ToString();
//-== DATABASE CODE REMOVED ==-
bool IsDuplicate = ...;
if(IsDuplicate)
{
MessageBox.Show("Cannot have duplicate item names at this level!");
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells["Name"];
this.CellBeingEdited = true;
this.dataGridView1.CurrentCell.Value = this.LastEditedRowName;
this.CellBeingEdited = false;
this.dataGridView1.BeginEdit(false);
return;
}
}
}
}
This bit of code does not fire twice when I edit a value in a row :
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
string test = "test";
if (this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() == test)
{
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[0];
this.dataGridView1.CurrentCell.Value = "not test";
this.dataGridView1.BeginEdit(false);
return;
}
}
Perhaps are you calling the event elsewhere?

C# editable listview

Yesterday I try to implement a new listview that support sub-item edit, my solution is to show a textbox when double click the sub-item. The key code as following:
protected override void OnDoubleClick(EventArgs e)
{
Point pt = this.PointToClient(Cursor.Position);
ListViewItem curItem;
int subItemIndex = GetSubItemAt(pt.X, pt.Y, out curItem);
DoubleClickEventArgs args = new DoubleClickEventArgs(subItemIndex);
base.OnDoubleClick(args);
if (subItemIndex>=0 && !args.Cancel)
{
//StartEdit(...);
}
}
public void EndEdit(bool acceptChanges)
{
//validation
.................
.................
AfterSubItemEventArgs e = new AfterSubItemEventArgs(this.SelectedItems[0], m_editSubItemIndex, this.SelectedItems[0].SubItems[m_editSubItemIndex].Text, m_textbox.Text, false);
OnAfterSubItemEdit(e);
if (e.Cancel)
{
//....
}
else
{
//set new value
}
m_textbox.Visible = false;
m_editSubItemIndex = -1;
}
OnAfterSubItemEdit is a event that user can do some validations or other operations. I add a check in this method, if the new value exist, I will show a messagebox to user firstly, then hide the textbox. But now, the problem comes, when i move the mouse, the listview items can be selected, I don't how to solve this issue, I tried my best to find out the way, but failed. So, please help me!
Listview has a LabelEdit property; when you set it "true", then in an event handler you can call Listview.Items[x].BeginEdit(), and edit an item. As an example, you can handle ListView.DoubleClick event and call BeginEdit right there:
private void Form1_Load(object sender, System.EventArgs e)
{
listView1.LabelEdit = true;
}
private void listView1_DoubleClick(object sender, System.EventArgs e)
{
if(this.listView1.SelectedItems.Count==1)
{
this.listView1.SelectedItems[0].BeginEdit();
}
}
The problem is that your form still calls the DoubleClick event whether the value exists or not. Add appropriate condition before calling base DoubleClick in your code, i.e.:
if(!new value exists)
base.OnDoubleClick(args);

In the WinForms DevExpress XtraWizard control, how can one hide the back button?

I know that if I set the WizardStyle of an XtraWizard control to WizardAero, it will remove or hide the back button from the first page instead of simply disabling it, as can be seen here. I want the same behaviour, but I want to keep the WizardStyle as Wizard97.
Is this possible, and if so, how?
One way to do this would be to use the CustomizeCommandButtons event on the WizardControl.
private void wizardControl1_CustomizeCommandButtons(object sender, CustomizeCommandButtonsEventArgs e)
{
e.PrevButton.Visible = false;
}
If you only want to hide it on the first page
private void wizardControl1_CustomizeCommandButtons(object sender, CustomizeCommandButtonsEventArgs e)
{
if(wizardControl1.SelectedPageIndex == 0)
e.PrevButton.Visible = false;
}
It seems like it will reset the visibility each time (so you don't need to toggle it back on). Anyway, I think this is what you're looking for.
http://documentation.devexpress.com/#WindowsForms/DevExpressXtraWizardWizardControl_CustomizeCommandButtonstopic
You can also set your own fields to the wizard buttons and then use these anywhere in your code. This will, for example, allow you to hide/disable the "Next" button until all fields page have been completed.
private void NodeConfigurationWizardCustomizeCommandButtons(object sender, CustomizeCommandButtonsEventArgs e)
{
_nextButton = e.NextButton;}
private void GetRowsButtonClick(object sender, EventArgs e)
{
var rowList = ServiceClient.GetAvailableRows();
var rowsReturned = rowList.Count > 0;
_nextButton.Button.Enabled = rowsReturned ;}

Categories