Form is still "TopMost" although it is set to false - winforms

I don't know how to solve this. I have a Form2 which is opened via a Form1. Although the option "TopMost" is set to "false" the Form2 is still in the foreground. Clicking on the Form1 somewhere does nothing. Is there a way to solve this and let the Form2 pop up again into the Foreground when the button is clicked which opens it?
private void button_Click(object sender, EventArgs e)
{
if (form2.Visible == false)
{
form2 = new Form2(this);
form2.Show(this);
}
}

Related

Adding tabpage to a tabcontrol which is on another form on click of button

I have a winform called form1 which has tabcontrol with some tabpages and one button on clicking of which I open another form called form2. I want to add tabpage to form1 tabcontrol on click of button which is present on form2.
Assumed that you have not created a tabcontrol at runtime in the Firstform.
In FirstForm.cs
private void button1_Click(object sender, EventArgs e)
{
SecondForm obj = new SecondForm(this);
obj.Show();
}
public void addTabpage()
{
TabPage tbpg = new TabPage();
tbpg.Text = "New tabpage";
tabControl1.TabPages.Add(tbpg);
this.BringToFront();
}
In SecondForm.cs, change the constructor as follows
static FirstForm objpub;
public SecondForm(FirstForm obj)
{
InitializeComponent();
objpub = obj;
}
private void button1_Click(object sender, EventArgs e)
{
if (objpub != null)
{
objpub.addTabpage();
}
}
If this answer does not meet your problem, then provide your code.
Cheers !

Windows Forms method Form.ShowDialog() strange behaviour

I was trying to show a form(FormChild), with some radio buttons in, just to select, close, and get the value of the selected radio button from the calling form(FormParent). On a click event handler for a Button in FormParent, I just did:
var formChild=newFormChild();
formChild.ShowDialog(this);
All was working great until I decided to handle the CheckedChanged event of one of the RadioButtons inside FormChild:
private void SomeRadioButton_CheckedChanged(object sender, EventArgs e)
{
Close();
}
Now the formChild.ShowDialog(this); did not showed formChild and formChild immediately returns DialogResult.Cancel.
Any explanation on this?
Thanks in advance
The lowest Tab Index radiobutton will be checked by default, If this event handler is assigned to that button it will cause the situation that you are describing.
You can either change your Tab Order or create a Boolean Flag that is set in your Forms Shown EventHandler to keep it from Triggering until you check it again.
public partial class Form1 : Form
{
bool initDone;
public Form1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (initDone)
{
if (((RadioButton)sender).Checked == true)
{
Close();
}
}
}
private void Form1_Shown(object sender, EventArgs e)
{
initDone = true;
}
}
Is there anywhere if your code that sets the value of the RadioButton? If you programmatically set the "Checked" property of the RadioButton, it will fire the event.
In your situation, the event handler contains your Form.Close() so the form never gets the chance to be visible to the user.
Note: Setting the RadioButton.Checked as "true" in the designer will not fire the event.

Silverlight: Closing a ChildWindow when the Overlay is clicked

If the user clicks on the overlay, I want the ChildWindow to automatically close and return the user to the main screen.
Is there a property that controls this? If not, is there a way to attach a click handler to the overlay?
Turns out you can get a reference to the overlay right after it is created. After that it is a simple matter of attaching the event handler.
private void Overlay_MouseButtonDown(object sender, MouseButtonEventArgs e)
{
this.Close();
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var overlay = (Grid)GetTemplateChild("Overlay");
overlay.MouseLeftButtonDown += Overlay_MouseButtonDown;
overlay.MouseRightButtonDown += Overlay_MouseButtonDown;
}

How to set focus on textbox when showing a child window

Upon starting up my silverlight app a child window appears to prompt the user to login. Within in the window I have a username textbox that I want to be focused so that the user can begin typing without focusing it with the mouse first.
This seems like it should work:
public partial class LoginForm : ChildWindow
{
public LoginForm()
{
InitializeComponent();
}
private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
{
txtUsername.Focus();
}
}
I noticed that the Load event happens before the window is rendered which might be the problem, however I don't see an event handler for Rendered or similar.
Edit: Forgot to mention this application is running in the browser.
You need set TabIndex on txtUsername with lower value than other controls on that child window.
Try this:
public partial class LoginForm : ChildWindow
{
public LoginForm()
{
InitializeComponent();
Loaded+=LoginFormLoaded;
}
private void LoginFormLoaded(object sender, RoutedEventArgs e)
{
txtUsername.Focus();
Loaded-=LoginFormLoaded;
}
}
update:
You can also add your event on your TextBox. the textbox has a Loaded event too. If you use this event, you are sure that the TextBox is loaded.
If you are running the application in the browser make sure that the silverlight plugin has focus before calling the focus method of the control.
private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
{
HtmlPage.Plugin.Focus();
txtUsername.Focus();
}
This works on most browsers except I couldn't get it to work on Safari.
Here is something I use:
public partial class LoginForm : ChildWindow
{
public LoginForm()
{
InitializeComponent();
Loaded += (s, e) =>
{
txtUsername.Focus();
};
}
}
And in XAML set your TabIndex to 1
For some reason that I do not have time to figure out, the Child Window in the Silverlight SDK opens, animates, and the focus moves to some unknown place. Most of the solutions suggested on this thread do not work for me because the control receives focus briefly (you can see the focus hit the text box) and then the focus moves to somewhere else (I am guessing that the storyboard on the ChildWindow template has something to do with that). So, I figured out this work around that is actual code that has been implemented and has been proven to work:
public NewChildWindow()
{
InitializeComponent();
this.GotFocus += NewChildWindow_GotFocus;
}
private void NewChildWindow_GotFocus(object sender, RoutedEventArgs e)
{
this.GotFocus -= NewChildWindow_GotFocus;
txtBoxToFocusOn.Focus();
}
Make your textbox TabIndex="0" and in its Loaded event do the "YourtxtBox.Focus()"

tab usercontrol closing event

Hi i have a tab load this usercontrol. when i wish to close this tab, i wish to call this cancelbutton_click event to pop up confirmation on closing, if OK, then close, if Cancel, the tab stays.
if i use Unloaded event, it will pop up twice before closing.
private void UserControl_Unloaded(object sender, RoutedEventArgs e)
{
cancelbutton_click(sender,null);
}
Cancel button:
private void cancelButton_Click(object sender, RoutedEventArgs e)
{
MessageBoxResult objResult = MessageBox.Show("\nAre you sure you want to cancel?", "Cancel Confirmation", MessageBoxButton.OKCancel);
if (objResult == MessageBoxResult.OK)
{
try
{
TabItem tabItem = parentWindow.FindTabItemByName(ControlType.BusinessesContractors.ToString(), false);
this.parentWindow.mainTabControl.Items.Remove(tabItem);
this.parentWindow.statusTextBlock.Text = "Ready";
}
catch (Exception ex)
{
}
}
how to resolve this? thanks
Unloaded is called when the control is already being removed, it's not an event you want to handle often, just create a button which is supposed to close the tab, handle it's click, check if the user wants to cancel via the dialogue and close the tab if he does not.

Resources