pop up user control not rendering correctly when created - c#

I have a class which handles a sort of user control popup, which works by inheriting from System.Windows.Forms.ToolStripDropDown. This has worked for the popup type i had currently, of which i shall detail below;
First, is the class which i use to hold the user control in a popup type style;
public class PopupWindow : System.Windows.Forms.ToolStripDropDown
{
private System.Windows.Forms.Control _content;
private System.Windows.Forms.ToolStripControlHost _host;
public PopupWindow(System.Windows.Forms.Control content)
{
//Basic setup...
this.AutoSize = false;
this.DoubleBuffered = true;
this.ResizeRedraw = true;
this.BackColor = content.BackColor;
this._content = content;
this._host = new System.Windows.Forms.ToolStripControlHost(content);
//Positioning and Sizing
this.MinimumSize = content.MinimumSize;
this.MaximumSize = content.Size;
this.Size = content.Size;
content.Location = Point.Empty;
//Add the host to the list
this.Items.Add(this._host);
}
}
as we can see here, i'm simply passing a control to it, and letting it do the work. When using this on an "onclick" popup, like so, it works fine;
public void Popup(object sender, MouseEventArgs e, other params)
{
DevicePopup popupDevice = new DevicePopup();
//do stuff to the control here before displaying
PopupWindow popup = new PopupWindow(popupDevice);
popup.Show(Cursor.Position);
}
and calling it like so;
this.Controls[btnAdd.Name].MouseClick += (sender, e) =>
{
int index = temp;
generatePopup.Popup(sender, e, mDevices[index], this);
};
doing this successfully creates the popup user control at my mouse click, as intended.
However, i'm now trying to use a second type of popup which spawns when something happens. Below is my new popup class, and calling it;
public void AlarmNotificationPopup(IDeviceInterface device)
{
try
{
AlarmNotification ANotification = new AlarmNotification();
//do stuff to the control again before displaying
PopupWindow popup = new PopupWindow(ANotification);
popup.Show(100, 100);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
AlarmNotificationPopup(device);
However, this popup doesn't render / create properly, and looks like so;
I'm not entirely sure how to fix this. anyone have any ideas?

A couple of things to try:
I don't think you need your "Basic Setup", so comment that out. Also, try setting your margins and paddings:
public PopupWindow(Control content) {
// Basic setup...
// this.AutoSize = false;
// this.DoubleBuffered = true;
// this.ResizeRedraw = true;
// this.BackColor = content.BackColor;
this._content = content;
this._host = new ToolStripControlHost(content);
this._host.Margin = new Padding(0);
this._host.Padding = new Padding(0);
this.Padding = new Padding(0);
this.Margin = new Padding(0);
//Positioning and Sizing
this.MinimumSize = content.MinimumSize;
this.MaximumSize = content.Size;
this.Size = content.Size;
content.Location = Point.Empty;
//Add the host to the list
this.Items.Add(this._host);
}
Make sure your AlarmNotification has its MinimumSize property set.
If that doesn't help solve the problem, then you probably need to document what the AlarmNotification class is doing.

Related

Generate a New Guid into a Textbox area using a button. (C#)

Looking for help.
I have a form which generates fields into a Class extends groupbox.
I am creating a button to clear and then generate a new GUID into a text box but I can seem to access my textbox which has been creating in the Initialize method.
I created a list to store input.
private List<InputSetItem> _inputSetItems = new List<InputSetItem>();
This is where the textbox is created:
public void initialize()
{
//balanceIdentifier
var balanceIdentifierSet = InputGenerator.GenerateInputControl(this, typeof(Guid), "BalanceIdentifier");
Controls.Add(balanceIdentifierSet.Label);
Controls.Add(balanceIdentifierSet.Input);
balanceIdentifierSet.Label.Left = 820;
balanceIdentifierSet.Input.Left = 1050;
balanceIdentifierSet.Input.Width = 400;
balanceIdentifierSet.Label.Top = 20;
balanceIdentifierSet.Input.Top = 20;
_inputSetItems.Add(balanceIdentifierSet);
// btn_Guid
this.btn_Guid.BackColor = System.Drawing.Color.Blue;
this.btn_Guid.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point);
this.btn_Guid.ForeColor = System.Drawing.Color.White;
this.btn_Guid.Location = new System.Drawing.Point(1455, 19);
this.btn_Guid.Name = "btn_Guid";
this.btn_Guid.Size = new System.Drawing.Size(75, 26);
this.btn_Guid.TabIndex = 3;
this.btn_Guid.Text = "GENERATE";
this.btn_Guid.UseVisualStyleBackColor = false;
this.btn_Guid.ImageAlign = ContentAlignment.MiddleRight;
this.btn_Guid.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
this.btn_Guid.FlatStyle = FlatStyle.Flat;
this.Controls.Add(btn_Guid);
this.btn_Guid.Click += new System.EventHandler(this.generateRandomGuid);
}
This is the UI
This is my Generate Method, I cant wrap my head around accesses and updating this textBox
private void generateRandomGuid(object sender, EventArgs e)
{
var delBal = new InputSetItem();
Guid randomGuid = Guid.NewGuid();
//delBal.btn_Guid.Input = randomGuid.ToString();
//delBal.Input.Text;
}
So figured it out.
I had to create a Control instances
private Control _idemp;
Add the textbox assigning it
var IdempotencyRef = InputGenerator.GenerateInputControl(this, typeof(Guid), "IdempotencyReference");
_idemp = IdempotencyRef.Input;
Controls.Add(IdempotencyRef.Label);
Controls.Add(_idemp);
And then updating the method
private void generateRandomGuid(object sender, EventArgs e)
{
Guid randomGuid = Guid.NewGuid();
_idemp.Text = randomGuid.ToString();
}

Newly added controls won't paint to Form

So I have a Form that is a scale A4 page that allows users to drag and drop controls on the form for printing.
IE where ever the control is on the form its location is used to print the controls data, eg: File name or image, to that point of an A4 page.
However I have created a number of templates for the form that sets the controls in certain locations and adds in any missing controls. When the templates are selected any extra controls don't show on the form even though I call the Invalidate() method.
Here is my code for the method that adds the controls to the Form:
private void standardIDToolStripMenuItem_Click(object sender, EventArgs e)
{
selectedID = true;
selectedInvoice = false;
selectedLetter = false;
lblName.Visible = true;
lblDOB.Visible = true;
lblUID.Visible = true;
lblName.Location = new Point(200, 100);
lblDOB.Location = new Point(200, 125);
lblUID.Location = new Point(200, 150);
lblName2.Text = lblName.Text;
lblName2.Location = new Point(60, 750);
lblName2.Enabled = true;
lblName2.Visible = true;
lblDOB2.Text = lblDOB.Text;
lblDOB2.Location = new Point(60, 775);
lblDOB2.Enabled = true;
lblDOB2.Visible = true;
lblUID2.Text = lblUID.Text;
lblUID2.Location = new Point(60,800);
lblUID2.Enabled = true;
lblUID2.Visible = true;
hidden1.Location = new Point(300, 100);
DOBHidden.Location = new Point(300, 125);
UIDHidden.Location = new Point(300, 150);
#region ID Background placeholder
PictureBox backPic = new PictureBox();
backPic.Location = new Point(24, 48);
backPic.ForeColor = System.Drawing.Color.PaleGreen;
backPic.Size = new Size(504, 176);
backPic.Visible = true;
backPic.Show();
backPic.SendToBack();
this.Invalidate();
#endregion
}
Why will the new controls not appear on the form when I have called the Invalidate() method to force it to repaint?
It seems that you don't add them to Controls:
please try this on every control after you have specified location and the rest of the control initialization:
this.Controls.Add(lblName)
Mong Zhu seems to be right and I also suggest you to catch a glimpse of some kind of report designer if you can use some third-part libaries ( I'm not sure if winforms privides something like DevExpress reports for example)
I guess that it will be helpful with stuff you are doing in your project.

C# - How to close created form window from other form

I'm making a function in Form1 that make a new form.
private void makeForm(String option) {
Form formWin = new Form();
formWin.TopMost = true;
formWin.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
formWin.Size = new Size(500,600);
formWin.StartPosition = FormStartPosition.Manual;
formWin.Location = new Point(1366 - formWin.Size.Width , 768 - formWin.Size.Width);
formWin.BackColor = Color.White;
formWin.TransparencyKey = Color.White;
var formWinModel = new PictureBox
{
Name = "formWin",
Size = new Size(formWin.Size.Width, formWin.Size.Height),
SizeMode = PictureBoxSizeMode.Zoom,
Image = Trans.Properties.Resources.Form_Special,
Location = new Point(0, 0),
};
formWin.Controls.Add(formWinModel);
if (option == "show")
{
formWin.Show();
}
if (option == "exit") {
formWin.Visible = false;
formWin.Close();
formWin.Dispose();
}
}
When I used makeForm("show") it will create formWin form. But how to close formWin form. I used makeForm("exit") but it doesn't work.
Thanks
You have created a Modal window, which means the previous form, (the one that you opened formWin from) wont be accessible while formWin is active. If you want both forms to be accessible at the same time, you should use formWin.Show() instead.
Your method wont work anyways because you're looking for a local variable (form) to close which is not possible, you should declare Form formWin outside of your method and save the value to this variable. When you later want to close it. But that's gonna cause some other problems such as what if you wanted to create multiple forms at the same time? Which questions your whole logic, why even close the Form from your previous Form?
private Form formWin = null;
private void makeForm(String option)
{
if(formWin == null)
{
formWin = new Form();
formWin.TopMost = true;
formWin.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
formWin.Size = new Size(500, 600);
formWin.StartPosition = FormStartPosition.Manual;
formWin.Location = new Point(1366 - formWin.Size.Width, 768 - formWin.Size.Width);
formWin.BackColor = Color.White;
formWin.TransparencyKey = Color.White;
var formWinModel = new PictureBox
{
Name = "formWin",
Size = new Size(formWin.Size.Width, formWin.Size.Height),
SizeMode = PictureBoxSizeMode.Zoom,
Image = Trans.Properties.Resources.Form_Special,
Location = new Point(0, 0),
};
formWin.Controls.Add(formWinModel);
}
if (option == "show")
{
formWin.Show();
}
if (option == "exit")
{
formWin.Visible = false;
formWin.Close();
formWin.Dispose();
formWin = null;
}
}
You should create formWin varibale in out makeForm function. I hope it help you.

C# Winforms App requires two clicks on Button_Click Event

The button requires two clicks to fire up the event. Here is an image and the code.There is a combobox which triggers the button with different items, but when I click the button to show an item in a panel on the page, I have to click it twice so it can trigger the event. After selecting an item once by twice-clicking it, every next time i click it works with one click, just like it should.
Here is the image of the combobox which triggers the button
And there is the code :
namespace Carbon
{
public partial class ucAnaliza : MetroFramework.Controls.MetroUserControl
{
static ucAnaliza _instance;
public static ucAnaliza Instance3
{
get
{
if (_instance == null)
_instance = new ucAnaliza();
return _instance;
}
}
public MetroFramework.Controls.MetroPanel MetroAnaliza
{
get { return mPanelAnaliza; }
set { mPanelAnaliza = value; }
}
public ucAnaliza()
{
InitializeComponent();
}
private void ucAnaliza_Load(object sender, EventArgs e)
{
}
private void mPotvrdiElementi_Click(object sender, EventArgs e)
{
switch (((ComboBox)mDropAnaliza).SelectedItem.ToString())
{
case "Главна рамка":
_instance = this;
ucGlavna uc = new ucGlavna();
uc.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc);
break;
case "Челна рамка":
_instance = this;
ucCelna uc2 = new ucCelna();
uc2.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc2);
break;
case "Подолжна рамка":
_instance = this;
ucPodolzna uc3 = new ucPodolzna();
uc3.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc3);
break;
}
}
}
}
Here is the code from the designer for the button :
// mPotvrdiElementi
//
this.mPotvrdiElementi.BackColor = System.Drawing.Color.Transparent;
this.mPotvrdiElementi.CausesValidation = false;
this.mPotvrdiElementi.Cursor = System.Windows.Forms.Cursors.Hand;
this.mPotvrdiElementi.ForeColor = System.Drawing.SystemColors.MenuBar;
this.mPotvrdiElementi.Image = global::Carbon.Properties.Resources.Checked_Checkbox_24px;
this.mPotvrdiElementi.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.mPotvrdiElementi.ImageSize = 24;
this.mPotvrdiElementi.Location = new System.Drawing.Point(758, 34);
this.mPotvrdiElementi.Name = "mPotvrdiElementi";
this.mPotvrdiElementi.Size = new System.Drawing.Size(80, 25);
this.mPotvrdiElementi.Style = MetroFramework.MetroColorStyle.Orange;
this.mPotvrdiElementi.TabIndex = 4;
this.mPotvrdiElementi.TabStop = false;
this.mPotvrdiElementi.Text = "Потврди";
this.mPotvrdiElementi.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.mPotvrdiElementi.UseCustomBackColor = true;
this.mPotvrdiElementi.UseCustomForeColor = true;
this.mPotvrdiElementi.UseSelectable = true;
this.mPotvrdiElementi.UseStyleColors = true;
this.mPotvrdiElementi.Click += new System.EventHandler(this.mPotvrdiElementi_Click);
I know it is a long time ago but I was having the same problem...
But I found a solution to the problem and is working every time and not killing the usability.
private int focusFlag = 0;
private void MainForm_MouseEnter(object sender, EventArgs e)
{
if (focusFlag < 1)
{
this.FocusMe();
++focusFlag;
}
}
This will not always try to focus on that form when trying to go to other forms or something else, it will just focus once and that is enough... after that it will behave normally :)
It seems the MetroForm doesn´t get Focus until you click within the form and it is just a bug from the developers of the MetroFramework when using certain Metro Controls within the Form.
I have seen others posting the same problem when they are using the MetroFramework.
Hopefully this will help.

Combobox closes before i can select a checkbox

I'm currently working in VS 2012.
.NET 4.5 and working on an mmc snap-in. (i know right?!)
so i followed this topic:
Is there a simple way to implement a Checked Combobox in WinForms
as i want something similar to the scheduled task manager.
But that solution does not seem to work for me.
the listview pops up but when i try to click on a checkbox in my listview.
It gives me a big middle finger and closes my dropdown.
is there any way i can suppress the combobox's "focus lost" close event?
i can, not hide the list but then it never hides.
For Example:
// designer class
//
// comboBox1
//
this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.comboBox1.DropDownHeight = 1;
this.comboBox1.DropDownWidth = 1;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.IntegralHeight = false;
this.comboBox1.Location = new System.Drawing.Point(256, 371);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(238, 21);
this.comboBox1.TabIndex = 5;
this.comboBox1.DropDown += new System.EventHandler(this.comboBox1_DropDown);
this.comboBox1.DropDownClosed += new System.EventHandler(this.comboBox1_DropDownClosed);
//
// lstWeekDays
//
this.lstWeekDays.CheckBoxes = true;
this.lstWeekDays.Location = new System.Drawing.Point(50, 63);
this.lstWeekDays.Name = "lstWeekDays";
this.lstWeekDays.Size = new System.Drawing.Size(263, 97);
this.lstWeekDays.TabIndex = 13;
this.lstWeekDays.Tag = "lstlstWeekDays";
this.lstWeekDays.UseCompatibleStateImageBehavior = false;
this.lstWeekDays.View = System.Windows.Forms.View.SmallIcon;
this.lstWeekDays.Visible = false;
// Code behind
public Form1()
{
InitializeComponent();
this.lstWeekDays.Items.Add("Monday");
this.lstWeekDays.Items.Add("Tuesday");
this.lstWeekDays.Items.Add("Wednesday");
this.lstWeekDays.Items.Add("Thursday");
this.lstWeekDays.Items.Add("Friday");
this.lstWeekDays.Items.Add("Saturday");
this.lstWeekDays.Items.Add("Sunday");
}
private void comboBox1_DropDown(object sender, EventArgs e)
{
lstWeekDays.Visible = true;
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
lstWeekDays.Visible = false;
}
Add the checkboxes to this list instead of the panel.

Categories

Resources