Windows phone save form - c#

I'm developing some app for Windows phone 8.1, and I have this problem:
Have 1 form with some checkbox, radio buttons and some text fields. When I fill it up, and move to next form with:
Frame.Navigate(typeof(SOME_FORM));
and I have some things to do, and want to come back to 1st form, it's all cleared. How to remember what i fill up on 1st form, when i come back from 2nd?
And I add all controls programmatically, so I can't save it in static variable.
Any help?

In your first page set property NavigationCacheMode to Required
in XAML
NavigationCacheMode="Required"
or in code behind
this.NavigationCacheMode = NavigationCacheMode.Required;
But the question is: when do you add controls to page? if you add control in OnNavigatedTo you should check NavigationMode and don't reload page on back navigation
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
{
//do nothing - filled controls already there
return;
}
//add controls
}

Related

How to go to previous page in windows phone 8 when back button is pressed

I have a home page or landing page in my windows phone c# based app where user enters login details and upon successful login user is redirected to page2 . Here the user will see a list box with few items . Upon selecting an item from this list box a new page called "Threadx" opens.(where x is the each page that opens upon clicking the x item in the list box)
While user is on this Thread page "Threadx" he may receive the toast notifications and the thread gets updated with new replies or answers on that thread.
But When user clicks on back button the "ThreadX" page doesn't get closed and instead it goes to its previous state where it has less number of messages , and so on until the app gets closed.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.NavigationMode == NavigationMode.Back)
{
return;
}
}
I would like to know if this "Threadx" page can be closed upon clicking back button without affecting other "Threadx+1","Threadx+2"..."Threadx+n" pages.
Any help would be really appreciated.
Normally windows keeps the pages on it's stack when you leave a page and navigate to another page. If you want to navigate to the previous page on pressing the Back Button you can do following things:
Add following line to OnNavigatedTo method:
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
Add definition for HardwareButtons_BackPressed method:
private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
e.Handled = true;
if (Frame.CanGoBack)
Frame.GoBack();
}
Don't forget to add Windows.Phone.UI.Input to the namespace list.
The other way I got it worked was using the below code in the onnavigateto method in my "thread" page and it worked for me. Let me know if there is an elegant way of doing it or better way of doing it .
if (e.NavigationMode == NavigationMode.Back)
{
NavigationService.Navigate(new Uri("/View/Page2.xaml", UriKind.Relative));
}

Devexpress Navigation Menu makes screen to freez

I've a devexpress Navigation menu item that opens a data entry form (user control). The user control has validation rules that compel users not to leave textboxs blank. And, it works pretty good so far.
But, the problem comes when I click on other menu items while the data entry user control is already displayed. This time, the screen just freezes and stucks, and I've to restart the system. What are the possible causes and solutions? Thanks in advance
Here are some code snapshots:
//Here is what I've on the main form. It has a panel control called mainPanel to display the user controls
private XtraUserControl uc;
private void MainForm_Load(object sender, EventArgs e)
{
displayUserControl("Data Entry");
//...
}
private void navigationBar_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
{
displayUserControl(e.Link.Caption);
}
private void displayUserControl(string link)
{
switch (link)
{
case "Data Entry":
uc = new ucDataEntry(); //the data entry user control that freezes the system
break;
case "Setting":
uc = new ucSetting();
break;
case "Chart":
uc = new UCReportChart();
break;
}
mainPanel.Controls.Clear();
mainPanel.Controls.Add(uc);
uc.Dock = DockStyle.Fill;
uc.Show();
}
Any time a menu item is clicked, you are clearing the previous control out of your main panel, and replacing it with a new one. Perhaps it is the validation logic in the ucDataEntry control that is causing the application to hang? (You haven't posted code for that control, so I can't be sure.)
As an aside, by calling mainPanel.Controls.Clear(), you are leaking memory. The documentation for this function states that you must explicitly call the Dispose() method for any controls that are cleared in this manner.

How to refresh user control grid by using another user control event

I am developing windows application using C#. Need solution for below mentioned scenario. I have windows form containing two user control. First user control contains grid and when user click on any row of grid another user control display details of selected cell. when I modify any data from details control I need to refresh data grid in parent control.
I am using below code to load child control.
private void GridInquiry_CellClick(object sender, DataGridViewCellEventArgs e)
{
PanelParentControl.Controls.Clear();
InquiryDetailsCls.InquiryID = Convert.ToInt32(GridInquiry.SelectedRows[0].Cells[0].Value.ToString());
CtrlInqDetails inqDetails = new CtrlInqDetails(InquiryDetailsCls.InquiryID, 1);
inqDetails.Dock = DockStyle.Fill;
PanelParentControl.Controls.Add(inqDetails);
}
I can recommend to you to use the Notification Center for C#,With this you can make a global event, register for this event anywhere in you code and to fire this event from anywhere in your code.

Suppress Automatic Textbox Selection on Windows Forms Form.Show()

I have an application where I am trying to mimic the "soft description" textboxes like those found for the tags and title locations on this site.
The way I've done this is essentially to create my textbox, and depending on what happens when the mouse pointer enters or leaves the control, it updates the content of the textbox to get the effect.
The problem is what when my form is first shown, the mouse cursor immediately jumps into the first textbox, which removes the title telling the user what the textbox is for.
If I turn off AcceptTab on the textbox, then everything works as expected, but the user loses the ability to tab into the textbox.
Is there a way to turn off this automatic selection of the textbox?
Could you this.Focus() on the form itself, or on some label control?
Bit late but a perfect solution is to select the form on load of form.
Adding this line to the constructor will give the expecting result.
this.Select();
But while using multi thread controls like OpenFileDialog if u want to unfocus/deselect text-box this.Select() was not working so I selected a button in the form using.
button1.Select();
The TabIndex property controls what order things will tab in, and on load, focus goes to the first control (ordered by TabIndex) that has AcceptTab as true. You can change the ordering so that the control that you want the user focus to start in is lowest (and have tabs work cycle through controls as you'd expect).
Alternatively, as Jason suggested, you could simply call Focus() on whatever control or the form itself in the FormLoad event.
I used a variant on Jason's technique. First, I created a dummy textbox with tabindex 0. That way, when the form is shown, that textbox will be selected. Next, I made the dummy textbox have zero width, so that it has no visible component.
However, once the form is loaded, I don't want the user to be able to tab over to the "nonexistant" textbox. Therefore, I added these two bits:
//These functions prevent the textboxes from being implicitly selected.
private void dummyBox_Leave(object sender, EventArgs e)
{
dummyBox.TabStop = false;
}
private void Main_Enter(object sender, EventArgs e)
{
dummyBox.TabStop = true;
dummyBox.Select();
}
Where Main is the name of my form.
Hope this helps someone.
Billy3

How do you return the focus to the last used control after clicking a button in a winform app?

I'm working on a windows forms application (C#) where a user is entering data in a form. At any point while editing the data in the form the user can click one of the buttons on the form to perform certain actions. By default the focus goes to the clicked button so the user has to click back on to the control they want to edit in order to continue modifying the data on the form. What I need to be able to do is return the focus to the last edited control after the button click event has been processed. Here's a sample screenshot that illustrates what I'm talking about:
The user can be entering data in textbox1, textbox2, textbox3, etc and click the button. I need the button to return the focus back to the control that most recently had the focus before the button was clicked.
I'm wondering if anyone has a better way of implementing this functionality than what I've come up with. Here's what I'm doing right now:
public partial class Form1 : Form
{
Control _lastEnteredControl;
private void textBox_Enter(object sender, EventArgs e)
{
_lastEnteredControl = (Control)sender;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Do something here");
_lastEnteredControl.Focus();
}
}
So basically what we have here is a class variable that points to the last entered control. Each textbox on the form is setup so the textBox_Enter method is fired when the control receives the focus. Then, when the button is clicked focus is returned to the control that had the focus before the button was clicked. Anybody have any more elegant solutions for this?
For a bit of 'simplicity' maybe try.
public Form1()
{
InitializeComponent();
foreach (Control ctrl in Controls)
{
if (ctrl is TextBox)
{
ctrl.Enter += delegate(object sender, EventArgs e)
{
_lastEnteredControl = (Control)sender;
};
}
}
}
then you don't have to worry about decorating each textbox manually (or forgetting about one too).
You could do the following
Change the button to a label and make it look like a button. The label will never get focus and you don't have to do all the extra coding.
I think what you're doing is fine. The only thing I could think of to improve it would be to store each control into a stack as they are accessed. That would give you a complete time line of what was accessed.
Your approach looks good. If you want to avoid having to add an the event handler to every control you add, you could create a recursive routine to add a GotFocus listener to every control in your form. This will work for any type of control in your form, however you could adjust it to meet your needs.
private void Form_OnLoad(object obj, EventArgs e)
{
AddGotFocusListener(this);
}
private void AddGotFocusListener(Control ctrl)
{
foreach(Control c in ctrl.Controls)
{
c.GotFocus += new EventHandler(Control_GotFocus);
if(c.Controls.Count > 0)
{
AddGotFocusListener(c);
}
}
}
private void Control_GotFocus(object obj, EventArgs e)
{
// Set focused control here
}
Your implementation looks good enough -- what I do want to know is why you want to do this in the first place? Won't it be preferrable for the focus to cycle back to the first entry? Is the data in the last text box so malleable that once they click the button it is "remembered"? Or do you have some sort of operation that the button does to that specifici text box data -- in that case shouldn't the focus go to a subsequent control instead?
I'm interested in finding out why you want to do this in the first place.
Yeah, I admit the requirement is a bit unusual. Some of the information that the users will be entering into this application exists in scans of old documents that are in a couple of different repositories. The buttons facilitate finding and opening these old docs. It's difficult to predict where the users will be on the form when they decide to pull up a document with more information to enter on the form. The intent is to make the UI flow well in spite of these funky circumstances.
Create a class called CustomTextBox that inherits from TextBox. It has a static variable called stack. When the textbox loses focus push onto the stack. When you want to find the last focused control then just pop the first item from the stack. Make sure to clear the static Stack variable.

Categories