WinForms AcceptButton not working? - c#

Ok, this is bugging me, and I just can't figure out what is wrong...
I have made two forms. First form just has a simple button on it, which opens the other as a dialog like so:
using (Form2 f = new Form2())
{
if (f.ShowDialog() != DialogResult.OK)
MessageBox.Show("Not OK");
else
MessageBox.Show("OK");
}
The second, which is that Form2, has two buttons on it. All I have done is to set the forms AcceptButton to one, and CancelButton to the other. In my head this is all that should be needed to make this work. But when I run it, I click on the button which opens up Form2. I can now click on the one set as CancelButton, and I get the "Not OK" message box. But when I click on the one set as AcceptButton, nothing happens?
The InitializeComponent code of Form2 looks like this:
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(211, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// button2
//
this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button2.Location = new System.Drawing.Point(130, 13);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
//
// Form2
//
this.AcceptButton = this.button1;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.button2;
this.ClientSize = new System.Drawing.Size(298, 59);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);
}
I have done nothing else than add those two buttons, and set the AcceptButton and CancelButton. Why doesn't it work?

Just setting the AcceptButton/CancelButton is not enough. This just tells which button should be invoked on Enter/Esc. You have to set the button's DialogResult property.

Try setting DialogResult on button1
this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;

Definitely try the tutorial How to easily apply AcceptButton and CancelButton for custom dialog box in Winform.

I had an issue with the AcceptButton not working and while the DialogResult suggestion was part of the fix, I had 2 other things that needed to change:
My button was not visible - Intentionally because I wanted to stop the "ding" when a carriage return was "pressed" by scanning a barcode.
The container that the button was inside made a difference. I had to have it in the same container, in my case a Forms.Panel, as the textbox that was trying to access it. I'm not sure why this would make a difference, but it did.
I hope this helps someone.

You need to set the KeyPreview property of the form to True, the default value is False. Remember that if focus is set to any other button rather than the AcceptButton the Enter key will execute this button

Related

Adding C# form into another form

I need to add the form into the panel of the another parent form, but that is not working. When I try to add within the main parent form. I have a dashboard form and when I click the button a sidebar for gets open into one of the panels and again I need to load the form into the second panel of the dashboard form when I click on the button of the sidebar form.
public void btn_add_Click(object sender, EventArgs e)
{
Admin_Dashboard frm = new Admin_Dashboard();
Brand.Add_Brand myform = new Brand.Add_Brand();
myform.FormBorderStyle = FormBorderStyle.None;
myform.TopLevel = false;
myform.AutoScroll = true;
frm.content.Controls.Clear();
frm.content.Controls.Add(myform);
myform.Show();
}
This is what you do:
Make a custom control, which everything that the form has.
Change the form so it only has the custom control.
In the panel where you want the form... you put the custom control.
Bonus chatter: you can create a class to represent all the information that the custom control has to show. Then you can pass it around to show it both in the form and in the panel. Oh, and that class is a we call a view model.
I'm not sure what frm.content.Controls is, but this works...
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
class Form1 : Form
{
private Panel panel1;
private Button button1;
public Form1()
{
this.panel1 = new Panel();
this.button1 = new Button();
this.SuspendLayout();
this.panel1.Location = new System.Drawing.Point(305, 51);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(457, 338);
this.panel1.TabIndex = 0;
this.button1.Location = new System.Drawing.Point(321, 9);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.FormBorderStyle = FormBorderStyle.None;
form2.TopLevel = false;
form2.AutoScroll = true;
panel1.Controls.Clear();
panel1.Controls.Add(form2);
form2.Show();
}
}
class Form2 : Form
{
private Button button1;
public Form2()
{
this.button1 = new Button();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(321, 9);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Yellow;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
}
}
Am I missing something?

C# .NET UserControl Retain Child Control Properties on Cut-And-Paste

so I managed to build my own simple user control. Basically it is a custom Button containing a child Label control on top of it. The button works as how it should be during runtime.
However during design time I got an issue whenever I cut-and-paste that button (let's say I want to move it from Panel1 to Panel2 by cut-and-paste).
The button itself retains its properties such as background color, etc, but the child Label inside it is reinitialized everytime we paste it, so the text and color inside that label changed back to its default value.
The value of the labels text is set by "Text" property which overrides Text property of the UserControl such as follows :
private String _text = "Button";
[Browsable(true), Description("Sets the text displayed on the button"), Category("Display Settings")]
public override String Text {
get => _text;
set {
_text = value;
lb_Text.Text = _text;
}
}
Is there a way to retain child control properties during cut-and-paste in the Designer view?
Below is the code generated for InitializeComponent() section of the UserControl, which will be called whenever it is added to a form. Details aside, I acknowledged that default text and color values is re-initialized there, so I'm not sure how we replace those during cut-and-paste.
private void InitializeComponent()
{
this.lb_Text = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// lb_Text
//
this.lb_Text.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(127)))), ((int)(((byte)(127)))), ((int)(((byte)(127)))), ((int)(((byte)(127)))));
this.lb_Text.Dock = System.Windows.Forms.DockStyle.Fill;
this.lb_Text.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold);
this.lb_Text.ForeColor = System.Drawing.Color.Black;
this.lb_Text.Location = new System.Drawing.Point(0, 0);
this.lb_Text.Name = "lb_Text";
this.lb_Text.Size = new System.Drawing.Size(200, 50);
this.lb_Text.TabIndex = 1;
this.lb_Text.Text = "Button";
this.lb_Text.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// MomentaryButton
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImage = global::HMIControls.Properties.Resources.Button_Normal;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.Controls.Add(this.lb_Text);
this.Name = "MomentaryButton";
this.Size = new System.Drawing.Size(200, 50);
this.ResumeLayout(false);
}

Cannot set tabPage image to a tabControl C#

I have a ImageList, with one item, in the TabControl:
private System.Windows.Forms.ImageList tabControlMain_ilMain;
this.TabControlMain.ImageList = this.tabControlMain_ilMain;
this.tabControlMain_ilMain.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("tabControlMain_ilMain.ImageStream")));
this.tabControlMain_ilMain.TransparentColor = System.Drawing.Color.Silver;
this.tabControlMain_ilMain.Images.SetKeyName(0, "");
and I am setting this image to a specific TabPage:
this.tabControlMain_tp17.ImageIndex = 0;
and I can see in the designer the image attached to the TabPage, but when I am running the application, I cannot see the image, it is just an empty space near the TabPage name.
Does anyone know what could be the problem ?
Thank you!
P.S. What I have seen is that, if I am not selecting the ImageIndex, the TabPage header is getting less length, so I guess the image is taking the space, but cannot see it at runtime.
So i did nothing different than you i think. But mine is working.
Maybe remove and readd the controls.
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.TabControlMain = new System.Windows.Forms.TabControl();
this.tabControlMain_tp17 = new System.Windows.Forms.TabPage();
this.tabControlMain_ilMain = new System.Windows.Forms.ImageList(this.components);
this.TabControlMain.SuspendLayout();
this.SuspendLayout();
//
// TabControlMain
//
this.TabControlMain.Controls.Add(this.tabControlMain_tp17);
this.TabControlMain.ImageList = this.tabControlMain_ilMain;
this.TabControlMain.Location = new System.Drawing.Point(44, 42);
this.TabControlMain.Size = new System.Drawing.Size(192, 191);
//
// tabControlMain_tp17
//
this.tabControlMain_tp17.ImageIndex = 0;
this.tabControlMain_tp17.Location = new System.Drawing.Point(4, 23);
this.tabControlMain_tp17.Text = "tabControlMain_tp17";
//
// tabControlMain_ilMain
//
this.tabControlMain_ilMain.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("tabControlMain_ilMain.ImageStream")));
this.tabControlMain_ilMain.TransparentColor = System.Drawing.Color.Transparent;
this.tabControlMain_ilMain.Images.SetKeyName(0, "");
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.TabControlMain);
this.TabControlMain.ResumeLayout(false);
this.ResumeLayout(false);
}
private System.Windows.Forms.TabControl TabControlMain;
private System.Windows.Forms.TabPage tabControlMain_tp17;
private System.Windows.Forms.ImageList tabControlMain_ilMain;

How to compile and add the form designed by user to a project

I am going to let users create Forms during RunTime and add them to a project. I have done the designing and the UI of the form with the help of an open source Form Designer.
Here is the image of the Form Designer:
Lets assume I have the Form1.cs and Form1.cs[Designer] files which are enough for a WinForm. But how do I compile it to a DLL or an EXE and add it to the project? Any ideas? Any clues?
Thanks!!
EDIT
It creates this code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.CheckBox checkBox1;
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(71, 49);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseCompatibleTextRendering = true;
this.button1.UseVisualStyleBackColor = true;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(71, 94);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 1;
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(38, 184);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(104, 24);
this.checkBox1.TabIndex = 2;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseCompatibleTextRendering = true;
this.checkBox1.UseVisualStyleBackColor = true;
//
// form1
//
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "form1";
this.ResumeLayout(false);
this.PerformLayout();
}
}
I will do some extra coding to the rest of the code myself. but I dont know how to compile it and add it to the main exe?
The clue is System.CodeDom.Compiler
i think Form1.cs will contain no code (other then InitializeComponents call in constructor). so if you merge it with the designer code with simple string operations your job will be easier.

How to detect the form name when mouse is position on any SDI form

i was looking for trick to get the form name when mouse is place on it. suppose i have one mdi form and many sdi form like form1,form2,form3 and all sdi form are opened. suppose i have one timer running on form1 and which will run periodically. i want to show the form name on form1's label from the timer tick event when mouse is positioned on any SDI form window.
this way i try to do it. here is the code
private void timer1_Tick(object sender, EventArgs e) {
var handle = WindowFromPoint(Cursor.Position);
if (handle != IntPtr.Zero) {
var ctl = Control.FromHandle(handle);
if (ctl != null) {
label1.Text = ctl.Name;
return;
}
}
label1.Text = "None";
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(Point pos);
the above code run perfectly but there is some glitch. when i place my mouse on MDI form or on Form1 then form name is showing on form1 but when i place the mouse on Form2 or Form2 then their name is not showing. i am not being able to understand what is the problem in this code. please guide me to fix it.
Since you have the control, I think you just need to use the FindForm() function:
var ctl = Control.FromHandle(handle);
if (ctl != null) {
var form = ctrl.FindForm();
if (form != null) {
label1.Text = form.Name;
}
}
The reason Form2's name isn't showing up is that the instance of Form2 that you have created does not have a name. In order to demonstrate this, take a look at the following code block, situated in a Program.cs file:
MDIParent mdi = new MDIParent();
Form1 frm1 = new Form1();
frm1.MdiParent = mdi;
Form2 frm2 = new Form2();
frm2.MdiParent = mdi;
frm1.Show();
frm2.Show();
Application.Run(mdi);
If you add a breakpoint, you will see that Form2's name is blank. But wait, there's more!
If you add a control, any control, to Form 2, it will have a name when you create it. It looks like the naming system does not feel the need to assign the form control a name until multiple controls are present, which it does during its InitializeComponent call. You can take a look at this code. Here is the code generated for "Form3" with no controls:
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form3";
}
And here is the same form after a label has been added:
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Form3
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.label1);
this.Name = "Form3";
this.Text = "Form3";
this.ResumeLayout(false);
this.PerformLayout();
}
As you can see, a name is only assigned to the form if other controls are present. As for why this is the case, you would probably have to ask the person who wrote the auto-generation code for that answer.

Categories

Resources