Combobox closes before i can select a checkbox - c#

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.

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();
}

Close previous windows form when clicked other button

i try to close windows form in mdiparent when i click other button, the result is when i click other button, it still appear from the back of new window. so how can i handle this?
private void btn_ic_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
SelectIC ss = new SelectIC();
ss.MdiParent = this;
ss.Show();
Detail aa = new Detail();
aa.MdiParent = this;
aa.Close();
btn_ic.Enabled = false;
btn_cat.Enabled = true;
}
private void btn_cat_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
Detail aa = new Detail();
aa.MdiParent = this;
aa.Show();
SelectIC ss = new SelectIC();
ss.MdiParent = this;
ss.Close();
btn_cat.Enabled = false;
btn_ic.Enabled = true;
}
You're making new instance of form and then closing it. That way you're not closing existing window but creating new (invisible) one and closing it. You should find existing window in collection of MdiChildren and then close it. Something like this:
private void btn_ic_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
SelectIC ss = new SelectIC();
ss.MdiParent = this;
ss.Show();
var detailForm = this.MdiChildren.FirstOrDefault(f => f.GetType() == typeof(Detail));
detailForm?.Close();
btn_ic.Enabled = false;
btn_cat.Enabled = true;
}
private void btn_cat_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
Detail aa = new Detail();
aa.MdiParent = this;
aa.Show();
var selectForm = this.MdiChildren.FirstOrDefault(f => f.GetType() == typeof(SelectIC));
selectForm?.Close();
btn_cat.Enabled = false;
btn_ic.Enabled = true;
}

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.

how to show inherit groupbox in C#

My english isn't very good.
Hi, I have class where I inherit from GroupBox and I want to use polymorphism and I see in debugger that all is corrent but after compilation I see nothing...
Here is screenshot like it is and how it should be.
grid = new Grid.KierownikGrid();
SetGrid();
private void SetGrid()
{
grid.Location = new System.Drawing.Point(1, 0);
grid.Size = new System.Drawing.Size(10,10);
grid.TabIndex = 10;
grid.TabStop = false;
grid.Text = "";
}
public class KierownikGrid : GroupBox
{
RadioButton addUsers;
RadioButton deleteUsers;
RadioButton troubles;
public KierownikGrid()
:base()
{
Inicjacja();
}
protected void Inicjacja()
{
this.Controls.Add(addUsers = new RadioButton());
this.Controls.Add(deleteUsers = new RadioButton());
this.Controls.Add(troubles = new RadioButton());
this.addUsers.AutoSize = true;
this.addUsers.Checked = true;
this.addUsers.Location = new System.Drawing.Point(3, 10);
this.addUsers.TabIndex = 0;
this.addUsers.TabStop = true;
this.addUsers.Text = "Dodaj użytkownika";
this.addUsers.UseVisualStyleBackColor = true;
this.deleteUsers.AutoSize = true;
this.deleteUsers.Location = new System.Drawing.Point(125, 10);
this.deleteUsers.TabIndex = 1;
this.deleteUsers.Text = "Usuń użytkownika";
this.deleteUsers.UseVisualStyleBackColor = true;
this.troubles.AutoSize = true;
this.troubles.Location = new System.Drawing.Point(250, 10);
this.troubles.TabIndex = 2;
this.troubles.Text = "Problemy";
this.troubles.UseVisualStyleBackColor = true;
}
}
https://i.stack.imgur.com/DFu4t.png
https://i.stack.imgur.com/Dqeim.png
As #BugFinder already mentioned you have to add your control to a form.
There is a tutorial for that.
public class Form1 : System.Windows.Forms.Form
{
//Controls.
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ListBox lstBox = new ListBox();
private CheckBox chkBox = new CheckBox();
private Label lblCount = new Label();
private void Form1_Load(object sender, EventArgs e)
{
//Add controls to the form.
this.Controls.Add(btnAdd);
this.Controls.Add(txtBox);
this.Controls.Add(lstBox);
this.Controls.Add(chkBox);
this.Controls.Add(lblCount);
}
}

Can checkboxes be removed from a .NET WinForms ListView at runtime?

Is it possible to remove the checkboxes from a .NET WinForms ListView control at runtime?
The following code appears to have no effect when '.Checkboxes' has initially been set to 'true' and the control has rendered onto a form with checkboxes available for each list view item:
// C#:
testListView.BeginUpdate();
testListView.Checkboxes = false;
testListView.EndUpdate();
Is there a method that must be called to enact this change? What is the use of providing the .Checkboxes property when it defaults to 'false' and only has an effect if set to 'true'?
I just created a sample project using VS 2008 with the following code and it seemed to have worked:
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
ListViewItem lvi = new ListViewItem("Test");
listView1.Items.Add(lvi);
}
}
private void button1_Click(object sender, EventArgs e)
{
listView1.CheckBoxes = false;
}
the properties for my listview are as follows:
this.listView1.CheckBoxes = true;
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1});
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(12, 12);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(224, 174);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;

Categories

Resources