custom usercontrol - Bring a form to the front
How to Bring (form_x) to the front. I have a custom usercontrol which opens (form_x) and I use this usercontrol for all of my forms. and now the problem is that the usercontrol opens a new (form_x) instead of Bringing of(form_x) to the front.
my control
namespace template
{
public partial class Background : UserControl
{
IniFile ini = new IniFile(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\config.ini");
public Background()
{
InitializeComponent();
//Console.WriteLine(FilterNumbers("1.0.0.0. beta"));
//Console.WriteLine(FormatFileSize(125463));
}
private void lbl_About_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
frm_about formOptions = new frm_about();
bool isFormOpen = false;
foreach (Form frm in Application.OpenForms)
{
if (frm is frm_about)
{
frm.BringToFront();
isFormOpen = true;
break;
}
}
if (!isFormOpen)
formOptions.Show();
else
formOptions.Dispose();
}
private void ThisRefresh()
{
this.lbl_About.Visible = _About;
this.lbl_About.Enabled = _AboutE;
}
private bool _AboutE;
public bool AboutE
{
get { return _AboutE; }
set { _AboutE = value; ThisRefresh(); }
}
public static string FilterNumbers(string mightContainNumbers)
{
if (mightContainNumbers == null || mightContainNumbers.Length == 0) return "";
StringBuilder builder = new StringBuilder(mightContainNumbers.Length);
foreach (char c in mightContainNumbers)
if (Char.IsNumber(c))
builder.Append(c);
return builder.ToString();
}
开发者_如何学Python public static string FormatFileSize(long fileSize)
{
if (fileSize < 0) throw new ArgumentOutOfRangeException("fileSize");
if (fileSize >= 1024 * 1024 * 1024) return string.Format("{0:########0.00} GB", ((double)fileSize) / (1024 * 1024 * 1024));
else if (fileSize >= 1024 * 1024) return string.Format("{0:####0.00} MB", ((double)fileSize) / (1024 * 1024));
else if (fileSize >= 1024) return string.Format("{0:####0.00} KB", ((double)fileSize) / 1024);
else return string.Format("{0} bytes", fileSize);
}
}
}
//
I think you're problem is here:
frm_about formOptions = new frm_about();
bool isFormOpen = false;
foreach (Form frm in Application.OpenForms)
{
if (frm is frm_about)
{
frm.BringToFront();
isFormOpen = true;
break;
}
}
You're creating a new instance of frm_about
and then checking to see if it's on the list of the Applications open forms. It won't be.
This will mean that isFormOpen
will always be false.
精彩评论