When a winform first displays, the checkbox is unchecked by default. If when the form first displays, I click on the checkbox to 'check' it, the checkbox appears checked for a split second and then disappears. The checkedchanged event never fires. However, if anytime after the first initial attempt I click on the checkbox, the value changes (checked to unchecked and vice versa) like it should and the event fires.
Any idea why the checkbox would not check on the first attempt? It appears selected the first time when you hover over it so I know it has focus.
Update: it doesn't matter if you enter data into all other controls first and then click on the checkbox, the first time you click on it, it flashes as checked for a second, and then the check disappears. Anytime after the 1st time though it works. Strange...
Hard to tell without seeing a code snippet. When I've had things like this in the past, it's been due to having duplicate control ids, or wiring up event handlers incorrectly. Have you tried disabling portions of your code and seeing what affects the checkbox behaviour?
Strangely, putting code in the CheckedChanged() to set the value (what it gets set to anyway if I trace through it) seems to work:
if (this.chkbox1.Checked == true)
{
this.chkbox1.Value = "1";
this.chkbox1.Text = "Checked";
}
else
{
this.chkbox1.Value = "0";
this.chkbox1.Text = "Un-checked";
}
I also put a focus() in the click():
if (((System.Windows.Forms.MouseEventArgs)(e)).Clicks <= 1)
{
if (this.chkbox1.Focused == false)
{
this.chkbox1.Focus();
}
}
I have no idea why that fixes the problem, but it does.
do this happen with just one CB? or all the CB on the form.
Have you tried deleting the CB then adding it back?
I would suggest you post the code behind the CB?
For your custom code, I would try using different cast styles, to see if it makes any difference.
If you're using the standard C# syntax, and it fails (vanishing checkbox)
CheckBox checkBox = sender as CheckBox;
I would try using the old-style cast on the sender object and see if it gives you the desired result:
CheckBox checkBox = (CheckBox)sender;
This may give you a hint on the root cause.
Related
I have a combo box which I need to mirror in another tab page in a C# winforms based application.
I have perfectly working code for when you select a different item from the drop down list. Unfortunately, however, when I change the Text of a tab that has not been clicked on yet nothing actually happens.
If I first click each tab then everything works as expected.
Now I'm putting this down to some form of lack of initialisation happening first. So I've tried to select each tab in my constructor.
tabControlDataSource.SelectedIndex = 0;
tabControlDataSource.SelectedIndex = 1;
// etc
But this doesn't work.
I've also tried calling tabControlDataSource.SelectTab( 1 ) and still it doesn't work.
Does anyone know how I can force the tab to "initialise"?
Ok, typically I post the question after struggling for an hour and shortly afterwards find the solution.
TabPages are lazily initialised. So they don't fully initialise until they are made visible for the first time.
So i added this code to my constructor:
tabControlDataSource.TabPages[0].Show();
tabControlDataSource.TabPages[1].Show();
tabControlDataSource.TabPages[2].Show();
but this didn't work :(
It occurred to me, however, that the constructor might not be the best place. So I created an event handler for Shown as follows:
private void MainForm_Shown( object sender, EventArgs e )
{
tabControlDataSource.TabPages[0].Show();
tabControlDataSource.TabPages[1].Show();
tabControlDataSource.TabPages[2].Show();
}
And now everything is working!
Perhaps you could also use sort of a "lazy" synchronization (initialization) in this case. Quick robust ideas: polling timer to update content (which will update it once you see tab page), no dependses within second tab (no Changed events for combobox to update second tab content, use original combobox from first tab or rather have it's content underlying in accessable for both comboboxes class, etc), "reinitialization" when tab become visible (at which moment you also init your second combobox)...
Can't be a hour, no way =D
I'm currently working on a homework assignment where I need to set up a calculator-type program. It needs to read one or two user-input values (depending on the calculation), and then perform the calculation based on the values.
I currently have
2 textBoxes (tbInput1 and tbInput2),
4 radioButtons,
one button (btnCalc)
a blank label in which the result will be displayed.
Two of the radioButtons (rbtnTrap and rbtnFak) disable the first textBox when checked; the other two need two values to be entered, and for that reason enable both textBoxes when checked. btnCalc is supposed to enable itself when the relevant number of textBoxes have value - the relevant number of textBoxes depends on which radio button is checked.
The problem is that when I check rbtnTrap or rbtnFak (disabling tbInput1) and enter an integer in tbInput2, btnCalc stays disabled.
I'll try to explain what I have so far:
In the _TextChanged event for tbInput1, I have an exact copy of the second if block posted below. tbInput1 is only active when rbtnPot OR rbtnFib are checked, so that control only runs when that is the case.
In the _TextChanged event for tbInput2, I have the below, since tbInput2 is always enabled, and the control must run no matter which radio button is checked, although the control should run differently if i check rbtnTrap OR rbtnFak as opposed to rbtnPot OR rbtnFib.
Or that's my understanding of it. I'm certainly open to suggestions and corrections.
private void tbInput2_TextChanged(object sender, EventArgs e)
{
//For single-field values
if ((rbtnTrap.Checked || rbtnFak.Checked) &&
!string.IsNullOrWhiteSpace(this.tbInput2.Text))
{
btnCalc.Enabled = true;
}
else
{
btnCalc.Enabled = false;
}
// For multi-field values
if ((rbtnPot.Checked || rbtnFib.Checked) &&
(!string.IsNullOrWhiteSpace(this.tbInput1.Text)
&& !string.IsNullOrWhiteSpace(this.tbInput2.Text)))
{
btnCalc.Enabled = true;
}
else
{
btnCalc.Enabled = false;
}
}
Because it is a homework, I am not going to give your the straight answer, but instead a hint: the problem is in the code of the tbInput2_TextChanged event (code that you posted).
When rbtnTrap or rbtnFak are checked, your code will be run following such a path you don't think it will. Set a breakpoint (F9) on the first line of the tbInput2_TextChanged code and run the code step by step (F10) after entering some text in tbInput2.
You will see why your button btnCalc is enabled as you think it is, but disabled in the next moment.
Feel free to comment if you need more help afterwards. :)
EDIT
The problem comes from your if blocks. When one of rbtnTrap and rbtnFak is checked:
your code will run in the if clause of the first block and then do btnCalc.Enabled = true; but...
it will also run in the else clause of the second block (because (rbtnPot.Checked || rbtnFib.Checked) is false) and thus do btnCalc.Enabled = false;.
I'm not sure how much I understand your problem (I'm sorry, the explanation was a bit all over the place), but one thing that I do see, is that you're only implementing your tests in one event when you have two controls to check.
It sounds like a simple problem, but I really can't understand what is implemented where and who is supposed to do what, so if my 2 cents above were useless and you can explain the question again, that'll probably help in finding a solution
I'm trying to build a Listview EDIT/Insert template where I can use a checkbox to enable updating multiple database tables but to little success.. I managed to get the insert working by performing some foul sorcery on the Listview inserting event. But I'd prefer that it works with the Checkbox OnCheckedChanged event as it feels abit more kosher in my mind, and of course the added benefit on it working for the edittemplate..
protected void checktest_clicked(object sender, EventArgs e)
{
//testlabel.Text = testcheck.Checked.ToString(); <-- exists outside of LW
// so it works
//Label hejha = (Label)lwRapport.FindControl("testlabel");
CheckBox trial = (CheckBox)lwRapport.FindControl("upParameter");
if(trial != null)
{
if(trial.Checked == true)
{ testlabel.Text = "finally"; }
if(trial.Checked == false)
{ testlabel.Text = "Nope, not going to happen"; }
}
if (trial == null)
{ testlabel.Text = "not wanted"; }
}
That's my test snippet for checking how the FindControl works and so far I've been quite unsuccessful making it do what I want it to do..
Any Correction on faults / hack / workaround for this matter would be apritiated
EDIT1*
The checkbox is inside of the listview, more precisely in the inserttemplate. The template looks something on the lines like this:
textbox <bind"table1.element">
textbox2 <bind"table1.element2">
checkbox [_]
textbox3 <bind"table2.element">
Observe that the snippet above is just a pseudocode snippet of my layout not the acctual layout. What I'm attempting is to find the checkbox and bind it's checked value to a parameter which passes a couple of checks in the SPROC then executes the UPDATE command
You seem to be not able to find the check box control from the list view. This is because you are searching for the check box inside the list view, and what you should be doing is searching for it inside the selected item.
You can have a look at this. Although it's GridView, I think it will works too.
The WinForms CheckedListBox control has 2 default behaviors when clicking with a mouse:
In order to check/uncheck an item you're required to click an item twice. The first click selects the item, and the second toggles the check state.
In addition, one subsequent click of the same item will toggle that item's checked state.
As a convenience feature I needed to allow users to toggle the selection in one click. I have achieved this, so now default behavior #1 above is achieved in one click. The problem is behavior #2 no longer works correctly when clicking the same (i.e., currently selected) item. It works fine when jumping between items, which is desired, but it requires up to 4 clicks on the same item.
My workaround for this is to call the toggling logic twice if the user selects the same item repeatedly. So on to my questions:
This works, but why? What's the real underlying issue?
Is there a better way to achieve this so I can get it working like default behavior #2 without calling the method twice and keeping track of my last selection?
Oddly enough debugging the code reveals that the checked state has changed but it doesn't appear on the UI side till it's called twice. I thought it might be threading related but it's not a re-entrant event being triggered that might need BeginInvoke usage.
Here's my code:
using System.Linq;
using System.Windows.Forms;
namespace ToggleCheckedListBoxSelection
{
public partial class Form1 : Form
{
// default value of -1 since first item index is always 0
private int lastIndex = -1;
public Form1()
{
InitializeComponent();
CheckedListBox clb = new CheckedListBox();
clb.Items.AddRange(Enumerable.Range(1, 10).Cast<object>().ToArray());
clb.MouseClick += clb_MouseClick;
this.Controls.Add(clb);
}
private void clb_MouseClick(object sender, MouseEventArgs e)
{
var clb = (CheckedListBox)sender;
Toggle(clb);
// call toggle method again if user is trying to toggle the same item they were last on
// this solves the issue where calling it once leaves it unchecked
// comment these 2 lines out to reproduce issue (use a single click, not a double click)
if (lastIndex == clb.SelectedIndex)
Toggle(clb);
lastIndex = clb.SelectedIndex;
}
private void Toggle(CheckedListBox clb)
{
clb.SetItemChecked(clb.SelectedIndex, !clb.GetItemChecked(clb.SelectedIndex));
}
}
}
To reproduce my problem comment out the lines mentioned in the code comments and follow these steps:
Click the item at index 2 - state changes to checked.
With the current item selected, click it again - state does not change. Expected: unchecked. Click it a few times and it finally switches.
Thanks for reading!
As a convenience feature I needed to allow users to toggle the selection in one click.
I'm not sure what's happening with the code, but setting CheckOnClick to true will do this:
CheckOnClick indicates whether the check box should be toggled whenever an item is selected. The default behavior is to change the selection on the first click, and then have the user click again to apply the check mark. In some instances, however, you might prefer have the item checked as soon as it is clicked.
I'm having a pretty nasty problem with an auto-completing textbox. I want to initiate an asynchronous PostBack whenever a user selects an item from the auto-completed field and retain the value of the item, rather than the text inputted. This works perfectly when enter is pressed rather than a mouse click.
An example of my issue:
Someone goes to the page, and types 1000 into the textbox. The autocomplete displays 10002, 1000B, and 10000. The user clicks on 1000B and an asynchronous PostBack initiates. Instead of 1000B, the TextBox.Text value is still 1000.
My assumption is that the textbox is initiating the PostBack before the value is actually getting assigned to it. I'm just curious if anyone has any possible solutions for what I'm talking about.
I fixed it in this manner:
As per another question on the site I added an autoPostBack parameter to the options list.
In bottom of the SelectCurrent() function, I added these lines.
if (options.autoPostBackSelection == true) {
__doPostBack($input.id, "");
}
Then, my blur function looked like this:
.blur(function() {
hasFocus = 0;
if (!config.mouseDownOnSelect) {
hideResults();
}
if (options.autoPostBackSelection == true) {
selectCurrent();
}
I actually struggled with this for a bit, my Javascript/DOM event skills aren't very good. Hopefully this helps someone.