I am having a problem about holding values while scrolling between forms. I have two Windows forms. One of them is Main and the other one is NewUser
While I'm in Main and click to Change User button:
NewUser yk = new NewUser();
yk.ShowDialog();
When NewUser form loads:
private void NewUser_Load(object sender, EventArgs e)
{
if (impersonatedUser == null)
{
txtNewUserName.Select();
return;
}
btnRevert.Enabled = true; //user may revert back to the old user
btnLogon.Enabled = false;
txtNewUserName.Enabled = false;
txtPassword.Enabled = false;
btnRevert.Select();
}
reverting:
private void btnRevert_Click(object sender, EventArgs e)
{
// revert to previous user
impersonatedUser.Undo();
btnRevert.Enabled = false;
btnLogon.Enabled = true;
}
after I impersonated the user I can revert back to the user but if I close the form and then click Change User button in Main to revert user, I expect in this session already impersonated so impersonatedUser is not null and btnRevert.enabled = true; but everything starts as creating a new form. Therefore I can't revert If I don't want to lose variables in NewForm what should I do? Should I use this.Visible = false instead of this.Close() etc.? By the way I am not having any problem in Impersonation.
Your code in Main form
NewUser yk = newUser()
yk.ShowDialog()
will always create a new object of NewUser form. So if your object impersonatedUserdeclared in class NewUser it will be initialized as new again.
If, I guess, for you enough to keep in memory information of object impersonatedUser, then you can create a object of impersonatedUser in Main form and just pass it to NewUser form.
in your Main form:
Private YourClassOfUser impersonatedUser;
in you NewUser form :
Private YourClassOfUser impersonatedUser;
//Then create a custom constructor with parameter
Public void NewUser(YourClassOfUser inImpersonatedUser)
{
this.impersonatedUser = inImpersonatedUser;
}
and then in Main form open a NewUserform with this constructor
NewUser yk = newUser(this.impersonatedUser);
yk.ShowDialog();
If you can to show some code about how you initialized impersonatedUser object in NewUser form the answer can be little bid another
Related
I am trying to localize my windows phone 8.0 app (SilverLight). I want to change the default Appresources.resx file upon user choice. When the user changes the language from the settings page I want to save it by in IsolatedStorageSettings and then indicate the saved language's Appresources file in InitializeLanguage() method which was called in my constructor of app.xaml.cs class.
I learned the theoretical process but I'm unable to go further on how to approach it.
Following are the code snippets for a better understanding of my problem.
private void InitializeLanguage()
{
try
{
RootFrame.Language = XmlLanguage.GetLanguage(AppResources.ResourceLanguage);
FlowDirection flow = (FlowDirection)Enum.Parse(typeof(FlowDirection), AppResources.ResourceFlowDirection);
RootFrame.FlowDirection = flow;
}
catch
{
if (Debugger.IsAttached)
{
Debugger.Break();
}
throw;
}
}
And this the settings page code behind where I initially changed the language of a text box for test purposes which changes the language of the TextBox in runtime.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
ChangeLanguageCombo.Items.Add(new LanguageComboBox
{
Name = "English",
Code = "en-US"
});
ChangeLanguageCombo.Items.Add(new LanguageComboBox
{
Name = "Bangla",
Code = "bn"
});
}
public static IsolatedStorageSettings ChangedLanguage = IsolatedStorageSettings.ApplicationSettings;
private void ChangeLanguageCombo_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var languageComboBox = ChangeLanguageCombo.SelectedItem as LanguageComboBox;
ApplyChange(new CultureInfo(languageComboBox.Code.ToString()));
//now I want to save the user choice to the `IsolatedStorageSettings ChangedLanguage` and restart the app to take place the changes.
MessageBox.Show("Restart");
//after restart I want to indicate the Appresources file to the new selected one,(in InitializeLang() method) RootFrame.Language = XmlLanguage.GetLanguage(AppResources.ResourceLanguage); in this line
}
}
private void ApplyChange(CultureInfo culInfo)
{
Thread.CurrentThread.CurrentCulture = culInfo;
Thread.CurrentThread.CurrentUICulture = culInfo;
textBlockHello.Text = AppResources.Salutation;
}
I am sorry if the question is too clumsy to understand my purpose, I am new in this field and any kind of help or edit suggestion will do.
For retrieving the value of LocalStorageSettings from App.xaml.cs class:
string value= IsolatedStorageSettings.ApplicationSettings["userData"] as string;
In App.xaml.cs I added following code under the try block of method InitializeLanguage()
private void InitializeLanguage()
{
try
{
if (IsolatedStorageSettings.ApplicationSettings.Contains("selectedLang"))
{
var changedLang = IsolatedStorageSettings.ApplicationSettings["selectedLang"] as string;
if (changedLang != null) ApplyChange(new CultureInfo(changedLang));
}
}
//rest of the part in this method remained same
}
private void ApplyChange(CultureInfo culInfo)
{
Thread.CurrentThread.CurrentCulture = culInfo;
Thread.CurrentThread.CurrentUICulture = culInfo;
}
And in my settings page when user selects a preferred language:
public static IsolatedStorageSettings ChangedLanguage = IsolatedStorageSettings.ApplicationSettings;
private void ChangeLanguageCombo_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var languageComboBox = ChangeLanguageCombo.SelectedItem as LanguageComboBox;
if (languageComboBox != null)
{
if (!ChangedLanguage.Contains("selectedLang"))
{
ChangedLanguage.Add("selectedLang", languageComboBox.Code.ToString());
}
else
{
ChangedLanguage["selectedLang"] = languageComboBox.Code.ToString();
}
ChangedLanguage.Save();
MessageBox.Show("Restart");
}
}
After restarting the app the default Appresources file would be the new languages's Appresources file as it was saved on the IsolatedStorageSettings and on app launching the App.xaml.cs page calls the InitializeLanguage() method.
So this is the way how I was able to change the default Appresources file when user changes the language of my app from the settings page.
I am using a win form to search the record and when the record is selected from a grid on celldoubleclick event. The search form should be closed and the selected row record is loaded back to to main form from which search form is begin called.
The code to open the search form.
private void F1Button_Click(object sender, EventArgs e)
{
Forms.frmSearchNewAccount frm = new Forms.frmSearchNewAccount();
frm.ShowDialog();
if (frm.DialogResult == System.Windows.Forms.DialogResult.OK)
{
//here comes the selected record
}
}
//Search Form grid view cell double click event code is here
try
{
if (e.RowIndex >= 0)
{
this._SelectedRecord = new Flour_Mills.PARTY();
_SelectedRecord.PARTY_ID = (string)((DataTable)SearchPartydataGrid.DataSource).Rows[e.RowIndex]["PARTY_ID"];
_SelectedRecord.NAME = (string)((DataTable)SearchPartydataGrid.DataSource).Rows[e.RowIndex]["NAME"];
Controller.PartyDAL.Load(_SelectedRecord.PARTY_ID);
DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
The _selectedRecord is a static variable but it is not accessible in main form.
Any Suugestions????
If u need more explination I am here to to elaborate more.
Simply it can be do as:
public var _selectedRecordFromSearchForm;
private void F1Button_Click(object sender, EventArgs e)
{
Forms.frmSearchNewAccount frm = new Forms.frmSearchNewAccount();
frm.ShowDialog(this); // pass this form as Owner
if (frm.DialogResult == System.Windows.Forms.DialogResult.OK)
{
//here comes the selected record
}
}
In search form:
this._SelectedRecord = new Flour_Mills.PARTY();
_SelectedRecord.PARTY_ID = (string)((DataTable)SearchPartydataGrid.DataSource).Rows[e.RowIndex]["PARTY_ID"];
_SelectedRecord.NAME = (string)((DataTable)SearchPartydataGrid.DataSource).Rows[e.RowIndex]["NAME"];
Controller.PartyDAL.Load(_SelectedRecord.PARTY_ID);
this.Owner._selectedRecordFromSearchForm = _SelectedRecord; // set _searchRecoed to owners field
DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
You could declare _SelectedRecord as public in your search form and when the form closes you can access the variable like this :
if (frm.DialogResult == System.Windows.Forms.DialogResult.OK)
{
var SelectedRecord = frm._SelectedRecord;
}
I have a control with textbox. After user input few lines of text and submit that by button then, for every line of text I need to show a childwindow with grid where user has to select some value.
Let's say that user inputs 5 lines of text with names of clients (one line one client name).
For every of them aften click Submit, he must select Salesperson from ChildWindow.
Of course now effect of my loop is opening 5 ChildWindows with the in the same time.
How can I do user get next ChildWindow only after choosing an element from Childwindow grid ?
Maybe your control could use a class that looks something like this.
public class SalesPersonSelector
{
private Queue<string> _clientNamesToProcess;
private Dictionary<string, SalesPerson> _selectedSalesPersons;
private Action<IDictionary<string, SalesPerson>> _onComplete;
private string _currentClientName;
public void ProcessNames(IEnumerable<string> clientNames, Action<IDictionary<string, SalesPerson>> onComplete)
{
this._clientNamesToProcess = new Queue<string>(clientNames);
this._selectedSalesPersons = new Dictionary<string, SalesPerson>();
this._onComplete = onComplete;
this.SelectSalespersonForNextClient();
}
private void SelectSalespersonForNextClient()
{
if (this._clientNamesToProcess.Any())
{
this._currentClientName = this._clientNamesToProcess.Dequeue();
ChildWindow childWindow = this.CreateChildWindow(this._currentClientName);
childWindow.Closed += new EventHandler(childWindow_Closed);
childWindow.Show();
}
else
{
this._onComplete(this._selectedSalesPersons);
}
}
private ChildWindow CreateChildWindow(string nextClientName)
{
// TODO: Create child window and give it access to the client name somehow.
throw new NotImplementedException();
}
private void childWindow_Closed(object sender, EventArgs e)
{
var salesPerson = this.GetSelectedSalesPersonFrom(sender as ChildWindow);
this._selectedSalesPersons.Add(this._currentClientName, salesPerson);
this.SelectSalespersonForNextClient();
}
private SalesPerson GetSelectedSalesPersonFrom(ChildWindow childWindow)
{
// TODO: Get the selected salesperson somehow.
throw new NotImplementedException();
}
}
Assuming your control has already split up the names from the TextBox into a list called "names", then you can do this:
var salesPersonSelector = new SalesPersonSelector();
salesPersonSelector.ProcessNames(names, selections =>
{
foreach (var selection in selections)
{
var clientName = selection.Key;
var salesPerson = selection.Value;
// TODO: Do something with this information.
}
});
I haven't tested this, but Visual Studio isn't giving me any red squiggly lines.
On my project I show a Usercontrol childwindow for logging in. Now when I want to submit the login values (username, password) the content of that childwindow has become null... You prob think that I made it a 2nd time but no...
Here is my code for creating the childwindow. And for closing it (that's where it fails)
public void openLoginWindow()
{
if (login == false)
{
window.Content = new LoginView();
window.HasCloseButton = false;
window.Show();
}
else
{
window.Close();
}
}
Thank's for the help
Evert
what is that LoginView object? Is it a custom usercontrol? I'm not sure how your system is working but what I would do is create a specific childwindow for logging in (in that childwindow you can use your LoginView object if you want). Then in code :
public void openLoginWindow()
{
LoginChildWindow dlg = new LoginChildWindow();
dlg.HasCloseButton = false;
dlg.Closed += new EventHandler(dlg_Closed);
dlg.Show();
}
void dlg_Closed(object sender, EventArgs e)
{
LoginChildWindow dlg = ((LoginChildWindow)sender);
dlg.Closed -= dlg_Closed;
//Retrieve your values here
}
I have a Window with seven buttons; I use it as a menu in a simple game I am working on, but I display it as a dialog. How can I know which button user has pressed, since DialogResult in WPF only offers true, false and null?
If you're making a custom Window in this way, you don't really need to worry about DialogResult.
You can keep track of this in a property within your Window, and just read the property after the dialog is closed.
MyDialog window = new MyDialog();
if (window.ShowDialog() == false)
{
// user closed the window...
}
var choice = window.CustomPropertyContainingChoice;
Define your own enum and offer a static method to display the window that return your enum.
The code below does the same thing it is part of a window that allows users to review their changes and accept or cancel. As I only need true and false I used a bool however it would be trivial to change to an enum.
public static bool DisplayChanges(List<INormalizedMessage> LstMessages)
{
var retlist = LstMessages.Where(( INormalizedMessage NM ) => { return NM.Status != NormalizedMessageStatus.NoChange; });
ReviewChanges RC = new ReviewChanges();
RC.Messages = retlist.ToList();
RC.ShowDialog();
return RC.Result;
}
private void cmdCancle_Click( object sender, RoutedEventArgs e )
{
Result = false;
Hide();
}
private void cmdOK_Click( object sender, RoutedEventArgs e )
{
Result = true;
Hide();
}