C# Winforms anchoring doesn't work when started maximized - c#

I have windows form with some form with buttons and other components. Buttons are anchored to Bottom and Right. When the application is started as maximized, the buttons don't appear in the corner of the form as they should but remain as if they were anchored to Top and Left. If I unmaximize this form, they remain as far away from the bottom right as in maximized. However, if the form is started normal, not maximized, then everything is ok and the buttons are in the corner. I have the same buttons in other forms and there everything works good, only this one is broken. How can I fix this?
Designer:
Form started maximized:
Form unmaximized:
Now I change the WindowState from Maximized to Normal:
And when I maximize:
And this happens only in this form, in other forms, this solution works fine, it doesn't matter if maximized or normal. What is broken in this particular form?
Designer.cs code for this form is the following (the whole file has over 600 lines, so I selected the important ones:
this.buttonAneks = new System.Windows.Forms.Button();
(...)
// buttonAneks
//
this.buttonAneks.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonAneks.BackColor = System.Drawing.SystemColors.Window;
this.buttonAneks.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonAneks.Location = new System.Drawing.Point(715, 585);
this.buttonAneks.Name = "buttonAneks";
this.buttonAneks.Size = new System.Drawing.Size(75, 23);
this.buttonAneks.TabIndex = 103;
this.buttonAneks.Text = "Aneksy";
this.buttonAneks.UseVisualStyleBackColor = false;
this.buttonAneks.Click += new System.EventHandler(this.buttonAneks_Click);
(...)
this.Controls.Add(this.buttonAneks);
this.Controls.SetChildIndex(this.buttonAneks, 0);
(...)
private System.Windows.Forms.Button buttonAneks;

Turns out that the problem was the Adobe PDF Reader control axAcroPDF, which was present in this particular form, and for unknown reasons, it caused such behavior. Removing it solved the problem.

Related

CefSharp WinForms - Flickering when page loading

I am using CefSharp.WinForms version 84.4.10.
My application is a wip game launcher for the games I make.
Screenshot of the launcher.
There is a Main form that loads and unloads child forms when buttons are pressed. The child forms contain a panel stretched to fill the whole child form. The CefSharp is initialized on the panel.
Here's the code that does that:
public CefSharp.WinForms.ChromiumWebBrowser browser;
private void Changelog_Load(object sender, EventArgs e)
{
browser = new CefSharp.WinForms.ChromiumWebBrowser("https://aerial-knightstudios.com/store/akslauncher/")
{
Dock = DockStyle.Fill,
Size = new Size(600, 600),
Location = new Point(200, 200),
};
this.panelChangelogPage.Controls.Add(browser);
}
When the child form is enabled, a black box appears that fills half the form then it flickers white then the webpage loads.
So far I have tried the following:
I have turned on double buffering by adding DoubleBuffered = true; inside public FormMain() and this made the flickering less intense, but it still flickers a lot.
I also changed [STAThread] in program.cs to [MTAThread]. This also helped, but very slightly.
Any solutions are welcome.

Maximizing a form, and disabling user to resize the form

I am trying to create a form that is maxed out, without allowing the user to resize it. I tried to maximize the FormWindowState, and remove the minimize and maximize button. By setting this.MimimumSize and this.MaximumSize to this.Size (the maximized size), this should account to a maximized form.
Yet, when I run it, the form becomes a really small square. Any thoughts on how I can fix this?
public partial class Testscherm : Form
{
public Testscherm()
{
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MinimumSize = this.Size;
this.MaximumSize = this.Size;
this.MaximizeBox = false;
this.MinimizeBox = false;
InitializeComponent();
}
}
Try calling InitializeComponent() first, then add any statements which changes attributes/properties of the form. Otherwise, the designer generated code may undo any of changes you did beforehand.
Second, Form.Size does not deliver the form's size in maximized state. You could instead iterate over
System.Windows.Forms.Screen.AllScreens
then get the actual screen size along the lines of
System.Windows.Forms.Screen.AllScreens.First().WorkingArea.Size;
and assign it to this.Size;
Another problem here is, as soon as you assign this, this.MaximizeBox = false, Winforms forbids WindowState to be FormWindowState.Maximized. This is obviously "by design". What you probably want here is to use
this.FormBorderStyle = FormBorderStyle.None;
which does not only remove the maximum and minimum buttons, but also the close button. If you need such button, consider to add your own close button to the window.
Putting this all together gives
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = FormBorderStyle.None;
this.Size = System.Windows.Forms.Screen.AllScreens.First().WorkingArea.Size;
this.MinimumSize = this.Size;
this.MaximumSize = this.Size;
But: are you sure it is a good idea what you are trying there? How will this behave on a machine with two monitors of different resolution for example? It may be ok for a program which uses specific hardware and works as a dedicated software which takes over the machine exclusively (something like a Kiosk mode). Note the window may still be moved around using certain Win-<Key> keyboard shortcuts.

Pop-up modal dialog with grey background [duplicate]

This question already has answers here:
Center a C# Windows Form inside another window
(1 answer)
Show a child form in the centre of Parent form in C#
(19 answers)
Show Dialog box at center of its parent
(5 answers)
Closed 1 year ago.
I want to make classic a modal Window with dark background.
I don't know why this is so hard to make it. I tried a lot way to do it, however it just doesn't fit with my needs.
I have 10+ modal Windows and they are customized Forms which contain grids, charts, etc.
They are not 'OK, No, Cancel' stuff.
My code basically creates another Form with black background between parent Form and modal Form:
Form f = new Form();
f.BackColor = Color.Black;
f.Size = this.Size;
f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f.StartPosition = this.StartPosition;
f.Opacity = 0.6;
f.Show();
notificationSGA nsga = new notificationSGA(Cursor.Position);
nsga.ShowDialog();
f.Dispose();
f.Close();
Above code works perfectly fine. However if I move the parent (master) Form to somewhere instead of center of the screen, the black Form still appears at the center of the screen, not centered to the parent Form.
How can I solve my issue?
Note: This is not duplicated topic with below question:
How to show a pop up message with dark background
Change the .StartPosition to .Manual so that you can set .Location to where your reference form is.
Also, change the owner in the .Show() method for both new Forms.
Form f = new Form();
//(...)
f.Size = this.Size;
f.StartPosition = FormStartPosition.Manual;
f.Location = this.Location;
//(...)
f.Show(this);
using (var nsga = new notificationSGA(Cursor.Position)) {
nsga.StartPosition = FormStartPosition.CenterParent;
nsga.ShowDialog(f);
}
f.Dispose();

C# MDI Child Form - size to half of work area

We're using MDI for our program.
Yes, I know, use something else, MDI is bad etc etc...the client is more comfortable with the MDI style of UI so we're sticking to that. So far we have found no need for anything like TDI or SDI anyway.
The one thing we would like to be able to do is to automatically size an MDI child to be half of the MDI client area's size; kind of similar to how in Windows 7/8 you can drag a window to one side of the screen and it will resize it to fill half of the desktop.
How can I do this?
To clarify, I'm not looking for docking, and obviously resizing a form is easy enough, I'm more interested in how to catch when the user drags the form to the edge of the MDI client area, and somehow centralising that logic so I'm not duplicating it in every form. Showing an outline of where the form will end up would be pleasant as well (again, similar to Win7/8 resizing).
Thanks
You are in for a world of hurt.
But here is how to get started:
Form f = new Form();
f.ResizeEnd += f_ResizeEnd;
f.MdiParent = this;
f.Show();
void f_ResizeEnd(object sender, EventArgs e) {
MdiClient mc = this.Controls.OfType<MdiClient>().First();
Form f = sender as Form;
if (f.Right >= mc.ClientSize.Width) {
f.SetBounds(mc.ClientSize.Width / 2, 0,
mc.ClientSize.Width / 2, mc.ClientSize.Height);
}
}

Getting a text area in the system tray

i was just wondering what i need to research to be able to have a programme that is in the system tray, when the user clicks the programme icon, just above the system tray a small text area appears allowing the user to type in a search condition. There is plenty of resources for c# and getting your programme in the system tray, but then it just opens as normal, which is not quite what i am looking for.
Thanks
One way to accomplish this is to use a standard WinForms window which contains a single text box and has no border. This window can then be displayed and positioned as normal (likely using many of the existing samples) but will appear as a floating text box.
var form = new MyTextBoxForm();
form.FormBorderStyle = BorderStyle.None;
form.StartPosition = FormStartPosition.Manual;
// position the form
form.ShowDialog();
Handle the NotifyIcon.Click event and show your form in the desired location.
For example:
var screen = Screen.PrimaryScreen;
form.Left = screen.WorkingArea.Right - form.Width;
form.Top = screen.WorkingArea.Bottom - form.Height;
Maybe with this Make your program in the system + add a menu you could try editing the menu, like you'd do a regular menu with toolstrips.... and change the label by a textbox.
Just a random idea.

Categories