Simplified version
ParentWindow.ShowDialog()
Click default/Bring Up Child Window button in ParentWindow
Click event handler for default/Bring Up Child Window button does ParentWindow.Hide()
ChildWindow.ShowDialog()
Click ChildWindow's cancel/Go Back button
ParentWindow.Visibility = Visibility.Visible
ParentWindow is nowhere to be found.
I checked, and ParentWindow returns true for IsInitialized, IsLoaded, and IsVisible. I also Alt-Tabbed my way through all my windows to look for it - it's not hiding under anything.
Why can't I see ParentWindow anywhere?
Full version
parseSettingsWindow.ShowDialog()
Click default/Bring Up Fix Selector button in parseSettingsWindow
Click event handler for default/Bring Up Fix Selector button does:
a. ParentWindow.Hide()
b. parseSettingsWindow.GoToNextWindow flag set to true (Next window is Fix Selector)
while loop does fixSelector.ShowDialog() because it's not yet loaded
Click ChildWindow's cancel/Go Back button
while loop is entered again, goes to `case "Parse Settings" section
parseSettingsWindow.Visibility = Visibility.Visible
parseSettingsWindow is nowhere to be found
I checked, and parseSettingsWindow returns true for IsInitialized, IsLoaded, and IsVisible in the Immediate Window when pausing execution on the break; line of the "Parse Settings" while loop section. I also Alt-Tabbed my way through all my windows to look for it - it's not hiding under anything.
Why can't I see parseSettingsWindow anywhere?
Main class
public static bool UserPromptedSettingsWereWrittenToModel(ref Model model, ref ActiveFixes activeFixes, ref ActiveReports activeReports)
{
var viewModel = new ViewModel();
var parseSettingsWindow = new ViewPlusViewModel.ParseSettings();
parseSettingsWindow.InitializeComponent();
var fixSelector = new ViewPlusViewModel.FixSelector(viewModel);
fixSelector.InitializeComponent();
var seeAllFixesReports = new ViewPlusViewModel.SeeAllFixesReports();
seeAllFixesReports.InitializeComponent();
parseSettingsWindow.ShowDialog();
var nextWindowToOpen = "Fix Selector";
while (parseSettingsWindow.GoToNextWindow == true && fixSelector.GoToNextWindow == false)
{
switch(nextWindowToOpen)
{
case "Fix Selector":
if (fixSelector.IsLoaded)
{
fixSelector.Visibility = Visibility.Visible;
}
else
{
fixSelector.ShowDialog();
}
nextWindowToOpen = "Parse Settings";
break;
case "Parse Settings":
parseSettingsWindow.GoToNextWindow = false;
parseSettingsWindow.Visibility = Visibility.Visible;
nextWindowToOpen = "Fix Selector";
break;
}
}
if (parseSettingsWindow.GoToNextWindow == false)
{
parseSettingsWindow.Close();
if (fixSelector.IsLoaded) fixSelector.Close();
if (seeAllFixesReports.IsLoaded) { seeAllFixesReports.Close(); }
return false;
}
parseSettingsWindow.Close();
fixSelector.Close();
if (seeAllFixesReports.IsLoaded) { seeAllFixesReports.Close(); }
return true;
}
ParseSettingsWindow.cs
private void GoToNextWindow_Click(object sender, RoutedEventArgs e)
{
this.GoToNextWindow = true;
this.Hide();
}
You can't bring a hidden .ShowDialog() window back again via Window.Visibility. You need to use .ShowDialog() on it again. In addition, any buttons given the IsCancel = True property will no longer have that functionality even when using ShowDialog() again, so presses on that button will need to be handled manually.
Related
I need to hide the hamburger menu on certain pages but still display information in then navbar. I don’t know of any way to accomplish this.
Also, I need the navbar to stay fixed to the top of the screen but it’s getting cut off when the keyboard pops up.
How can I go about this?
FlyoutPage.ShouldShowToolbarButton method is used to determine whether to show/hide hamburger icon , and it is triggered every time when selecting pages.
We can define a bool field ,change its value when directing to specific pages.
FlyoutPage
public override bool ShouldShowToolbarButton()
{
return showIcon;
}
private bool showIcon = true;
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = e.SelectedItem as FlyoutPage1FlyoutMenuItem;
if (item == null)
return;
var page = (Page)Activator.CreateInstance(item.TargetType);
page.Title = item.Title;
Detail = new NavigationPage(page);
IsPresented = false;
FlyoutPage.ListView.SelectedItem = null;
//add this logic
showIcon = (item.Id == 1) ? false : true; //only the second page do not show hamburger icon
}
if (VendorSettingWondow == null)
{
VendorSettingWondow = new VendorSettingWindow();
VendorSettingWondow.Top = this.Top;
VendorSettingWondow.Left = this.Left + this.Width/2;
//VendorSettingWondow.Visibility = Visibility.Visible;
VendorSettingWondow.injdf.Text = filename; // setting file name
VendorSettingWondow.inJDFVendorInfo.ItemsSource = inJDFVendors;
// VendorSettingWondow.inJDFVendorInfo.ItemsSource = vendorList.Vendors;
VendorSettingWondow.DBVendorList.ItemsSource = DBvendorList.Vendors;
VendorSettingWondow.ShowDialog();
}
I am creating a subwindow if some condition is satisfied in the main window like above. There are two buttons in my sub window. When i click the button the sub window is getting closed. I am using ShowDialog() because I want to control to be transferred from main window to sub window when the subwindow is created
Check the IsCancel property of the Button. If it is set to true, set it to false to prevent the button from closing the dialog.
I am using the Tab_SelectionChanged event of TabControl in WPF. It contains 3 tab items. I have to restrict the user to navigate to other tabs i.e Settings and Schedule while work is in-progress on the home tab. While using the event i am facing an issue i.e. If i clicked on settings tab it shows me a popup "You cannot navigate while work is in progress" and when i clicked on schedule tab after clicking on settings tab it shows me the same popup twice. The reason behind this is the Settings tab remains selected.Here is my code for this:
private void tabMHPC_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TabControl tab = (TabControl)sender;
if (tab.SelectedIndex != -1)
{
if (tab.SelectedIndex != 4 && tab.SelectedIndex != 1 && tab.SelectedIndex != 0)
{
if (scanStatus == "fixing")
{
MessageBox.Show(ApplicationInfo.ApplicationName + " is still busy in fixing issues.Please let the fixation complete.", ApplicationInfo.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Information);
homeTab.IsSelected = true;
}
else
{
MessageBox.Show(ApplicationInfo.ApplicationName + " is still busy scanning issues.Please stop it before you leave the Home tab.", ApplicationInfo.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Information);
homeTab.IsSelected = true;
}
}
else if (tab.SelectedIndex == 0)
{
}
}
}
I want that previous tab item isSelected property gets false when i move on other tabitem.
Instead of handling the SelectionChanged event, you should data bind a property of a suitable type to the TabControl.SelectedItem property:
<TabControl SelectedItem="{Binding YourSelectedItemProperty}" ... />
When you do this, you will then be able to stop the TabItem being changed:
public YourDataType YourSelectedItemProperty
{
get { return yourSelectedItemProperty; }
set
{
if (isOkToChangeTabItem)
{
yourSelectedItemProperty = value;
NotifyPropertyChanged("YourSelectedItemProperty");
}
}
}
The final part of the solution is to set the isOkToChangeTabItem variable to true or false depending on whether it is OK for the user to change the selected TabItem or not.
When a user logs into my application, there are some menu items that I don't want every user to see. So I would like to either disable or make invisible the menu item. For example fileToolStripMenuItem is the first item in my menuStrip, when I try:
fileToolStripMenuItem.Enabled = false; - this does not work
menuStrip.Items[0].Enabled = false; - this does work
Can anyone enlighten me as to the difference here?
Also, I would like to be able to disable a drop down item from one of the menu items, but I cannot do that either.
Here's the code:
public Form1()
{
InitializeComponent();
// bunch of other code here
if (!login.ValidatedUser)
{
menuStrip1.Items[0].Visible = false; // this works
toolsToolStripMenuItem.Visible = false; // this does not
btnStartResourceManager.Enabled = false;
listAvailableSizes.Enabled = true;
panelPicSet.Enabled = true;
}
}
fileToolStripMenuItem.Enabled = false; works as expected. I think you trying to disable it before InitializeComponent(); call.
public form()
{
InitializeComponent();
fileToolStripMenuItem.Enabled = false;//disables all file menu
saveasToolStripMenuItem.Enabled = false; //disables save as menu item in file menu list
}
Use the specific name of your menu item and change its Visible property. i.e.
private void toggleToolStripMenuItem_Click(object sender, EventArgs e)
{
if (shown)
saveToolStripMenuItem.Visible = false;
else
saveToolStripMenuItem.Visible = true;
shown = !shown;
}
For Sub Items, just right click on the item and see its name In Design Section in Properties Window. In my case below addNewToolStripMenuItem1.
public Form()
{
InitializeComponent();
menuStrip1.Items[1].Visible = false; // For Main Item // Bold Letters
addNewToolStripMenuItem1.Visible = false; //For Sub Items
}
I'm trying to get all controls in a winform disabled at the Load event.
I have a form (MDI) which loads a Login Form. I want to disable the controls behind the Login Form to only let the user enter his username and password, and then if the user is valid re-enable the controls again.
Just show the login form as a modal dialog, i.e., frm.ShowDialog( ).
Or, if you really want to disable each control, use the Form's Controls collection:
void ChangeEnabled( bool enabled )
{
foreach ( Control c in this.Controls )
{
c.Enabled = enabled;
}
}
I suggest doing it this way instead of simply setting the Form's Enabled propery because if you disable the form itself you also disable the tool bar buttons. If that is ok with you then just set the form to disabled:
this.Enabled = false;
However, if you are going to do this you may as well just show the login prompt as a modal dialog :)
Simple Lambda Solution
form.Controls.Cast<Control>()
.ToList()
.ForEach(x=>x.Enabled = false);
Container like Panel control that contains other controls
then I used queue and recursive function get all controls.
for (Control control in GetAllControls(this.Controls))
{
control.Enabled = false;
}
public List<Control> GetAllControls(Control.ControlCollection containerControls, params Control[] excludeControlList)
{
List<Control> controlList = new List<Control>();
Queue<Control.ControlCollection> queue = new Queue<Control.ControlCollection>();
queue.Enqueue(containerControls);
while (queue.Count > 0)
{
Control.ControlCollection controls = queue.Dequeue();
if (controls == null || controls.Count == 0)
continue;
foreach (Control control in controls)
{
if (excludeControlList != null)
{
if (excludeControlList.SingleOrDefault(expControl => (control == expControl)) != null)
continue;
}
controlList.Add(control);
queue.Enqueue(control.Controls);
}
}
return controlList;
}
Just for some fun with linq, because you can.....
What you could do is create a "BatchExecute" extension method for IEnumerable and update all your controls in 1 hit.
public static class BatchExecuteExtension
{
public static void BatchExecute<T>(this IEnumerable<T> list, Action<T> action)
{
foreach (T obj in list)
{
action(obj);
}
}
}
Then in your code....
this.Controls.Cast<Control>().BatchExecute( c => c.enabled = false);
Cool.
I agree that ShowDialog is the way to go, but to answer the original question, you can do this if you want to disable all controls:
foreach (Control c in this.Controls)
{
c.Enabled = false;
}
As Ed said, showing the form as a modal dialog will do what you want. Be sure to check the dialog result returned from ShowDialog in case they cancel it instead of clicking login.
But if you really want to disable all the controls on the form then you should be able to just disable the form itself, or some other parent control like a panel that has all controls in it. That will disable all child controls. This will also allow the child controls to go back to their previous state when the parent control is enabled again.
Trying the ShowDialog show this exception:
Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog.
What im doing is this:
private void frmControlPanel_Load(object sender, EventArgs e)
{
WindowState = FormWindowState.Maximized;
ShowLogin();
//User = "GutierrezDev"; // Get user name.
//tssObject02.Text = User;
}
private void ShowLogin()
{
Login = new frmLogin
{
MdiParent = this,
Text = "Login",
MaximizeBox = false,
MinimizeBox = false,
FormBorderStyle = FormBorderStyle.FixedDialog,
StartPosition = FormStartPosition.CenterScreen
};
Login.ShowDialog();
}