how to enable menu strip in child form?
i just want to enable the menu button strip when i will close the child form
how to code that in child form?
Student....
private void tsmNewEmp_Click(object sender, EventArgs e)
{
if(NewEmp == null)
{
NewEmp = new NewEmployee();
NewEmp.MdiParent = this;
}
NewEmp.Show();
tsmNewEmp.Enabled = false;
tsmNewContract.Enabled = false;
}
You can use the FormClosed event to update the buttons in the parent form after the child is closed:
private void tsmNewEmp_Click(object sender, EventArgs e)
{
if(NewEmp == null)
{
NewEmp = new NewEmployee();
NewEmp.MdiParent = this;
NewEmp.FormClosed += FormClosed_1;
}
NewEmp.Show();
tsmNewEmp.Enabled = false;
tsmNewContract.Enabled = false;
}
private void FormClosed_1(object sender, FormClosedEventArgs e)
{
tsmNewEmp.Enabled = true;
tsmNewContract.Enabled = true;
}
Control[] controls = this.MdiParent.Controls.Find("Menu", true);
foreach (Control ctrl in controls)
{
if (ctrl.Name == "Menu")
{
MenuStrip strip = ctrl as MenuStrip;
strip.Items["login"].Enabled = false;
strip.Items["logout"].Enabled = false;
}
}
Related
i try to close windows form in mdiparent when i click other button, the result is when i click other button, it still appear from the back of new window. so how can i handle this?
private void btn_ic_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
SelectIC ss = new SelectIC();
ss.MdiParent = this;
ss.Show();
Detail aa = new Detail();
aa.MdiParent = this;
aa.Close();
btn_ic.Enabled = false;
btn_cat.Enabled = true;
}
private void btn_cat_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
Detail aa = new Detail();
aa.MdiParent = this;
aa.Show();
SelectIC ss = new SelectIC();
ss.MdiParent = this;
ss.Close();
btn_cat.Enabled = false;
btn_ic.Enabled = true;
}
You're making new instance of form and then closing it. That way you're not closing existing window but creating new (invisible) one and closing it. You should find existing window in collection of MdiChildren and then close it. Something like this:
private void btn_ic_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
SelectIC ss = new SelectIC();
ss.MdiParent = this;
ss.Show();
var detailForm = this.MdiChildren.FirstOrDefault(f => f.GetType() == typeof(Detail));
detailForm?.Close();
btn_ic.Enabled = false;
btn_cat.Enabled = true;
}
private void btn_cat_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
Detail aa = new Detail();
aa.MdiParent = this;
aa.Show();
var selectForm = this.MdiChildren.FirstOrDefault(f => f.GetType() == typeof(SelectIC));
selectForm?.Close();
btn_cat.Enabled = false;
btn_ic.Enabled = true;
}
i am working in treeview in which no option for the child node and parent node click event so plz suggest me to work on Both click event
my code is
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (treeView1.SelectedNode = "Edit User")
{
label2.Visible = true;
}
else
{
label2.Visible = false;
}
}
Try This :
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node.Text = "Edit User")
{
label2.Visible = true;
}
else
{
label2.Visible = false;
}
}
I have a user control (AddNewTransaction) inside a Popup (NewTransaction in DebtPage.xaml) that I open with this code:
[DebtPage.xaml.cs]
public void Button_Click_1(object sender, RoutedEventArgs e)
{
if (!NewTransaction.IsOpen) { NewTransaction.IsOpen = true; }
NewTransaction.HorizontalOffset = (Window.Current.Bounds.Width) / 2 - (New_Transaction_Grid.Width / 2);
NewTransaction.VerticalOffset = (Window.Current.Bounds.Height) / 2 - (New_Transaction_Grid.Height / 2);
NewTransaction.Visibility = Visibility.Visible;
RetanguloBranco_Background.Visibility = Visibility.Visible;
}
The Popup opens and there's a custom control (AddNewTransaction) with a "Save" button. The button suppose to save data AND close the Popup. But it just save, the Popup keeps opened.
[AddNewTransaction.xaml.cs]
private static DebtPage _debtPage;
public static DebtPage debtPage
{
get
{
if (_debtPage == null)
{
_debtPage = new DebtPage();
}
return _debtPage;
}
}
private void BotaoSave_Click(object sender, RoutedEventArgs e)
{
SalvarTransaction(); //It's working!
debtPage.ClosePopup_NewTransaction(); //It is not!
}
[DebtPage.xaml.cs]
public void ClosePopup_NewTransaction()
{
NewTransaction.IsOpen = false;
NewTransaction.Visibility = Visibility.Collapsed;
RetanguloBranco_Background.Visibility = Visibility.Collapsed;
}
Thanks!
I've solved the problem with this:
private void BotaoSave_Click(object sender, RoutedEventArgs e)
{
SalvarTransaction();
Grid gridParent = this.Parent as Grid;
Popup parent = gridParent.Parent as Popup;
if (parent != null)
{
parent.IsOpen = false;
}
}
But I still want to know why ClosePopup_NewTransaction(); does nothing.
So I created a window with a system tray icon. The window starts out minimized and will re-appear when the system tray Icon is clicked. However, it ONLY works when you click on the minimize button. If you click the red exit button the window disappears, the System Tray Icon remains(as it should) but when you click on it the program throws an error.
Cannot set Visibility or call Show, ShowDialog, or
WindowInteropHelper.EnsureHandle after a Window has closed.
Here is the relevant code
public partial class MainWindow : Window
{
public static NotifyIcon icon;
List<string> food = new List<string>();
bool on = false;
public MainWindow()
{
InitializeComponent();
food.Add("Breakfast");
food.Add("Soups");
food.Add("Vegetables");
food.Add("Crab roll");
food.Add("Sushi");
food.Add("Egg rolls");
food.Add("Salad");
MainWindow.icon = new NotifyIcon();
window1.WindowState = WindowState.Minimized;
icon.Icon = new System.Drawing.Icon("favicon.ico");
icon.Visible = true;
icon.Click += new EventHandler(icon_Click);
icon.BalloonTipClicked += new EventHandler(icon_BalloonTipClicked);
icon.DoubleClick += new EventHandler(icon_DoubleClick);
icon.BalloonTipClosed += new EventHandler(icon_BalloonTipClosed);
icon.MouseMove += new System.Windows.Forms.MouseEventHandler(icon_MouseMove);
StateChanged += new EventHandler(MainWindow_StateChanged);
}
void icon_BalloonTipClicked(object sender, EventArgs e)
{
this.Show(); //This is where the error is
window1.WindowState = WindowState.Normal;
}
void icon_DoubleClick(object sender, EventArgs e)
{
this.Show(); //This is where the error is
window1.WindowState = WindowState.Normal;
}
void icon_BalloonTipClosed(object sender, EventArgs e)
{
on = false;
}
void icon_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (!on)
{
icon.BalloonTipText = "";
foreach(string item in food){
if (!item.Contains("Breakfast") && !item.Contains("Soups") && !item.Contains("Vegetables"))
{
icon.BalloonTipText += item+"\n";
}
}
icon.ShowBalloonTip(10);
on = true;
}
}
void MainWindow_StateChanged(object sender, EventArgs e)
{
if (window1.WindowState == WindowState.Minimized)
{
this.Hide();
}
}
private void icon_Click(Object sender, EventArgs e)
{
icon.BalloonTipText = "";
foreach (string item in food)
{
if (!item.Contains("Breakfast") && !item.Contains("Soups") && !item.Contains("Vegetables"))
{
icon.BalloonTipText += item + "\n";
}
}
icon.ShowBalloonTip(10);
on = true;
}
}
Intercept the Closing event of the window, and cancel it (this will prevent the window from closing) - then hide the window instead:
public MainWindow()
{
// Subscribe to closing event (when X is pressed)
this.Closing += MainWindow_Closing;
InitializeComponent();
}
void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Prevent window from closing
e.Cancel = true;
// Hide window
this.Hide();
}
I have created a virtual keyboard, the keyboard pops up for wpf textbox controls. How can I make the keyboard pops out when clicking on a textbox inside a web page?
the logic for displaying a textbox is given below:
static void TouchScreenKeyboardPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
FrameworkElement host = sender as FrameworkElement;
if (host != null)
{
host.GotFocus += new RoutedEventHandler(OnGotFocus);
host.LostFocus += new RoutedEventHandler(OnLostFocus);
}
}
static void OnGotFocus(object sender, RoutedEventArgs e)
{
Control host = sender as Control;
if (sender == typeof(TextBox))
{
_PreviousTextBoxBackgroundBrush = host.Background;
_PreviousTextBoxBorderBrush = host.BorderBrush;
_PreviousTextBoxBorderThickness = host.BorderThickness;
host.Background = Brushes.Yellow;
host.BorderBrush = Brushes.Red;
host.BorderThickness = new Thickness(4);
}
_CurrentControl = host;
if (_InstanceObject == null)
{
FrameworkElement ct = host;
while (true)
{
if (ct is Window)
{
((Window)ct).LocationChanged += new EventHandler(TouchScreenKeyboard_LocationChanged);
((Window)ct).Activated += new EventHandler(TouchScreenKeyboard_Activated);
((Window)ct).Deactivated += new EventHandler(TouchScreenKeyboard_Deactivated);
break;
}
if(ct.Parent != null)
ct = (FrameworkElement)ct.Parent;
}
_InstanceObject = new TouchScreenKeyboard();
_InstanceObject.AllowsTransparency = true;
_InstanceObject.WindowStyle = WindowStyle.None;
_InstanceObject.ShowInTaskbar = false;
_InstanceObject.ShowInTaskbar = false;
_InstanceObject.Topmost = true;
host.LayoutUpdated += new EventHandler(tb_LayoutUpdated);
}
}
static void TouchScreenKeyboard_Deactivated(object sender, EventArgs e)
{
if (_InstanceObject != null)
{
_InstanceObject.Topmost = false;
}
}
static void TouchScreenKeyboard_Activated(object sender, EventArgs e)
{
if (_InstanceObject != null)
{
_InstanceObject.Topmost = true;
}
}
static void TouchScreenKeyboard_LocationChanged(object sender, EventArgs e)
{
syncchild();
}
static void tb_LayoutUpdated(object sender, EventArgs e)
{
syncchild();
}
static void OnLostFocus(object sender, RoutedEventArgs e)
{
Control host = sender as Control;
host.Background = _PreviousTextBoxBackgroundBrush;
host.BorderBrush = _PreviousTextBoxBorderBrush;
host.BorderThickness = _PreviousTextBoxBorderThickness;
if (_InstanceObject != null)
{
_InstanceObject.Close();
_InstanceObject = null;
}
}
#endregion
}
private static void syncchild()
{
if (_CurrentControl != null && _InstanceObject != null)
{
Point virtualpoint = new Point(0, _CurrentControl.ActualHeight + 3);
Point Actualpoint = _CurrentControl.PointToScreen(virtualpoint);
if (WidthTouchKeyboard + Actualpoint.X > SystemParameters.VirtualScreenWidth)
{
double difference = WidthTouchKeyboard + Actualpoint.X - SystemParameters.VirtualScreenWidth;
_InstanceObject.Left = Actualpoint.X - difference;
}
else if (!(Actualpoint.X > 1))
{
_InstanceObject.Left = 1;
}
else
_InstanceObject.Left = Actualpoint.X;
_InstanceObject.Top = Actualpoint.Y;
_InstanceObject.Show();
}
}
private static void SetKeyInBrowser(string key)
{
var elementName = (((mshtml.HTMLDocument)(dom)).activeElement).id;
if (elementName != null)
{
var existingText = dom.getElementById(elementName).getAttribute("value");
if (existingText == null)
{
existingText = "";
}
//if it's a backspace.
if (isBackspace)
{
existingText = existingText.ToString().Remove(existingText.ToString().Length - 1);
dom.getElementById(elementName).setAttribute("value", existingText.ToString());
}
else if (key.Length != 0)
{
dom.getElementById(elementName).setAttribute("value", existingText.ToString() + key[key.Length - 1]);
}
}
isBackspace = false;
}
You need to use javascript for that. You need to know when that textfield is focused like this:
Check if focus is on a textfield
And then once the focused event is fired on JavaScript you should call your WPF function for showing the keyboard up. Here's useful hints:
How to programmaticaly check if a text input box inside a web page loaded in WPF Browser has focus?