How to save radio button selection when navigating the back and forward?
For textbox, I know it's something like this:
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
if (e.PageState != null)
{
this.text5_input.Text = e.PageState["txtContents"] as string;
}
}
private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
e.PageState["txtContents"] = this.text5_input.Text;
}
However when we use the similar code for radio buttons, an error pops up saying cannot convert string(e.PageState) to bool(radio button).
How do I make this happen?
You can use RadioButtonInstance.IsChecked.ToString and to recover the data use bool.Parse((bool)e.PageState["rbState"])
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
if (e.PageState != null)
{
this.text5_input.Text = e.PageState["txtContents"] as string;
this.RadioButtonInstance.IsChecked = (bool)e.PageState["rbState"];
}
}
private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
e.PageState["txtContents"] = this.text5_input.Text;
e.PageState["rbState"] = this.RadioButtonInstance.IsChecked;
}
In theory this is for bool, can you try?
Related
I have an entry_TextChanged event handler and a button.
How can I say if this button clicked go to that entry_TextChanged handler again?
private bool save = false;
private void entry_TextChanged(object sender, TextChangedEventArgs e)
{
if (save == true)
{
// Do Something
}
}
private void goto_Clicked(object sender, EventArgs e)
{
save = true;
// GO and do the entry texchanged??
}
you can just call it the same as any other function entry_TextChanged(null, null) or any other values that you want to send to the function
private void goto_Clicked(object sender, EventArgs e)
{
save = true;
entry_TextChanged(null, null)
}
I wrote a code in Windows form with a checkbox that enable an Update button when the user check. Now, I am required to change the code into WPF because of UI requirement. How can I convert this code below into a working format in WPF.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
License_agreement();
}
private void License_agreement()
{
updatebutton.Enabled = checkBoxforupdate.Checked;
}
My attempted codes is below:
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
checkboxlincense.IsChecked = true;
}
private void License_agreement()
{
updatebutton.Enabled = checkboxlincense.Checked;
}
I would do it in the XAML rather than the code behind.
Set the Button's IsEnabled="{Binding ElementName=checkboxlincense, Path=IsChecked}"
I figured out how to do it. I just needed to Disable the Update button initially in the property section such as: IsEnabled="False".
Change
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
checkboxlincense.IsChecked = true;
}
...to:
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
checkboxlincense.IsChecked = true;
License_agreement();
}
private void License_agreement()
{
updatebutton.Enabled = checkboxlincense.IsChecked;
}
I will leave it to you to simplify but this closest matches your original code.
In my code I want to perform some actions when some controls are focused. So instead of having one handler for each control i was wondering if there could be any way of adding all controls to the handler and inside the handler function perform the desired action.
I have this:
private void tb_page_GotFocus(Object sender, EventArgs e)
{
tb_page.Visible = false;
}
private void tb_maxPrice_GotFocus(Object sender, EventArgs e)
{
tb_maxPrice.Text = "";
}
private void tb_maxPrice_GotFocus(Object sender, EventArgs e)
{
tb_maxPrice.Text = "";
}
I want this:
private void AnyControl_GotFocus(Object sender, EventArgs e)
{
if(tb_page.isFocused == true)
{
...
}
else if (tb_maxPrice.isFocused == true)
{
...
}
else
{
...
}
}
Is this possible? How could I do it? Thanks a lot.
Iterate your controls in your form or panel and subscribe to their GotFocus Event
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control c in this)
{
c.GotFocus += new EventHandler(AnyControl_GotFocus);
}
}
void AnyControl_GotFocus(object sender, EventArgs e)
{
//You'll need to identify the sender, for that you could:
if( sender == tb_page) {...}
//OR:
//Make sender implement an interface
//Inherit from control
//Set the tag property of the control with a string so you can identify what it is and what to do with it
//And other tricks
//(Read #Steve and #Taw comment below)
}
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
}
}
I thought we should raise an event
This is what I have found:
event OnButtonClicked ()EventArgs;
HTMLButtonClickEventArgs:EventArgs
{
String ButtonName;
}
I am doing a web Browser control and this is the code I wrote for its button clicked,but I want to know on which button user clicks:
public delegate void ButtonPressedEventHandler(object sender, EventArgs e);
public event ButtonPressedEventHandler ButtonPressed;
void OnButtonPressed()
{
if (ButtonPressed != null)
ButtonPressed(this, new EventArgs());
}
I write an example for you:try this:
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = "<html><body><button id=\"btn1\" type=\"button\">Click Me!</button><button id=\"btn2\" type=\"button\">Click Me!</button></body></html>";
}
Call Click event:
//Edited
bool First_Call = true;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (First_Call)
{
webBrowser1.Document.Click += new HtmlElementEventHandler(Document_Click);
First_Call = false;
}
}
Get Active Element When User click on document but
void Document_Click(object sender, HtmlElementEventArgs e)
{
// **Edited**
//Check Element is Button
if (webBrowser1.Document.ActiveElement.TagName == "BUTTON")
{
MessageBox.Show(webBrowser1.Document.ActiveElement.Id);
}
}