Due to fact that I load data from a database and place it into a DevExpress TextEdit control on FormLoad, the event handler TextEdit_EditValueChanged is called. Is it possible to make any checking in the event handler, or prevent the event from being raised?
Something like this:
bool dataLoaded = false;
private void LoadData()
{
// do the loading and set the Text property of the textEdit
dataLoaded = true;
}
private void TextEdit_EditValueChanged(object sender, EventArgs e)
{
if (dataLoaded == false) return;
// the code after this comment will run only after the data was loaded
}
Or you can add the event handler after the loading was done, like this:
private void LoadData()
{
// do the loading and set the Text property of the textEdit
TextEdit.EditValueChanged += TextEdit_EditValueChanged;
}
private void TextEdit_EditValueChanged(object sender, EventArgs e)
{
// the code after this comment will run only after the data was loaded
}
Use property
private void TextEdit_EditValueChanged(object sender, EventArgs e)
{
if (!this.IsLoaded) return;
// the code after this comment will run only after the data was loaded
}
Related
I need a way to pass user control to user controls.
I am using Windows Forms.
For example. Say I have a radiobutton in User control 1 and I want User Control 2 to call and see if that radiobutton is checked on User control 1. How would I reference that?
And for some sample code:
This is UserControl1
public void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
This is UserControl2
private void button4_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
//do something
else
//do something
}
Seems like you have two unrelated user controls on your form. And UserControl2 (UC2) should change it's behavior is something happens on UserControl1 (UC1). That means you should have an event on UC1 which will fire if radiobutton1 checked state changes. You also will need to expose radiobutton status. You can do it either with custom EventArgs or with public property:
UserControl1
public event EventHandler SomethingChanged;
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (SomethingChanged != null)
SomethingChanged(this, EventArgs.Empty);
}
public bool IsSomethingEnabled => radioButton1.Checked;
UC2 should allow changing it's behavior. That can be done with public property
UserControl2
public bool UseCoolFeature { get; set; }
private void button4_Click(object sender, EventArgs e)
{
if (UseCoolFeature)
//do something
else
//do something else
}
And last step - coordinator which will manage both usercontrols. It's your form. Subscribe to event from UC1 and change state of UC2:
Form
private void userControl1_SomethinChanged(object sender, EventArgs e)
{
userControl2.UseCoolFeature = ((UserControl1)sender).IsSomethingEnabled;
}
You can even use in-place event handler:
userControl1.SomethingChanged += (s,e) =>
userControl2.UseCoolFeature = userControl1.IsSomethingEnabled;
You can store the value in your session:
UserControl1
public void radioButton1_CheckedChanged(object sender, EventArgs e)
{
Session["radioButton1Checked"] = radioButton1.Checked;
}
UserControl2
private void button4_Click(object sender, EventArgs e)
{
if (Session["radioButton1Checked"] != null && (bool)Session["radioButton1Checked"])
//do something
else
//do something
}
I made a combobox with the name FormatComboBox. I populated it with a list of items. I want to trigger an event whenever the user selects an item from the list. The following is my code.
private void FormatComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
/// some code
}
I put a break point inside the code to see whether it is working and found that it isn't. After I tried using
private void FormatComboBox_SelectedValueChanged(object sender, EventArgs e)
private void FormatComboBox_SelectedItemChanged(object sender, EventArgs e)
I am working on c# for the first time and I was following this tutorial
http://www.kinectingforwindows.com/2013/04/09/tutorial-kinect-television/
The one they used was the following
private void OnSelectedFormatChanged(object sender, SelectionChangedEventArgs e)
But even that is not working
Make sure that the event is attached to the FormatComboBox.
By design:
By Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox1.SelectedIndexChanged +=comboBox1_SelectedIndexChanged;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
You need to make sure that you are actually adding the event handler properly in your code or in the text box's properties. It should look something like this:
public partial class Form1 : Form
{
FormatComboBox fbox = new FormatComboBox();
// Associate event handler to the combo box.
fbox.SelectedValueChanged+=FormatComboBox_SelectedValueChanged;
prviate void FormatComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
// do stuff
}
}
SelectionChanged methods are triggered when the selection is changed by program. So, for example, calling dataGridView.ClearSelection() or dataGridView.Rows[0].Selected = true would call the method
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
}
Is it possible to execute code only when the user changed the selection, e.g. by selecting a row/cell with the mouse or keyboard?
You will have to code this in
private bool _programmaticChange;
private void SomeMethod()
{
_programmaticChange = true;
dataGridView.ClearSelection();
_programmaticChange = false;
}
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
if (_programmaticChange) return;
// some code
}
this will make it run only on user actions
What method is to be used in order to detect whether a checkbox was touched by a user to change the isChecked status in my windows phone app? In my code I manually set a checkbox on start up and the callback gets fired right away, while I only want to fire the callback if the user interacted with the view.
public CheckBoxPage()
{
InitializeComponent();
AvailableCheckBox.IsChecked = true; //name of the checkbox
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)//event handler
{
MessageBox.Show("Changed");
}
Use a variable to keep track of whether the page is loaded or not and only have the handler do stuff if it's loaded.
private bool _isLoaded = false;
public CheckBoxPage()
{
InitializeComponent();
AvailableCheckBox.IsChecked = true;
_isLoaded = true; // enable the AvailableCheckBox_Checked handler
}
void AvailableCheckBox_Checked(object sender, RoutedEventArgs e)
{
if (!_isLoaded) return; // stop here if not loaded yet
// everything is loaded so let's execute some stuff
MessageBox.Show("Changed");
}
Use the Click method:
private void AvailableCheckBox_Click(object sender, RoutedEventArgs e)
{
if (AvailableCheckBox.IsChecked == true)
{
// Checked
}
}
Add the handler after you've decided if the Checkbox should be checked.
public CheckBoxPage()
{
InitializeComponent();
AvailableCheckBox.IsChecked = true;
AvailableCheckBox.Checked += AvailableCheckBox_Checked;
}
void AvailableCheckBox_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show("Changed");
}
Let's say there is event ComboBox_SelectedIndexChange something like this
private void MyComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
//do something//
}
And i have a function which changes value of ComboBox.
Private void MyFunction()
{
MyComboBox.Text = "New Value";
}
Can i make MyFunction prevent from calling the event MyComboBox_SelectedIndexChanged while changing the value of MyComboBox?
Can i make MyFunction prevent from calling the event MyComboBox_SelectedIndexChanged while changing the value of MyComboBox?
No, you cannot. You have two fundamental options, both of which accomplish the same thing:
You can unhook the event handler method from the control, set the value, and then reattach the event handler method to the control. For example:
private void MyFunction()
{
MyComboBox.SelectedIndexChanged -= MyComboBox_SelectedIndexChanged;
MyComboBox.Text = "New Value";
MyComboBox.SelectedIndexChanged += MyComboBox_SelectedIndexChanged;
}
You can declare a class-level field that will keep track of whether the value was updated programmatically or by the user. Set the field when you want to update the combo box programmatically, and verify its value in the SelectedIndexChanged event handler method.
For example:
private bool allowComboBoxChange = true;
private void MyComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (allowComboBoxChange)
{
//do something
}
}
private void MyFunction()
{
allowComboBoxChange = false;
MyComboBox.Text = "New Value";
allowComboBoxChange = true;
}
You may attach or detach an event handler.
//attach the handler
MyComboBox.SelectedIndexChanged+=(sender,eventArgs)=>
{
//code
};
//detach the handler
MyComboBox.SelectedIndexChanged-=(sender,eventArgs)=>
{
//code
};
Or
Private void MyFunction()
{
comboBox1.SelectedIndexChanged -= new EventHandler(TestIt);
MyComboBox.Text = "New Value";
comboBox1.SelectedIndexChanged += new EventHandler(TestIt);
}
private void TestIt(object sender, EventArgs e)
{
//do something//
}