I want to searialise and save the IsolatedStorageSettings.SiteSettings on all the tabs when the browser is closed. But I get the Exit event only on the tab that is presently active.
How is it possible that all the tab data is saved
public partial class App
{
private siteSettings = IsolatedStorageSettings.SiteSettings;
public App()
{
InitializeComponent();
this.Exit += App_Exit;
}
private static void App_Exit(object sender, EventArgs e)
{
App app = (App)sender;
app.Exit -= App_Exit;
siteSettings.Save();
}
}
Related
I am using IStylusSyncPlugin added to RealTimeStylus plugins to get X,Y,Pressure and timer tick from stylus. This works fine until the window on which I collect this data loses focus. After that, even if focus get back to window, the StylusSyncPlugin does not receiving data. Do anyone have any idea what can i do, to fix this problem? I've found, that stylus events from main window (for ex. PreviewStylusMove) are still firing, but points from these events does not contains timestamp.
A simple code example which could be helpful to reproduce this issue:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
RealTimeStylus rts = new RealTimeStylus(new WindowInteropHelper(this).Handle);
rts.AsyncPluginCollection.Add(new SyncStylusPlugin());
rts.Enabled = true;
}
class SyncStylusPlugin : IStylusSyncPlugin
{
public DataInterestMask DataInterest => DataInterestMask.Packets;
public void Packets(RealTimeStylus sender, PacketsData data)
{
Console.WriteLine("Packets arrived");
}
public void StylusDown(RealTimeStylus sender, StylusDownData data) { }
public void StylusUp(RealTimeStylus sender, StylusUpData data) { }
public void CustomStylusDataAdded(RealTimeStylus sender, CustomStylusData data) { }
public void Error(RealTimeStylus sender, ErrorData data) { }
public void InAirPackets(RealTimeStylus sender, InAirPacketsData data) { }
public void RealTimeStylusDisabled(RealTimeStylus sender, RealTimeStylusDisabledData data) { }
public void RealTimeStylusEnabled(RealTimeStylus sender, RealTimeStylusEnabledData data) { }
public void StylusButtonDown(RealTimeStylus sender, StylusButtonDownData data) { }
public void StylusButtonUp(RealTimeStylus sender, StylusButtonUpData data) { }
public void StylusInRange(RealTimeStylus sender, StylusInRangeData data) { }
public void StylusOutOfRange(RealTimeStylus sender, StylusOutOfRangeData data) { }
public void SystemGesture(RealTimeStylus sender, SystemGestureData data) { }
public void TabletAdded(RealTimeStylus sender, TabletAddedData data) { }
public void TabletRemoved(RealTimeStylus sender, TabletRemovedData data) { }
}
}
I've found, that click on application icon at taskbar making RealTimeStylus work again. Is there any way to fire the same events like mouse do when clicking on this icon? Which events should be fired?
EDIT:
During reading microsoft docs about RealTimeStylus I've found, that "When you create a RealTimeStylus object, you have the option of attaching it to a window handle or to a control. Attaching the RealTimeStylus object to a window handle requires additional permissions. For more information about these permissions, see Partial Trust Considerations for the StylusInput APIs." "The RealTimeStylus that takes the handle parameter requires the UIPermissionWindow.AllWindows and SecurityPermissionFlag.UnmanagedCode permissions, in addition to the permissions required by the constructor that takes the attachedControl parameter." Do anyone know how to check, if Window has these permissions, and how to track them to check if Window loses them sometimes on focus lost?
EDIT2:
I tried to set UIPermission by adding [UIPermission(SecurityAction.Demand, Window =UIPermissionWindow.AllWindows)] to main window class and also by setting new UIPermission(UIPermissionWindow.AllWindows).Demand(); in main window constructor, but it changes nothing.
EDIT3:
Next thing that I have noticed is that, the StylusInRange and StylusOutOfRange events are working everytime, even if window has no focus.
I'm facing the same issue raised in "Arrow keys and changing control's focus hang the application". Since I'm unable to comment on it due to I don't have enough points. Hence, I posted a new one.
Thanks in advance.
Run the program
Select any radiobutton
Click button2
Press tab to focus onto radiobutton
Press Left/Right Navigation keys
Form hang-up (not responding).
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
ucPage11.BringToFront();
ucPage11.Enabled = true;
richTextBox1.Enabled = false;
}
private void ucPage11_SelectionChanged()
{
ucPage11.Enabled = false;
richTextBox1.BringToFront();
richTextBox1.Enabled = true;
}
}
UserControl.cs
- The user control has 3 radio button control in it.
public delegate void SelectionEventHandler();
public partial class UCPage1 : UserControl
{
//private ToggleImageControlManager toggleImgCtrlMgr;
public UCPage1()
{
this.InitializeComponent();
}
public event SelectionEventHandler SelectionChanged;
private void RBO_Click(object sender, EventArgs e)
{
SelectionChanged?.Invoke();
}
}
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.
I Made an application. The Main form Name is Form1.
And the other Form is called PoP.
public partial class pops : Form
{
public pops()
{
InitializeComponent();
CenterToScreen();
}
private void pops_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Close();
}
private void lblAdminNo_Click(object sender, EventArgs e)
{
}
}
Make two public properties on popup form and retrieve them from parent form.
string username = string.Empty;
string password = string.Empty;
using (LoginForm form = new LoginForm ())
{
DialogResult result = form.ShowDialog();
if (result == DialogResult.Ok)
{
username = form.Username;
password = form.Password;
}
}
It all depends on from where are you calling the Pop form.
If it is called from the Form1 itself, then the Popform's object itself would provide you the value.
Pop popFrm = new Pop();
if(popFrm.ShowDialog() == Ok)
{
string userName = popFrm.TextBox1.Text;
}
If the Pop is invoked from a different area/part of application, you may have to store it somewhere common to both the forms.
This can be done through events. This approach is particularly useful when data to be posted even when the child form is kept open.
The technique is- From parent form, subscribe to a child from event. Fire the event when child form closes, to send data
----- SAMPLE CODE-----
Note: In the Parent Form add a Button:button1
namespace WindowsFormsApplication2
{
public delegate void PopSaveClickedHandler(String text);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Pops p = new Pops();
p.PopSaveClicked += new PopSaveClickedHandler(p_PopSaveClicked);//Subscribe
p.ShowDialog();
}
void p_PopSaveClicked(string text)
{
this.Text = text;//you have the value in parent form now, use it appropriately here.
}
}
Note: In the Pops Form add a TextBox:txtUserName and a Button:btnSave
namespace WindowsFormsApplication2
{
public partial class Pops : Form
{
public event PopSaveClickedHandler PopSaveClicked;
public Pops()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
if(PopSaveClicked!=null)
{
this.PopSaveClicked(txtUserName.Text);
}
}
}
}
Summary:
1.Add a delegate(place where it available to both parent and child form) :
public delegate void PopSaveClickedHandler(String text);
2.In form:Pops, Add an event:
public event PopSaveClickedHandler PopSaveClicked;
3.Subscribe to the event in Parent Form:
p.PopSaveClicked += new PopSaveClickedHandler(p_PopSaveClicked);
4.Invoke the event in form:Pops Save Button Click
if(PopSaveClicked!=null)
{
this.PopSaveClicked(txtUserName.Text);
}
You can send data to the form object before you display it. Create a method to call, send the info through the constructor... etc.
I have an application that has a main form and uses an event handler to process incoming data and reflect the changes in various controls on the main form. This works fine.
I also have another form in the application. There can be multiple instances of this second form running at any given time.
What I'd like to do is have each instance of this second form listen to the event handler in the main form and update controls on its instance of the second form.
How would I do this?
Here's some sample code. I want to information from the_timer_Tick event handler to update each instance of SecondaryForm.
public partial class Form1 : Form
{
Timer the_timer = new Timer();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
the_timer.Tick += new EventHandler(the_timer_Tick);
the_timer.Interval = 2000;
the_timer.Enabled = true;
}
void the_timer_Tick(object sender, EventArgs e)
{
// I would like code in here to update all instances of SecondaryForm
// that happen to be open now.
MessageBox.Show("Timer ticked");
}
private void stop_timer_button_Click(object sender, EventArgs e)
{
the_timer.Enabled = false;
}
private void start_form_button_Click(object sender, EventArgs e)
{
SecondaryForm new_form = new SecondaryForm();
new_form.Show();
}
}
class SecondForm
{
private FirstForm firstForm;
public SecondForm()
{
InitializeComponent();
// this means unregistering on form closing, uncomment if is necessary (anonymous delegate)
//this.Form_Closing += delegate { firstForm.SomeEvent -= SecondForm_SomeMethod; };
}
public SecondaryForm(FirstForm form) : this()
{
this.firstForm = form;
firstForm.Timer.Tick += new EventHandler(Timer_Tick);
}
// make it public in case of external event handlers registration
private void Timer_Tick(object sender, EventArgs e)
{
// now you can access firstForm or it's timer here
}
}
class FirstForm
{
public Timer Timer
{
get
{
return this.the_timerl
}
}
public FirstForm()
{
InitializeComponent();
}
private void Button_Click(object sender, EventArgs e)
{
new SecondForm(this).ShowDialog(); // in case of internal event handlers registration (in constructor)
// or
SecondForm secondForm = new SecondForm(this);
the_timer.Tick += new EventHandler(secondForm.Timer_tick); // that method must be public
}
Consider using loosely coupled events. This will allow you to couple the classes in such a way that they never have to be directly aware of each other. The Unity application block comes with an extension called EventBroker that makes this very simple.
Here's a little lick of the sugar:
public static class EVENTS
{
public const string UPDATE_TICKED = "event://Form1/Ticked";
}
public partial class Form1 : Form
{
[Publishes(EVENTS.UPDATE_TICKED)]
public event EventHandler Ticked;
void the_timer_Tick(object sender, EventArgs e)
{
// I would like code in here to update all instances of SecondaryForm
// that happen to be open now.
MessageBox.Show("Timer ticked");
OnTicked();
}
protected virtual void OnTicked()
{
if (Ticked == null) return;
Ticked(this, e);
}
}
public partial class SecondaryForm : Form
{
[SubscribesTo(EVENTS.UPDATE_TICKED)]
private void Form1_Ticked(object sender, EventHandler e)
{
// code to handle tick in SecondaryForm
}
}
Now if you construct both of these classes using Unity, they will automatically be wired together.
Update
Newer solutions use message bus to handle loosely coupled events. See http://masstransit-project.com/ or http://nimbusapi.github.io/ as examples.
I guess you can make SecondaryForm take in the parent form in the constructor, and the add an event handler in the constructor.
private void start_form_button_Click(object sender, EventArgs e)
{
SecondaryForm new_form = new SecondaryForm(this);
new_form.Show();
}
In SecondaryForm.cs:
public SecondaryForm(ISomeView parentView)
{
parentView.SomeEvent += .....
}