how to not blocking loop in windows form - visual-studio-2010

i'm pretty new in the .net programming and i would like to get some suggestions.
I'm trying to create an easy client GUI application using the VS2010 Designer to create a single form in which i have:
1 comboBox, (containing the list of possible commands)
1 button, (used to execute the command selected in the combobox)
1 picturebox (in which i display images received from my server application)
I was able to create my client application and display a different image in the picturebox received from the server everytime i press the button.
What i would like to do is a not blocking loop in the event button click so that as long as the the client combobox command is set to start imaging, the images sent by the server are displayed in the picturebox and it stops when the client combobox command is set to stop imaging.
I'm not sure about how to do that because if i try and loop in the event button click, the GUI becomes unresponsive and i don't have a chance to change the command in the combobox.
Any help would be much appreciated.
Thanks.

Here's a "cheap" way to update the GUI by using the ReportProgress feature of the BackgroundWorker class. First drop a BackgroundWorker object on your form. Then...
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker1.DoWork += DoWork;
backgroundWorker1.ProgressChanged += UpdateGui;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();
}
private void DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
System.Threading.Thread.Sleep(1000);
backgroundWorker1.ReportProgress(0);
}
}
void UpdateGui(object sender, ProgressChangedEventArgs e)
{
textBox1.Text = DateTime.Now.ToLongTimeString();
}

Related

Close a form when FormClosing cancels the event

I am building an application that minimizes to tray when the user clicks the close button (the cross on the upper right corner) using this code:
private void FrmTest_FormClosing(object sender, FormClosingEventArgs e) {
e.Cancel = true;
WindowState = FormWindowState.Minimized;
}
however, if I want to actually close the window on a button using Close() this handler is called and the form doesn't close.
How can I close my form / application now?
I fixed it (thought I checked this, but apparently, something went amiss while debugging).
You can use the CloseReason property to find out why the event is called. When the user clicks on the close button on the window, e.CloseReason is UserClosing, otherwise it is ApplicationExitCall:
private void FrmTest_FormClosing(object sender, FormClosingEventArgs e) {
if (e.CloseReason == CloseReason.UserClosing) {
e.Cancel = true;
WindowState = FormWindowState.Minimized;
}
}
Update when using Close() to close the form, the CloseReason will be the same and you should use a boolean as Hans Passant mentions in the comments. When using Application.Exit(), this CloseReason solution works.

How to enable PhoneApplicationPage_BackKeyPress event when set textbox.Focus() on textbox_LostFocus event in windows phone 8

How to enable
PhoneApplicationPage_BackKeyPress
event when set textbox.Focus() on textbox_LostFocus event in windows phone 8.
I want to show keyboard when i add some item into listbox from textbox. Listbox gets update but my keyboard always gets hide on button click.
I want to make it open all the time till i press back key.
Thanks,
Nishant
you can get BackKeyPress Event by :
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base.OnBackKeyPress(e);
}
You can write textbox.Focus() at the end of button click when all work is done
try this :
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
ListboxName.Items.Add(TextBoxName.Text);
TextBoxName.Text = "";
TextBoxName.Focus();
}
I have implemented. Its working...

How to delete items from a list in Windows Phone 8?

I have created an app. One of the features in the app is making a list and saving to Isolated Storage.
To the point, my problem is with deleting items from the list. To clarify, the user of the app can add items to list, and then save it. I am also using a LongListMultiSelector, which I downloaded from the web. What happens here is you can check items in a special boxes that pops-up (LongListMultiSelector feature). Up to this point, every feature works well. I want the user to be able to check the checkboxes, and press delete button, much like Windows Phone Hotmail, and rid of them. This is the part where I am having trouble.
So I click the trash can button which should function as a delete button. I have the following code for it.
private void button3_Click(object sender, EventArgs e)
{
while (LLMS.SelectedItems.Count > 0)
{
source.Remove((MainList)LLMS.SelectedItems[0]);
InitializeComponent();
}
}
LLMS represents the LongListMultiSelector.
MainList is class and the list.
What happens is the app freezes up and stops working, though no pop-up comes up.
You can use this lines of code:
void OnRemoveClick(object sender, EventArgs e)
{
IList source = MainList.ItemsSource as IList;
while (MainList.SelectedItems.Count > 0)
{
source.Remove((**ObjectName**)MainList.SelectedItems[0]);
}
}

WebBrowser control: show to user when it's navigating

I'm developing a Windows Phone application.
I'm using WebBrowser control and I want to show to users when is loading a page. I've used events:
private void Browser_Navigating(object sender, Microsoft.Phone.Controls.NavigatingEventArgs e)
{
LoadingText.Visibility = System.Windows.Visibility.Visible;
}
private void Browser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
LoadingText.Visibility = System.Windows.Visibility.Collapsed;
}
But it doesn't work.
Any advice?
I think your problem is in the navigated event - this
From msdn
Occurs when the WebBrowser control has navigated to a new document and has begun loading it.
This obviously could be long before the document is actually rendered.
I'm not sure there's any event to use to determine when the page is fully loaded and is rendered.
In iron7, I detect when the editor is loaded by using a timer - that timer keeps trying to call javascript methods in the script - I know these are only available after the document javascript ready occurs.
Try using the LoadCompleted event:
private void Browser_LoadCompleted(object sender, NavigationEventArgs e)
{
LoadingText.Visibility = System.Windows.Visibility.Collapsed;
}
This ensures that once everything is rendered the loading bar will disappear.
See the msdn page: http://msdn.microsoft.com/en-us/library/microsoft.phone.controls.webbrowser.loadcompleted(v=VS.92).aspx
(I think Stuart was looking at the Windows Forms implementation of WebBrowser rather than the Phone Control)

PhotoChooserTask + Navigation

I taken two Images & added event (MouseButtonDown) for them.
When first image handles event to open Gallery. Second image handles events for open camera.
When user has choosed his image from the gallery, I want to navigate to next page. Its navigates. But before completing navigation process, it displays MainPage & then moves toward next page. I didnt want to display the MainPage once user chooses the image from the gallery.
Plz help.
Thanks in advance.
public partial class MainPage : PhoneApplicationPage
{
PhotoChooserTask objPhotoChooser;
CameraCaptureTask cameraCaptureTask;
// Constructor
public MainPage()
{
InitializeComponent();
objPhotoChooser = new PhotoChooserTask();
objPhotoChooser.Completed += new EventHandler<PhotoResult>(objPhotoChooser_Completed);
cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += new EventHandler<PhotoResult>(objCameraCapture_Completed);
}
void objPhotoChooser_Completed(object sender, PhotoResult e)
{
if (e != null && e.TaskResult == TaskResult.OK)
{
//Take JPEG stream and decode into a WriteableBitmap object
App.CapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
//Delay navigation until the first navigated event
NavigationService.Navigated += new NavigatedEventHandler(navigateCompleted);
}
}
void navigateCompleted(object sender, EventArgs e)
{
//Do the delayed navigation from the main page
this.NavigationService.Navigate(new Uri("/ImageViewer.xaml", UriKind.RelativeOrAbsolute));
NavigationService.Navigated -= new NavigatedEventHandler(navigateCompleted);
}
void objCameraCapture_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
//Take JPEG stream and decode into a WriteableBitmap object
App.CapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
//Delay navigation until the first navigated event
NavigationService.Navigated += new NavigatedEventHandler(navigateCompleted);
}
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
}
private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
objPhotoChooser.Show();
}
private void image2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
cameraCaptureTask.Show();
}
To my knowledge when you use one of the choosers, like the Photo gallery or the camera, when your application is activating it will take it back to the page you left it. I don't think there is a way to get around this itself. What you would have to do is catch the Activating event in your main page code and Navigate to the desired page from there.
Now I am not completely sure how you would pass the image from the MainPage to the target page. It does not look like there is a property in the Navigation service to store this value. But you could either set it in an application wide variable, ModelView or even store it in the Isolated Storage area.
You could work around this by navigating to an intermediate blank page and have that intermediate page launch the tasks. When the tasks are completed you can then navigate as normal to your new page and only this blank page will show in transit.
Chris is correct that some of the tasks will navigate away from your app (effectively tombstoning it) and will the re-activate your application when the user returns from the task. For the camera this is particularly difficult, as to my knowledge there is no simple way to detect when you are returning from the camera. Also the camera doesn't work when attached to the debugger or Zune software (at least this is true on my HTC Surround), which makes troubleshooting quite difficult!
In my WP7 Barcode Scanning application I ended up using flags on the PhoneApplicationService class to help track where the navigation events are coming from. Something like:
PhoneApplicationService.Current.State["ReturnFromSampleChooser"] = true;
You can then check for these flags in the PhoneApplicationPage_Loaded or OnNavigatedTo method of your main page and redirect to the desired page as needed. Just make sure to clear the flag and be careful to not cause any loops in the navigation, as that might make your app fail certification (back button must ALWAYS work correctly).
For an example of how to use the camera and set/clear flags using PhoneApplicationService check out the source code for the Silverlight ZXing Barcode Library. You can download the full source here or browse the files online.

Resources