I have a windows form with a panel on the left, which consists purely of radiobuttons, and a tabcontrol in the middle, with multiple tab pages within it. Each of these individual tabpages have a series of datagridviews within it, which are shown and hidden depending on which radio button you check.
I accomplish this effect by having each of the radiobuttons on the left assigned a CheckChanged event, which loops through all of the controls within the tabpagecontrol.SelectedTab, and calls .Show() on the corresponding datagridview and calls .Hide() on the rest so that only one datagridview is visible at one time.
My problem occurs when i try to programmatically check one of these RadioButtons. Lets say in Method X, I write RadioButtonA.checked = true. This triggers the usual CheckedChange event handling, which loops through all the datagridviews on the currently selected tabpage and calls .Hide() on everything except the one datagridview form that the radiobutton is supposed to bring up and calls .Show() instead. However, on one of these .Hide() calls on the datagridview, it ends up triggering the RadioButtonA.CheckedChange event AGAIN for a second time. When i look at the sender argument passed to the function, it shows that the sender is the RadioButton i just programmatically clicked on.
I am adding these datagridviews programmatically and can confirm that there are no eventhandlers assigned whatsoever to them. Can anyone help me determine what is causing this additional event to get triggered? Thanks.
For obnoxious change events that trickle through and upset other event handlers on my forms, I've found the only solution is to add a small boolean value:
bool radioIng;
void MyMethod() {
radioIng = true;
try {
radioButton1.Checked = true;
// etc.
} finally {
radioIng = false;
}
}
void radioButton_EventHandler(object sender, EventArgs e) {
if (radioIng) return;
// rest of code here
}
EDIT:
Alternately, you could just remove all of your event handlers and reconnect them later:
void MyMethod() {
try {
radioButton1.CheckChanged -= radioButton_EventHandler;
radioButton2.CheckChanged -= radioButton_EventHandler;
radioButton3.CheckChanged -= radioButton_EventHandler;
// execute your code
radioButton1.Checked = true;
} finally {
radioButton1.CheckedChanged += new EventHandler(radioButton_EventHandler);
radioButton2.CheckedChanged += new EventHandler(radioButton_EventHandler);
radioButton3.CheckedChanged += new EventHandler(radioButton_EventHandler);
}
}
void radioButton_EventHandler(object sender, EventArgs e) {
if (sender == radioButton1) {
// code here to handle
} else if (sender == radioButton2) {
// code here to handle
} else if (sender == radioButton3) {
// code here to handle
}
}
Related
I am trying to make a button visible = false if a quantity in a text box is less than or equal to 0. The problem I am having is that you have to click the button in order to activate the function.
Here is my code so far
int quantity = int.Parse(textBox33.Text);
if (quantity <= 0)
button13.Visible = false;
if (quantity > 0)
button13.Visible = true;
do I have to disable the visibility of the text box beforehand?
Simply go to the form editor and double click on the textbox. In the code presented to you after double clicking add your code or double click on the form itself if you want the code to be executed whenever the form is loaded.
At first you should encapsulate the code to update the button in a specific method:
private void UpdateButton13()
{
button13.Visible = quantity > 0; // no need for if/else
}
Then you can call this from every event after which the button should be updated. From your comments it seems you want to update that button
at Form load and
when the text in textBox33 has been changed.
So for example:
public class YourForm : Form
{
public YourForm()
{
InitializeComponents();
// register event handlers:
Load += YourForm_Load;
textBox33.TextChanged += textBox33_TextChanged;
}
private void YourForm_Load(object sender, EventArgs e)
{
UpdateButton13();
}
private void textBox33_TextChanged(object sender, EventArgs e)
{
UpdateButton13();
}
private void UpdateButton13()
{
button13.Visible = quantity > 0; // no need for if/else
}
}
Of course you can also create and register the event handlers in the designer window, without having to write the code in the constructor yourself.
The code above may now seem a little redundant (same code in two methods and a one-line method). But I assume that you want to do further things on loading the form and on changing text, and maybe you want to call UpdateButton13 from other parts of your code, too. So encapsulating here is good style (imho) to avoid problems for further development.
go to textbox events and insert the code to textChanged event.
but for better than that you can do digit validation event
private void textBox33_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "[1-9]"))
{
e.Handled = true;
}
}
in that case order can be only positive number
Please read my question its not a duplicate one.
I've three radio buttons on windows form and all these buttons have common 'CheckedChanged' event associated. When I click any of these radio buttons, it triggers the 'CheckedChanged' event twice.
Here is my code:
private void radioButtons_CheckedChanged(object sender, EventArgs e)
{
//My Code
}
I inserted the breakpoint and the whole code within this event iterates twice.
Please tell me why it is behaving like this?
As the other answerers rightly say, the event is fired twice because whenever one RadioButton within a group is checked another will be unchecked - therefore the checked changed event will fire twice.
To only do any work within this event for the RadioButton which has just been selected you can look at the sender object, doing something like this:
void radioButtons_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = sender as RadioButton;
if (rb != null)
{
if (rb.Checked)
{
// Only one radio button will be checked
Console.WriteLine("Changed: " + rb.Name);
}
}
}
To avoid it, just check if radioButton is checked
for example:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
//your code
}
CheckedChanged is raised whenever the Checked property changes. If you select a RadioButton then the previously selected RadioButton is unchecked (fired CheckedChanged), and then the new RadioButton is checked (fired CheckedChanged).
It's triggering once for the radio button transition from checked to unchecked, and again for the radio button transitioning from unchecked to checked (i.e. any change in checked state triggers the event)
You could set the AutoCheck property true for each RadioButton then catch the Click event instead of the CheckChanged event. This would ensure that only one event is fired, and the logic in the handler can cast the sender to type RadioButton if needed to process the click. Often the cast can be avoided if the handler logic is simple. Here is an example which handles three controls, rbTextNumeric, rbTextFixed and rbTextFromFile:
private void rbText_Click(object sender, EventArgs e)
{
flowLayoutPanelTextNumeric.Enabled = rbTextNumeric.Checked;
txtBoxTextFixed.Enabled = rbTextFixed.Checked;
flowLayoutPanelTextFromFile.Enabled = rbTextFromFile.Checked;
}
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
int click = 0;
private void radioButton1_Click(object sender, EventArgs e)
{
click++;
if (click %2==1)
{
radioButton1.Checked = true;
}
if (click %2==0)
{
radioButton1.Checked = false;
}
if (radioButton1.Checked==true)
{
label1.Text = "Cheked";
}
if (radioButton1.Checked==false)
{
label1.Text = "Uncheked";
}
}
}
}
The other answers are correct but miss the reason for the underlying problem.
When a radio button is checked the first event sent is the change from the unchecked item
however if you check its state by its control name you will still see its old checked status because the form has not been updated yet. To see its true status you need to cast the sender object.
This allows you to perform any actions relating to the condition which is being deselected should you need to do so.
In the not uncommon scenario below multiple radio buttons are sent to the same handler event.
Simply checking the state of the sender for checked will not work here as we need to perform different actions depending on which radio button has been pressed.
So first we ignore any sender that has just been unchecked.
then we identify the checked sender by control name to process the correct action.
private void ModeChangedExample(object sender, EventArgs e)
{
// multiple radio buttons come here
// We only want to process the checked item.
// if you need to something based on the item which was just unchecked don't use this technique.
// The state of the sender has not been updated yet in the form.
// so checking against rdo_A check state will still show it as checked even if it has just been unchecked
// only the sender variable is up to date at this point.
// To prevent processing the item which has just been uncheked
RadioButton RD = sender as RadioButton;
if (RD.Checked == false) return;
if (rdo_A.Name == RD.Name)
{
//Do stuff
}
if (rdo_B..Name == RD.Name)
{
// Do other stuff
}
if (rdo_C.Name == RD.Name)
{
// Do something else
}
}
This problem of double checking happens when there is a series of RadioButton Clicks in succession.I had this same problem.The last click will give two results.To overcome this i made a dummy click in the end.The double click stopped.Try this method.
Venkatraman
I have some CheckBoxes created dynamically on code. I do read a barcode using a barcode reader. I'm trying to stop the Unchecked and Checked events from firing when I'm using the barcode. For that effect I:
only assign both events when I get the focus on the Checkboxes, and when I lose the focus I take the events out.
after each Checked and Unchecked event I assign the focus to another control in the window (so the LostFocus event gets triggered)
But went I use the barcode reader, all of the CheckBoxes objects receive the Unchecked event if they were checked (but not the Checked event if they were unchecked).
Is there a way to prevent this from happening?
The only places where the Unchecked method is being used are the ones in the code show, nowhere else in the code of the application.
A pointer to a better way to handle this dynamic creation of Checkboxes will not go unappreciated.
private void SomeMethod ()
{
foreach (KeyValuePair<String, String> kvp in someDictionary)
{
CheckBox checkBox = new CheckBox();
checkBox.Content = kvp.Key;
checkBox.GotFocus +=new RoutedEventHandler(checkBox_GotFocus);
checkBox.LostFocus += new RoutedEventHandler(checkBox_LostFocus);
checkBox.ClickMode = ClickMode.Release;
Grid.SetRow(checkBox, fileSelectionGrid.RowDefinitions.Count);
fileSelectionGrid.Children.Add(checkBox);
RowDefinition row = new RowDefinition();
fileSelectionGrid.RowDefinitions.Add(row);
}
}
void checkBox_LostFocus(object sender, RoutedEventArgs e)
{
CheckBox checkBox = sender as CheckBox;
checkBox.Checked -= new RoutedEventHandler(checkBox_Checked);
checkBox.Unchecked -= new RoutedEventHandler(checkBox_Unchecked);
}
void checkBox_GotFocus(object sender, RoutedEventArgs e)
{
CheckBox checkBox = sender as CheckBox;
checkBox.Checked += new RoutedEventHandler(checkBox_Checked);
checkBox.Unchecked += new RoutedEventHandler(checkBox_Unchecked);
}
EDIT :
Just checked that the click event is not raised when the CheckBox doesn't have the focus.
Usually for such kind of problems i declare a bool flag which is assigned value before and after code line where an event will fire and when that event is fired the first thing it does is to check for that flag value.
For e.g.
bool flag = false;
private void SomeMethod()
{
flag = true;
YourCheckBox.checked = false;
flag = false;
}
void YourCheckBox_Checked(object sender, RoutedEventArgs e)
{
if (flag)
return;
// Do something....
}
void YourCheckBox_UnChecked(object sender, RoutedEventArgs e)
{
if (flag)
return;
// Do something....
}
When i assigned flag = true the next line will fire selection changed event. when i does it will return coz flag is set to true;
Instead of basing on loosing focus, try disabling your event handlers directly before barcode reading and enabling them afterward. Something like this:
public void ReadBarcode(ComboBox cmbx)
{
FieldInfo info = cmbx.GetType().GetField("SelectedIndexChanged", BindingFlags.Instance | BindingFlags.NonPublic);
if (info != null)
{
object obj = info.GetValue(cmbx);
if (obj is EventHandler)
{
EventHandler handler = (EventHandler)obj;
cmbx.SelectedIndexChanged -= handler;
//
// Perform your bar code reading here.
//
cmbx.SelectedIndexChanged += handler;
}
}
}
I have used a single combobox for clarity sake, obviously you can use the same technique for an array of comboboxes
I found a solution. I added event handlers for KeyUp and KeyDown (the barcode input is consider keystrokes) to the control that was receiving the focus with:
e.Handled = true;
and that stopped the checkboxes receiving them.
As per the documentation, it is a Bubbling event. And the control and the checkboxes are cousins
control->grid 1->parent grid
checkbox->grid 2->parent grid
I would not have expected it to go up to the parent grid and then down. I need to read more about WPF.
Seems to me a bit of a hack. If someone gets a better answer, I will mark it.
I currently determine what page of a tabcontrol was clicked on via the SelectedIndexChanged event.
I would like to detect before the selected index actually changes, for validation purposes. For example, a user clicks a tab page other than the one they are viewing. A dialog is presented if form data is unsaved and asks if it's ok to proceed. If the user clicks no, the user should remain on the current tab.
Currently I have to remember the previous tab page and switch back to it after an answer of 'no.'
I considered MouseDown (and the assorted calculation logic), but I doubt that's the best way.
Add such an event to the tabControl when form_load:
tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);
void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
TabPage current = (sender as TabControl).SelectedTab;
// Validate the current page. To cancel the select, use:
e.Cancel = true;
}
I've actually tried all of the events including the suggestions here and none of the mentioned events occur at the right time to actually trap moving from the tab.
Even the tab page validation event fires when entering the tab rather than leaving it - either that or there's something peculiar going on with my machine or .NET 4. On the other hand, in .NET 4 there is the Deselecting event which fires at the right time for my purposes.
private void tab_Deselecting(object sender, TabControlCancelEventArgs e)
{
}
The TabControl has a collection of TabPages, each of which you can enforce validation on, e.g.:
public partial class MyForm : Form
{
public MyForm()
{
InitializeComponent();
foreach (var page in _tabControl.TabPages.Cast<TabPage>())
{
page.CausesValidation = true;
page.Validating += new CancelEventHandler(OnTabPageValidating);
}
}
void OnTabPageValidating(object sender, CancelEventArgs e)
{
TabPage page = sender as TabPage;
if (page == null)
return;
if (/* some validation fails */)
e.Cancel = true;
}
}
Ok so I'm trying to move items from one listbox to another by using multiple buttons i.e
I have 2 buttons cmdRight and cmdRight2 which are both disabled on form load
If the user selects a single item on the first listbox a cmdRIght button enables but cmdRight2 is still disabled , if the user selects multiple items on the first listbox a cmdRight2 button enables but cmdRight is disabled.
I've got the move buttons to work but the problem I'm having is after moving multiple items with the cmdRight2 button the cmdRight button enables (which it shouldn't it should only enable after selecting a single item in the listbox). I've tried numerous if statements etc. and yet it still happens.
I'm new to C# so any help would be appreciated.
Thank You
private void lbList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.lbList1.SelectedItems != null)
{
cmdRight.Enabled = true; //enable cmdRight
cmdClear.Enabled = true; //enable cmdClear
if (this.lbList1.SelectedItems.Count > 1)//if multiple items selected
{
cmdRight.Enabled = false;
cmdRight2.Enabled = true; //enable cmdRight2
}
}
}
private void cmdRight2_Click(object sender, EventArgs e)
{
foreach (int i in lbList1.SelectedIndices)
{
lbList2.Items.Add(lbList1.Items[i].ToString());
}
while (lbList1.SelectedItems.Count > 0)
{
lbList1.Items.Remove(lbList1.SelectedItems[0]);
}
cmdRight2.Enabled = false;
}
private void cmdRight_Click(object sender, EventArgs e)
{
lbList2.Items.Add(lbList1.SelectedItem); //Add selected item from list1 to list2
lbList1.Items.Remove(lbList1.SelectedItem);//remove the selected item in list1
cmdRight.Enabled = false; //disable cmdRight
}
How about creating one method EnableButtons that enables/disables to buttons according to given criteria like "enable cmdRight2 only if.... is true".
Then, call the method whenever some of the criteria might change. The advantage of this over the way you're doing it now is that the criteria within the method are "absolute" (in that the buttons are either enabled or disabled in one go) instead of "relative" (enable the button when the user does this or that).
You could also call this method from the Application.Idle event instead of calling it in response to some user action.
EDIT
Declare the following method:
private void EnableButtons()
{
controlX.Enabled = (<condition...>);
controlY.Enabled = (<condition...>);
}
You can either invoke that method from the positions in code where something should change in the buttons' enabled states, or you can do the following in the constructor of the form:
public Form1()
{
// Other code...
Application.Idle += new <The respective event handler>;
}
Then, declare a method with the respective signature for the event and call EnableButtons there. This method would be called in situations where your application is "idle" (waiting for user actions).
I think you want
if (this.lbList1.SelectedItems.Count == 1)
{
}
else if(this.lbList1.SelectedItems.Count > 1)
{
}
else
{
}
instead of
if (this.lbList1.SelectedItems != null)
Then you could place all of this in a method called "EnableButtons" as mentioned elsewhere
The problem is that you are removing the items one by one, so when only one item is left, you essentially have one item selected so your program enables the cmdRight. The easiest way around this is to have
cmdRight2.Enabled = false;
cmdRight.Enabled = false;
at the end of the cmdRight2_Click method.