Form not in right position - c#

I'd like to show an instance of a form class for a specific time. The form needs to be topmost and not steal focus. Here is my code:
public class mSplashForm : Form
{
public mSplashForm()
{
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = Color.LightBlue;
this.Opacity = 0.92D;
this.ShowInTaskbar = false;
this.MinimumSize = new System.Drawing.Size(5, 5);
}
}
public static void mSplash(int time = 500)
{
mSplashForm SF = new mSplashForm();
Application.EnableVisualStyles();
SF.Width = 500;
SF.Height = 100;
SF.Left = 500;
SF.Top = 500;
SetWindowPos(SF.Handle, HWND_TOPMOST, SF.Left, SF.Top, SF.Width, SF.Height, SWP_NOACTIVATE);
ShowWindow(SF.Handle, mEnumShowWindowCommands.ShowNoActivate);
Application.DoEvents();
Thread.Sleep(time);
SF.Close();
}
It works, but the form is not shown in the right position defined using Top and Left parameters. What is wrong please?

You've got your form set to start in FormStartPosition.WindowsDefaultLocation. Add this into your mSplash function:
SF.StartPosition = FormStartPosition.Manual;
This is why it's trying to position successively down the page (as per your comment) on each opening.

set the start position to manual:
this.StartPosition = FormStartPosition.Manual;

Try this
SF.StartPosition = FormStartPosition.Manual;
SF.Width = 500;
SF.Height = 100;
SF.Left = 500;
SF.Top = 500;

Related

Windows Form Cannot be Displayed CenterScreen

I used this code for the secondary form to act / be like mdi, this is windows form C#
frmFontLoad fontLoad = new frmFontLoad();
fontLoad.TopLevel = false;
Controls.Add(fontLoad);
fontLoad.Show();
fontLoad.BringToFront();
but the result then the form was loaded is like this refer to screenshot:
Form
Code on Form Load:
private void frmLabelPNInput_Load(object sender, EventArgs e)
{
this.StartPosition = FormStartPosition.CenterScreen;
Printer_SerialPort = new SerialPort(ConfigurationManager.AppSettings["Printer_com_port"]);
btnOk.Enabled = false;
}
how can i make this secondary form to be displayed at the center?
frmProgress progress = new frmProgress();
progress.Left = (this.ClientSize.Width - progress.Width) / 2;
progress.Top = (this.ClientSize.Height - progress.Height) / 2;
This solved my problem, thanks

(design) 2 Elements blocking each other

simple question here:
I have a problem with one of my labels.
My problem is that the (!!!) label corresponding to the textbox "Datei Name" is being blocked by said textbox. Is there a setting that I have missed that puts said label behind/in front of the textbox?
How the (this part of the) program works:
When something is incorrectly entered in one of the textboxes a label containing (!!!) and a Messagebox show up which tell the user where the error is located.
As i mentioned in the comment to the question, i'd use ErrorProvider instead of spending time to write code to manipulate controls on the form.
Here is MSDN example: How to: Display Error Icons for Form Validation with the Windows Forms ErrorProvider Component
How it works?
When the form is NOT valid, you'll see:
When you move cursor over icon, you'll see error details:
If you've got LinqPad, you can use this script to check how it works:
void Main()
{
MyForm mf = new MyForm();
mf.Show();
}
// Define other methods and classes here
public class MyForm: Form
{
private TextBox TxtDateiName = null;
private TextBox TxtDateiNr = null;
private Label Label1 = null;
private Label Label2 = null;
private Button BtnValidate = null;
Dictionary<Control, ErrorProvider> ValidatedControls = null;
public MyForm()
{
Initialize();
}
private void Initialize()
{
this.Size = new Size(250, 180);
this.MinimizeBox = false;
this.MaximizeBox = false;
this.Text = "ErrorProvider Example";
Label1 = new Label(){Text="Datei Name:", Location =new Point(10,12), AutoSize = true};
Label2 = new Label(){Text="Datei Nr:", Location =new Point(10,42), AutoSize = true};
TxtDateiName = new TextBox(){Name="TxtDateiName", Size= new Size(140,24), Location =new Point(78,10)};
TxtDateiNr = new TextBox(){Name="TxtDateiNr", Size= new Size(140,24), Location =new Point(78,42)};
BtnValidate = new Button(){Size = new Size(220, 48), Text = "Validate", Location = new Point(10, 78) };
BtnValidate.Click += BtnValidate_Click;
this.Controls.Add(Label1);
this.Controls.Add(Label2);
this.Controls.Add(TxtDateiName);
this.Controls.Add(TxtDateiNr);
this.Controls.Add(BtnValidate);
ValidatedControls = new Dictionary<Control, ErrorProvider>();
ValidatedControls.Add(TxtDateiName, new ErrorProvider(this));
ValidatedControls.Add(TxtDateiNr, new ErrorProvider(this));
ValidatedControls[TxtDateiName].SetIconAlignment(TxtDateiName, ErrorIconAlignment.MiddleRight);
ValidatedControls[TxtDateiName].SetIconPadding (TxtDateiName, 2);
ValidatedControls[TxtDateiName].BlinkRate = 1000;
ValidatedControls[TxtDateiName].BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
ValidatedControls[TxtDateiNr].SetIconAlignment(TxtDateiName, ErrorIconAlignment.MiddleRight);
ValidatedControls[TxtDateiNr].SetIconPadding (TxtDateiName, 2);
ValidatedControls[TxtDateiNr].BlinkRate = 1000;
ValidatedControls[TxtDateiNr].BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
}
private void BtnValidate_Click(object sender, EventArgs e)
{
bool ans = true;
foreach(var k in ValidatedControls.Keys)
{
switch(k.Name)
{
case "TxtDateiName":
ans = IsValidName(((TextBox)k).Text);
ValidatedControls[k].SetError(k, ans ? "" : "Name is wrong!");
break;
case "TxtDateiNr":
ans = IsValidNumber(((TextBox)k).Text);
ValidatedControls[k].SetError(k, ans ? "" : "Number is wrong!");
break;
}
}
ans = ValidatedControls.All(kvp=>kvp.Value.GetError(kvp.Key)=="");
MessageBox.Show(ans ? "Form is valid!" : "Form contains errors!", "Information");
if(ans) this.Close();
}
private bool IsValidName(string dName)
{
return Regex.IsMatch(dName, #"^(\w{3,})$");
}
private bool IsValidNumber(string dNumber)
{
return Regex.IsMatch(dNumber, #"^(\d{3,5})$");
}
}
Do not forget to add references (F4) to: System.Windows.Forms.dll
and then add the following namespaces:
System.Drawing
System.Windows.Forms
Final note:
This is just an example. So, the code is not optimized.

Dynamically Adding Class of Controls

I am having trouble with dynamically adding a class of controls that should when working look like this:
When a new one is added it should appear in the left panel under the toolstrip.
So far I am having trouble making them appear (The one in the middle is just the design I made).
Here is the code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Problem Occurs Here
EquationBox[] EquationBoxArray = new EquationBox[12];
for (int x = 0; x < 12; x++)
{
EquationBoxArray[x] = new EquationBox();
ActiveForm.Controls.Add(EquationBoxArray[x].mainPanel);
ActiveForm.Controls.Add(EquationBoxArray[x].colorPanel);
}
}
private void add_line_Click(object sender, EventArgs e) //Add Line
{
}
private void clear_Click(object sender, EventArgs e) //Clear Lines
{
}
}
public class EquationBox
{
public Panel colorPanel = new Panel();
public Panel mainPanel = new Panel();
public TextBox equationBox = new TextBox();
public CheckBox isVisibleBox = new CheckBox();
public EquationBox()
{
mainPanel.Size = new Size(200, 72);
colorPanel.Size = new Size(33, 72);
mainPanel.Location = new Point(50, 50);
colorPanel.Location = new Point(50, 50);
colorPanel.BackColor = Color.Red;
}
}
The problem occurs here:
//Problem Occurs Here
EquationBox[] EquationBoxArray = new EquationBox[12];
for (int x = 0; x < 12; x++)
{
EquationBoxArray[x] = new EquationBox();
ActiveForm.Controls.Add(EquationBoxArray[x].mainPanel);
ActiveForm.Controls.Add(EquationBoxArray[x].colorPanel);
}
When I run it, it return with:
Additional information: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
And even before that started happening, the EqautionBox wouldn't appear.
Thanks in advance, this is really troubling me.
For the constructor of EquationBox:
public EquationBox()
{
mainPanel.Size = new Size(200, 72);
colorPanel.Size = new Size(33, 72);
mainPanel.Location = new Point(50, 50);
colorPanel.Location = new Point(50, 50);
colorPanel.BackColor = Color.Red;
}
First, your control appeared, but mainPanel is overlap colorPanel and you can't see mainPanel (same BG color as your form), so swap which added first solved
EquationBox[] EquationBoxArray = new EquationBox[12];
for (int x = 0; x < 12; x++)
{
EquationBoxArray[x] = new EquationBox();
this.Controls.Add(EquationBoxArray[x].colorPanel);
this.Controls.Add(EquationBoxArray[x].mainPanel);
}
I am using this.Controls, not sure about the ActiveForm.Controls part, maybe on constructing, your Form1 is not the active one, so error occured.
Ps: I suggest add colorPanel to mainPanel, and only add mainPanel to Form. And UserControl is a good solution here as Steve Wellens said.
There are various issues with EquationBox the TextBox and CheckBox are not in the panel. It would be easier to make it a UserControl.
Then to do the positioning use a FlowLayoutPanel.

How to add one form into another form winform project

i tried to create a form instance with in another form and then add that form into main form. but the form which i added that is not showing. i want to show that form at center at top of all controls.
here is my code
BBA.Controls.ExecludeSpecialist ucExecludeSpecialist = null;
Form frmContainer = null;
private void btnExclude_Click(object sender, EventArgs e)
{
if (ucExecludeSpecialist != null)
{
if (frmContainer != null)
{
frmContainer.Controls.Remove(ucExecludeSpecialist);
ucExecludeSpecialist = null;
}
}
if (frmContainer != null)
{
this.Controls.Remove(frmContainer);
frmContainer = null;
}
frmContainer = new Form();
frmContainer.ControlBox = false;
frmContainer.StartPosition = FormStartPosition.Manual;
frmContainer.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
ucExecludeSpecialist = new BBA.Controls.ExecludeSpecialist();
ucExecludeSpecialist.SaveClicked +=
new BBA.Controls.ExecludeSpecialist.SaveComplete(OnSaveClicked);
ucExecludeSpecialist.CloseClicked +=
new BBA.Controls.ExecludeSpecialist.CloseComplete(OnCloseClicked);
ucExecludeSpecialist.BringToFront();
frmContainer.Height = ucExecludeSpecialist.Height;
frmContainer.Width = ucExecludeSpecialist.Width;
//frmContainer.Top = this.Height - frmContainer.Height / 2;
//frmContainer.Left = this.Height - frmContainer.Height / 2;
frmContainer.BringToFront();
frmContainer.TopLevel = false;
frmContainer.Controls.Add(ucExecludeSpecialist);
this.Controls.Add(frmContainer);
}
please guide me how to show that form on top of all control of another form at center. thanks
If I understand your comment correct, your problem is that a DataGrid overlays your recently added form? Try :
After you have add
frmContainer.Show();
your Form shoul be visible. After that you should solve your problem, if you call ucExecludeSpecialist.BringToFront(); after calling frmContainer.Show();
Example :
private void button1_Click(object sender, EventArgs e)
{
frmContainer = new Form();
frmContainer.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
frmContainer.Height = this.Height / 2;
frmContainer.Width = this.Width / 2;
frmContainer.BackColor = Color.Red;
frmContainer.TopLevel = false;
this.Controls.Add(frmContainer);
frmContainer.Show();
frmContainer.BringToFront();
}

C# - Transparent modal form on a window

I want to use fully transparent Modal form in my application, with being able to fill it with partially-transparent image; For this, I used to remove all visible elements from the form and got the code below.
class WinScreenshotWindow : Form
{
public WinScreenshotWindow()
{
// Create from without erasing background with a color
// Going not to use transparent form instead, it will produce context menu bugs in textboxes for child form
this.SuspendLayout();
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.FormBorderStyle = FormBorderStyle.None;
this.StartPosition = FormStartPosition.Manual;
this.ControlBox = false;
this.Visible = false;
this.Size = new Size(100, 100);
this.Location = new Point(200, 200);
this.ResumeLayout();
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// Erase Background Windows message:
}
protected override void OnPaint(PaintEventArgs e)
{
Rectangle clientRect = e.ClipRectangle;
e.Graphics.FillRectangle(Brushes.Transparent, clientRect);
}
}
static void Main()
{
Form form = new Form();
form.Size = new Size(400, 400);
form.Show();
var ww = new WinScreenshotWindow();
ww.ShowDialog(form);
}
But the result is something strange:
When I remove filling in OnPaint(), it is not visible at all.
The question is - why does this happen? If the background is transparent why do it shows the form in such way? And what can be done in this situation?
Any help appreciated.
Wouldn't it be easier to open a borderless form with a red backcolor and set the TransparencyKey = red?

Categories