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...
Related
I have a Windows Form Application, where the User can input numbers into three different TextBoxes. I want to save these numbers by checking the Checkbox next to it, so when the Application gets closed and re-opened you don't have to put in the numbers again.
I have added the Properties to the User Settings and implemented the Code below, but when I input a number and re-open the Application, nothing is shown and they aren't saved in the user.config file.
Any help is greatly appreciated as I can't find my mistake.
private void MainForm_Load(object sender, EventArgs e)
{
Text = Properties.Settings.Default.title;
chkBox1.Checked = Properties.Settings.Default.checkBox;
chkBox2.Checked = Properties.Settings.Default.checkBox;
chkBox3.Checked = Properties.Settings.Default.checkBox;
txtBox1.Text = Properties.Settings.Default.textBox;
txtBox2.Text = Properties.Settings.Default.textBox;
txtBox3.Text = Properties.Settings.Default.textBox;
this.Location = new System.Drawing.Point(Properties.Settings.Default.PX, Properties.Settings.Default.PY);
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.checkBox = chkBox1.Checked;
Properties.Settings.Default.checkBox = chkBox2.Checked;
Properties.Settings.Default.checkBox = chkBox3.Checked;
Properties.Settings.Default.textBox = txtBox1.Text;
Properties.Settings.Default.textBox = txtBox2.Text;
Properties.Settings.Default.textBox = txtBox3.Text;
Properties.Settings.Default.PX = this.Location.X;
Properties.Settings.Default.PY = this.Location.Y;
Properties.Settings.Default.Save();
}
private void chkBox1_Checked(object sender, EventArgs e)
{
this.Text = txtBox1.Text;
}
private void chkBox2_Checked(object sender, EventArgs e)
{
this.Text = txtBox2.Text;
}
private void chkBox3_Checked(object sender, EventArgs e)
{
this.Text = txtBox3.Text;
}
Why not use databinding to save changes automatically. You don't need to replicate the code on form_load and form_closing events.
The best explanation I have for control data binds is that they provide two way model update between a control properties and object properties.
More Info https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.databindings?view=netcore-3.1
private void Form1_Load(object sender, EventArgs e)
{
chkBox1.DataBindings.Add("Checked", Properties.Settings.Default, "Checked1",true, DataSourceUpdateMode.OnPropertyChanged);
chkBox2.DataBindings.Add("Checked", Properties.Settings.Default, "Checked2",true, DataSourceUpdateMode.OnPropertyChanged);
chkBox3.DataBindings.Add("Checked", Properties.Settings.Default, "Checked3",true, DataSourceUpdateMode.OnPropertyChanged);
//you can others
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//don't forget to call save on form closing
Properties.Settings.Default.Save();
}
The first part of my answer, regarding to the fact that nothing is saved when you close your application, is based on the assumption that when testing, you leave the third textbox empty
Why is nothing saved
First is why you are seeing nothing when opening your application, leading you to believe nothing was saved when closing it.
You are in the part of your code handling what happens when your application is closing, saving all of the textboxes (and checkboxes states) in the same setting
Which leads to the following
txtBox1 contains a
txtbox2 contains nothing (or an empty string if you prefer)
When saving, what is happening with your code is that in a first step, you are putting "a" into your textbox setting.
Then, you are replacing this vlue with the content of the second textbox, which is empty
(repeat for the third textbox)
The you are saving.... An empty value.
If you wish to fix this in a "naive" way, you would need a setting per textbox and checkbox.
Which would lead to code ressembling this in your Closing event handler
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.checkBox1 = chkBox1.Checked;
Properties.Settings.Default.checkBox2 = chkBox2.Checked;
Properties.Settings.Default.checkBox3 = chkBox3.Checked;
Properties.Settings.Default.textBox1 = txtBox1.Text;
Properties.Settings.Default.textBox2 = txtBox2.Text;
Properties.Settings.Default.textBox3 = txtBox3.Text;
Properties.Settings.Default.PX = this.Location.X;
Properties.Settings.Default.PY = this.Location.Y;
Properties.Settings.Default.Save();
}
Why do I say "naive", because as you've surely understood, this approach is not sustainable for a huge number of controls, but this is not the scope of the question, I'll let you research a solution on your own for this particular point.
Why are the checkbox doing nothing to determine what is saved
First, with the events available on Winforms (at least with the .NET Framework 4.5 which I used to reproduce what you had) the only events available to be notified of the checkbox state change are :
CheckedChanged
CheckStateChanged
The first is used on a binary Checkbox (checked or not)
The second on a checkbox with an uncertain state added to both of the other states.
I imagine you used the first of the two (because that is the one used by default by Visual Studio when double clicking on it in the designer).
The first issue here is that it notifiesyou that the state changed not only that it went from unchecked to checked, but the other way around too.
That means if you only want an action to be done when checking, you need to add a.... check (an if block) to skip the cases you're not interest into.
Next is the actual saving.
What you are doing in your code is just copying the textbox values in a property in your class, and that will NOT persist after closing the application.
Now there is two approach you could use to save those values into the settings, the first is to do it as soon as you check the boxes.
What you would need to do then is for each event handler to copy the value of the textbox.... directly into the settings
An example for the first textbox and checkbox :
private void chkBox1_Checked(object sender, EventArgs e)
{
if(chkBox1.Checked)
{
Properties.Settings.Default.checkBox1 = chkBox1.Checked;
}
}
I'm not a huge fan though, and would prefer the second solution => to check in the closing event, before copying the value from the textbox into the settings, if the corresponding checkbox is closed.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.chkBox1.Checked)
{
Properties.Settings.Default.textBox = txtBox1.Text;
}
[...]
}
Now that a little better, and should be working as intended.
Please note that this answer is oriented towards correcting the problem whilst using solutions that are the closest possible of your original code.
I need to make it so when the user clicks on a cell with TextEdit in a grid view, it will select all in the textedit. I tried many many ways i could find in the internet, but none of them work well.
"EditorShowMode = MouseUp" way breaks everything, for example when you click on a cell that has checkedit; it selects the cell, then you need o click again to actually click on the CheckEdit.
"Use EditorShowMode = MouseUp and manually handle other things on MouseDown" is just ew. Won't work fine for all types of controls.
"Change selection length etc. on ShownEditor event" way doesn't work too, actually it selects the text when clicked, but it doesn't override the default function so the selection instantly changes. Also tried the SelectAll method but it had some problems that i dont remember (probably didnt work at all).
I have really tried many things, but couldn't find a totally fine way. Please tell me if you can get a working way without breaking other types of controls in the grid.
Answered by Pavel on DevExpress Support (works great):
The easiest way to achieve this is to use the GridView.ShownEditor event to subscribe to the active editor's MouseUp event. Then, select all text in the MouseUp event handler and detach this handler to avoid subsequent text selection.
private void GridView_ShownEditor(object sender, EventArgs e)
{
GridView view = sender as GridView;
if (view.ActiveEditor is TextEdit)
view.ActiveEditor.MouseUp += ActiveEditor_MouseUp;
}
private void ActiveEditor_MouseUp(object sender, MouseEventArgs e)
{
BaseEdit edit = sender as BaseEdit;
edit.MouseUp -= ActiveEditor_MouseUp;
edit.SelectAll();
}
You could use GridView CustomRowCellEdit event and set an event of text editor such as Mouse Up. Setting the RepositoryItemTextEdit MouseUp event can be set as in the example.
Example:
private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
if (e.RepositoryItem is DevExpress.XtraEditors.Repository.RepositoryItemTextEdit)
{
DevExpress.XtraEditors.Repository.RepositoryItemTextEdit rep = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();
rep.ReadOnly = false;
rep.MouseUp += rep_MouseUp;
e.RepositoryItem = rep;
}
}
void rep_MouseUp(object sender, MouseEventArgs e)
{
DevExpress.XtraEditors.TextEdit te = sender as DevExpress.XtraEditors.TextEdit;
te.SelectAll();
}
You should handle Enter event for TextEdit
private void myRepositoryItemTextEdit_Enter(object sender, EventArgs e)
{
var editor = (DevExpress.XtraEditors.TextEdit)sender;
BeginInvoke(new MethodInvoker(() =>
{
editor.SelectionStart = 0;
editor.SelectionLength = editor.Text.Length;
}
}
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.
I'm creating listviews in a flowpanel at run time which later will accept drag and dropped files. the reason being is i want these to act as folders so a user double clicks and gets a window displaying the contents.
i'm having difficulty setting up the events for my listviews as they are added.
how do i create some events (like MouseDoubleClick and DragDrop) dynamically for each added listview? can i create a single function for both of these events and have listview1, listview2, listviewX use it?
i have a button that is adding the listviews, which works fine. please advise, i apologize if this is too conceptual and not exact enough.
private void addNewWOButton_Click(object sender, EventArgs e)
{
ListView newListView = new ListView();
newListView.AllowDrop = true;
flowPanel.Controls.Add(newListView);
}
You would have to have the routine already created in your code:
private void listView_DragDrop(object sender, DragEventArgs e) {
// do stuff
}
private void listView_DragEnter(object sender, DragEventArgs e) {
// do stuff
}
and then in your routine, your wire it up:
private void addNewWOButton_Click(object sender, EventArgs e)
{
ListView newListView = new ListView();
newListView.AllowDrop = true;
newListView.DragDrop += listView_DragDrop;
newListView.DragEnter += listView_DragEnter;
flowPanel.Controls.Add(newListView);
}
You would have to check who the "sender" is if you need to know which ListView control is firing the event.
You can also just use a lambda function for simple things:
newListView.DragEnter += (s, de) => de.Effect = DragDropEffects.Copy;
Just make sure to unwire the events with -= if you also remove the ListViews dynamically.
To answer the other half of your question, you can use a single handler for any event, from any source, that has the handler's signature. In the body of the handler, you just have to check the sender argument to determine which control raised the event.
You need a way to tell one control from a different one of the same class, however. One way to do this is to make sure to set the Name property on each control when you create it; e.g., newListView.Name = "FilesListView".
Then, before you do anything else in your event handler, check the sender.
private void listView_DragDrop(object sender, DragEventArgs e) {
ListView sendingListView = sender as ListView;
if(sendingListView == null) {
// Sender wasn't a ListView. (But bear in mind it could be any class of
// control that you've wired to this handler, so check those classes if
// need be.)
return;
}
switch(sendingListView.Name) {
case "FilesListView":
// do stuff for a dropped file
break;
case "TextListView":
// do stuff for dropped text
break;
.....
}
}
I want to minimize the amount of code i have to write for this small problem. I have 1 textbox that has a relationship with 2 checkboxes as yes and no. The textbox on form load is set to disabled. When the yes checkbox is changed this event occurs -
private void checkYes1_CheckedChanged(object sender, EventArgs e)
{
textBox14.Enabled = true;
checkNo1_cbx.Checked = false;
}
and when the no checkbox is changed -
private void checkNo1_cbx_CheckedChanged(object sender, EventArgs e)
{
textBox14.Enabled = false;
checkYes1_cbx.Checked = false;
}
Although another problem is that i have to press yes twice to get it to check.
This is for a question on a form and so far it goes up to 11 questions and more will be added in the future. So my 2 problems so far is -
How can I fix the problem when the checkbox is changed I have to press it again to check it.
Is it possible to improve this code to minimize the amount of code i will have to write in the future.
private void checkYes1_CheckedChanged(object sender, EventArgs e)
{
OnCheck(true);
}
private void checkNo1_cbx_CheckedChanged(object sender, EventArgs e)
{
OnCheck(false);
}
private void OnCheck(bool yes)
{
textBox14.Enabled = yes;
checkNo1_cbx.CheckedChanged -= checkNo1_cbx_CheckedChanged;
checkNo2_cbx.CheckedChanged -= checkYes1_CheckedChanged;
checkNo1_cbx.Checked = !yes;
checkNo2_cbx.Checked = yes;
checkNo1_cbx.CheckedChanged += checkNo1_cbx_CheckedChanged;
checkNo2_cbx.CheckedChanged += checkYes1_CheckedChanged;
}
However consider using RadioBox instead of CheckBox because you want to if one being checked uncheck the other..
Edit: In your previous design, you get it wrong changed I have to press it again to check it because of you have two event handlers assigned to each of the check boxes. now at your code when the first one checked, you are disable the text box and make the other unchecked, but when you call the other unchecked Checked = false you are calling the second check box event handler also so it will enable the text and make the first one disable... you should remove the event handler by -= when updating at your code if you don't want the handler handler to be triggered again.. And that what I am doing in the code sample provided.
Why are you using 2 checkoboxes ? One checkbox (check1) would be enough:
private void check1_CheckedChanged(object sender, EventArgs e)
{
textBox14.Enabled = check1.Checked;
}
EDIT:
Assuming that each question mean 1 textbox, then you need 1 checkbox per textbox... this could be further improved by using a more complex approach
unless there is a reason that you are making a round trip back and forth to the server to disabled a textbox on a checkbox selection change, why don't you just do that all on the client side via javascript?
I agree with Yahia. If you do need to explicitly provide the two options though, then you should consider using RadioButtons.