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");
}
Related
I'm trying to implement a customized exit prompt in my WinForms. (I should not be using DialogBox)
I have a User Control Object placed in my main form that is invisible and disabled by default. Clicking in a certain button I have placed on the form shows and enables the object, disabling everything in my form except the User Control.
private void btn_close_Click(object sender, EventArgs e) {
prompt1.Visible = true;
prompt1.Enabled = true;
disableControls();
//Wait for a button to be pressed in prompt1
//Make an action based on a button pressed.
//closeApp returns a boolean
if (!prompt1.closeApp)
{
prompt1.Visible = false;
prompt1.Enabled = false;
enableControls();
}
else
{
Application.Exit();
}
}
Here's my code at the prompt object:
public partial class Prompt : UserControl
{
bool exit;
public bool closeApp
{
get{return exit;}
}
public Prompt()
{
InitializeComponent();
}
private void btn_yes_Click(object sender, EventArgs e)
{
exit = true;
}
private void btn_no_Click(object sender, EventArgs e)
{
exit = false;
this.Hide();
}
}
What I want to do is wait for a button to be pressed in my prompt object before proceeding to the next line in the btn_close_Click().
What should I do? Is there a better way to implement this?
Add events to your usercontrol then handle those events on your main form.
In your usercontrol:
public event EventHandler<EventArgs> ExitCancelled;
public event EventHandler<EventArgs> ExitApplication;
private void btn_yes_Click(object sender, EventArgs e)
{
ExitApplication?.Invoke(this, EventArgs.Empty);
}
private void btn_no_Click(object sender, EventArgs e)
{
ExitCancelled?.Invoke(this, EventArgs.Empty);
}
Handle the events on your form:
public void prompt1_ExitApplication(object sender, EventArgs e)
{
Application.Exit();
}
public void prompt1_ExitCancelled(object sender, EventArgs e)
{
prompt1.Hide();
enablecontrols();
}
I am using Virtual Studio Community in C# (.Net 4.5).
I have a simple form, which contains one button and one webBrowser control.
When I click the button, I make the webBrowser navigate to google.com.
Then, when the page is loaded, I try to override the linkClick events as I saw in a solution I read on this site (stackoverflow) earlier.
But then, when I click on a link on the loaded page, it says the navigation was cancelled, but it navigates anyways.
What am I doing wrong?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com/");
}
private bool bCancel = false;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
for (int i = 0; i < webBrowser1.Document.Links.Count; i++)
{
webBrowser1.Document.Links[i].Click += new HtmlElementEventHandler(this.LinkClick);
}
}
private void LinkClick(object sender, System.EventArgs e)
{
bCancel = true;
MessageBox.Show("Link Was Clicked Navigation was Cancelled");
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (bCancel == true)
{
e.Cancel = true;
bCancel = false;
}
}
}
You need to bind the WebBrowserControl.Navigating event to the handler you've written; having the handler's name matching the control underscore event name isn't enough.
So you can do this in the Form1's constructor:
public Form1()
{
InitializeComponent();
webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
}
Better yet, add a Load event and do the same there. Check out the official documentation on the subject.
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
I have a user control with some buttons (tmNewItem, tmEdit, tmInsert)
I write a clickButton event for them.
for example:
public void btnEdit_Click(object sender, EventArgs e)
{
btnNew.Enabled = false;
btnEdit.Enabled = false;
}
I used this user control in another project and write another method for the buttons and assign it to the usr control:
public void DTedit(object sender, EventArgs e)
{
}
private void UserControl_Load(object sender, EventArgs e)
{
DT_Navigator.btnCancel.Click += new EventHandler(DTedit);
}
and now, when I run the project and press btnEdit button, the first time, btnEdit_Click will execute and after that DTedit. can i change it? I mean the first time DTedit (that I define it in my project) run, and after it btnEdit_Click (that I define it in the user control) run?
how can I do that?
Try this
public void DTedit(object sender, EventArgs e)
{
//Place your code here
DT_Navigator.btnCancel.Click -= new EventHandler(DTedit); //This will remove handler from the button click and it will not be executed next time.
}
private void UserControl_Load(object sender, EventArgs e)
{
DT_Navigator.btnCancel.Click += new EventHandler(DTedit);
}
Suggested Code
//User control
public event CancelEventHandler BeginEdit;
public event EventHandler EndEdit;
private btnYourButton_Click(object sender, EventArgs e)
{
CancelEventArgs e = new CancelEventArgs();
e.Cancel = false;
if (BeginEdit != null)
BeginEdit(this, e);
if (e.Cancel == false)
{
if (EndEdit != null)
EndEdit(this, new EventArgs);
//You can place your code here to disable controls
}
}
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
}