I write a custom control.This control create a Form when double mouse double clicked.I also added other controls(button and label etc).But I create textbox1 and textbox2 outside of function.I write events of this controls but this didnt work.Guys I write textbox_press event.Because of this event I can only write digit or letter but I run this program and clicked on my control new form display but this events dont work
namespace Deneme
{
public partial class Direnc : Control
{
public Direnc()
{
InitializeComponent();
}
private string res_name;
private int res_value;
Form form = new Form();
TextBox textBox1 = new TextBox();
TextBox textBox2 = new TextBox();
protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
//
// label1
//
Label label1 = new Label();
AutoSize = true;
label1.Location = new System.Drawing.Point(27, 35);
label1.Name = "label1";
label1.Size = new System.Drawing.Size(27, 13);
label1.TabIndex = 0;
label1.Text = "İsmi";
//
// label2
//
Label label2 = new Label();
AutoSize = true;
label2.Location = new System.Drawing.Point(13, 89);
label2.Name = "label2";
label2.Size = new System.Drawing.Size(41, 13);
label2.TabIndex = 1;
label2.Text = "Değeri";
//
// textBox1
//
textBox1.Location = new System.Drawing.Point(58, 32);
textBox1.Name = "textBox1";
textBox1.Size = new System.Drawing.Size(100, 22);
textBox1.TabIndex = 2;
//
// textBox2
//
textBox2.Location = new System.Drawing.Point(58, 86);
textBox2.Name = "textBox2";
textBox2.Size = new System.Drawing.Size(100, 22);
textBox2.TabIndex = 3;
//
// button1
//
Button button1 = new Button();
button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
button1.Location = new System.Drawing.Point(64, 145);
button1.Name = "button1";
button1.Size = new System.Drawing.Size(75, 48);
button1.TabIndex = 4;
button1.Text = "Kaydet";
button1.UseVisualStyleBackColor = true;
//
// form
//
form.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
form.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
form.BackColor = System.Drawing.Color.RoyalBlue;
form.ClientSize = new System.Drawing.Size(176, 205);
form.Controls.Add(button1);
form.Controls.Add(textBox2);
form.Controls.Add(textBox1);
form.Controls.Add(label2);
form.Controls.Add(label1);
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
form.MaximizeBox = false;
form.MinimizeBox = false;
form.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
form.Text = "Direnç";
form.TopMost = true;
form.ResumeLayout(false);
form.PerformLayout();
form.ShowDialog();
button1.Click += new EventHandler(button1_Click);
textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
textBox2.KeyPress += new KeyPressEventHandler(textBox2_KeyPress);
}
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar)
&& !char.IsSeparator(e.KeyChar);
}
void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if(char.IsLetter(e.KeyChar) && char.IsControl(e.KeyChar))
e.Handled = true;
}
void button1_Click(object sender, EventArgs e)
{
res_name = textBox1.Text;
res_value = Convert.ToInt32(textBox2.Text);
MessageBox.Show(res_name + res_value.ToString());
}
}
Based on your description, I think this is what you want. If not you need to give us more info on what doesn't work.
I think the problem you are facing is that your event are assigned AFTER you call ShowDialog(), which is modal. This means that the method will block the current thread and any code after it will not execute until the form is closed. The solution below was not tested but should hopefully help you solve your issue. Also as I said above in my comment it is safer from a maintenance point of view for you to add an actual form to your project with all the required controls. Dynamic controls can have surprises when you least expect them.
protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
// The rest of you code here
// The rest of you code here
// The rest of you code here
button1.Click += delegate
{
res_name = textBox1.Text;
res_value = Convert.ToInt32(textBox2.Text);
MessageBox.Show(res_name + res_value.ToString());
};
textBox1.KeyPress += delegate(object sender, KeyPressEventArgs ev)
{
ev.Handled = !char.IsLetter(ev.KeyChar) && !char.IsControl(ev.KeyChar) && !char.IsSeparator(ev.KeyChar);
};
textBox2.KeyPress += delegate(object sender, KeyPressEventArgs ev)
{
if (char.IsLetter(e.KeyChar) && char.IsControl(e.KeyChar))
e.Handled = true;
};
form.ShowDialog(); // <----------- MODAL call, all the code is added BEFORE it
}
Related
I have an event registered.
layout.SizeChanged += parentToggleBox.Resize;
Problem is, I would like this Resize event to wait until my mouse has moved away from the box.
How to make this event wait until a boolean is changed from false to true?
or wait until OnMouseLeave(object sender, MouseEventArgs e) is triggered?
Here's an example that I put together:
public class Form1 : Form
{
private Panel Panel1;
private Label Label1;
public Form1()
{
this.Panel1 = new Panel()
{
BackColor = System.Drawing.Color.Red,
};
this.Label1 = new Label()
{
Top = 200,
Text = "Click",
};
bool clicked = false;
bool entered = false;
this.Panel1.Click += (s, e) =>
{
this.Panel1.BackColor = System.Drawing.Color.Blue;
clicked = true;
};
this.Panel1.MouseEnter += (s, e) =>
{
this.Panel1.BackColor = System.Drawing.Color.Yellow;
clicked = false;
entered = true;
};
this.Panel1.MouseLeave += (s, e) =>
{
if (entered && clicked)
{
this.Panel1.BackColor = System.Drawing.Color.Green;
this.Label1.Text = "Success";
}
};
this.Controls.Add(this.Panel1);
this.Controls.Add(this.Label1);
}
}
I've used a Click event rather than a Resize to demonstrate the technique.
Effectively this is just setting two booleans and when the right combination of entered and clicked occurs then the code in the block that contains this.Label1.Text = "Success"; will run.
The colours are set for debugging purposes.
To run, do this:
var form1 = new Form1();
form1.ShowDialog();
I am creating an online test application, in which I am generating approximately 100 buttons at run time on form load. Here is the piece of code:w
private void addQuestion_Reviewbutton()
{
for (int i = 1; i <= clsGlobalVars.gnTotalQuestion; i++)
{
Button button = new Button();
button.Location = new Point(160, 30 * i + 10);
button.Click += new EventHandler(ButtonClickOneEvent);
button.Tag = i;
button.Name = "Question" + i;
button.Text = i.ToString();
button.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.button));
button.BackgroundImageLayout = ImageLayout.Stretch;//.Zoom;
button.FlatAppearance.BorderSize = 0;
button.Size = new System.Drawing.Size(47, 41);
button.BackColor = Color.Transparent;
button.FlatStyle = FlatStyle.Flat;
button.Font = new System.Drawing.Font("Segoe UI Semibold", 12);
button.ForeColor = Color.White;
button.Cursor = Cursors.Hand;
flowLayoutPanel1.Controls.Add(button);
}
}
and on this button click, I am changing the background-color.
void ButtonClickOneEvent(object sender, EventArgs e)
{
Button button = sender as Button;
//button.BackColor = Color.Yellow;
button.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.button_Orange));
lblQuestionNo.Text = ((int)button.Tag).ToString()+".";
btnNext.Focus();
}
I have a button on the form Named "Next". Now my problem is that if I am currently in question "1." and I press the button next I want to change the background image of the button whose text is "2".
Yo check this !
tldr; the guidance are
Is the click event go to ButtonClickOneEvent ?
How to call other button
...
profit?
protected void Page_Load(object sender, EventArgs e)
{
addQuestion_Reviewbutton();
}
void ButtonClickOneEvent(object sender, EventArgs e)
{
Button button = sender as Button;
var currentbtn_id = button.ID;
int nextid = Convert.ToInt32( currentbtn_id.Substring("Question".Length)) + 1;
var nextBtn = flowLayoutPanel1.FindControl("Question"+ nextid.ToString() );
(nextBtn as Button).BackColor = Color.Red;
}
I have a simple winform application which allows users to drag controls to a tablelayoutpanel. But after some testing and trying to drag controls to a specific index row, I found out it doesn't work, not even with a hardcoded index number.
With the provided code example, I'm trying to add a textbox to row index 2, but when I drag content from the listbox to the tablelayoutpanel, it just adds the textbox in 'random' places as seen in the screenshot below
I expect the existing textboxes to shift down and make place for the textbox that's being added, as far as I understand from this: https://social.msdn.microsoft.com/Forums/windows/en-US/e4312cd8-6031-4a5c-92bf-e8adb1941fe5/insert-row-at-particular-position-in-table-layout-panel?forum=winforms.
Am I doing something wrong?
Designer code:
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(367, 12);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(190, 407);
this.listBox1.TabIndex = 0;
this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 1;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Location = new System.Drawing.Point(13, 12);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 1;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(332, 407);
this.tableLayoutPanel1.TabIndex = 1;
this.tableLayoutPanel1.DragDrop += new System.Windows.Forms.DragEventHandler(this.tableLayoutPanel1_DragDrop);
this.tableLayoutPanel1.DragEnter += new System.Windows.Forms.DragEventHandler(this.tableLayoutPanel1_DragEnter);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(569, 431);
this.Controls.Add(this.tableLayoutPanel1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
}
Form code:
public partial class Form1 : Form
{
private int tempInt = 0;
public Form1()
{
InitializeComponent();
listBox1.Items.AddRange(new object[] { "test" });
tableLayoutPanel1.AllowDrop = true;
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.RowCount = 3;
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
tempInt++;
DoDragDrop("test" + tempInt, DragDropEffects.Copy);
}
private void tableLayoutPanel1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void tableLayoutPanel1_DragDrop(object sender, DragEventArgs e)
{
string text = e.Data.GetData(typeof(String)) as string;
TextBox tb = new TextBox();
tb.Dock = DockStyle.Fill;
tb.Text = text;
// I want to add the textbox to the second row
tableLayoutPanel1.Controls.Add(tb, 0, 2);
tableLayoutPanel1.SetRow(tb, 2);
}
}
EDIT:
Added code based as DonBoitnott's suggested
private void tableLayoutPanel1_DragDrop(object sender, DragEventArgs e)
{
string text = e.Data.GetData(typeof(String)) as string;
TextBox tb = new TextBox();
tb.Dock = DockStyle.Fill;
tb.Text = text;
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
{
int pos = tableLayoutPanel1.GetRow(tableLayoutPanel1.Controls[i]);
if (pos > 1)
{
tableLayoutPanel1.SetRow(tableLayoutPanel1.Controls[i], pos + 1);
}
}
tableLayoutPanel1.Controls.Add(tb, 0, 2);
}
Give this modified form class a try, I've made a few changes throughout:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.AddRange(new Object[] { "TextBox" });
tableLayoutPanel1.AllowDrop = true;
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f));
}
private void listBox1_MouseDown(Object sender, MouseEventArgs e)
{
var count = tableLayoutPanel1.Controls.Count;
DoDragDrop($"test{count + 1}", DragDropEffects.Copy);
}
private void tableLayoutPanel1_DragEnter(Object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void tableLayoutPanel1_DragDrop(Object sender, DragEventArgs e)
{
var tb = new TextBox();
tb.Dock = DockStyle.Fill;
tb.Text = (e.Data.GetData(typeof(String)) as String);
var newRow = tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
var ctrl = tableLayoutPanel1.GetChildAtPoint(tableLayoutPanel1.PointToClient(new Point(e.X, e.Y)));
if (ctrl != null)
{
var pos = tableLayoutPanel1.GetRow(ctrl);
for (Int32 i = tableLayoutPanel1.RowStyles.Count - 2; i >= pos; i--)
{
var c = tableLayoutPanel1.GetControlFromPosition(0, i);
if (c != null)
tableLayoutPanel1.SetRow(c, i + 1);
}
tableLayoutPanel1.Controls.Add(tb, 0, pos);
}
else
tableLayoutPanel1.Controls.Add(tb, 0, newRow);
}
}
The basic steps are:
Did I drop onto an existing control? If so, determine where.
If so, work bottom to top, moving each control down a slot. You must avoid overlapping, two controls cannot occupy the same cell.
If so, insert the new control into the newly emptied slot at the drop point.
If not, simply add the control at the end.
I have a Windows form with labels and I created a common click event handler for all the labels.
The event is bound from code. Now I need to create some more labels in run-time and bind the same event handler to those dynamically created labels. I tried the following but it didn't work.
private void Ctrl_Click(object sender, EventArgs e)
{
Control control = (Control)sender;
if (control is Label)
{
Label lbl = (Label)sender;
txtCaption.Text = lbl.Text;
cboFont.Text = lbl.Font.FontFamily.Name;
txtSize.Text = lbl.Font.Size.ToString();
chkBold.Checked = lbl.Font.Bold;
txtX.Text = lbl.Location.X.ToString();
txtY.Text = lbl.Location.Y.ToString();
txtWidth.Text = lbl.Width.ToString();
gbLogo.Visible = false;
gbControl.Visible = true;
gbDetail.Visible = false;
}
}
private void btnAddDynamic_Click(object sender, EventArgs e)
{
Label label = new Label();
int count = pl.Controls.OfType<Label>().ToList().Count;
label.Location = new Point(50, (25 * count) + 2);
label.AutoSize = true;
Graphics g = label.CreateGraphics();
label.Width =Convert.ToInt32(g.MeasureString(label.Text, label.Font).Width);
label.Name = "label_" + (count + 1);
label.Text = "label " + (count + 1);
label.TabIndex=0;
label.Click += new EventHandler(this.Ctrl_Click);
pl.Controls.Add(label);
}
I am trying to call a button's visibility, from a MouseHover event on another object.
What I'm trying to do is when I mouse over a pictureBox to set the button that is attached to that pictureBox to be visible, on default the button when it is created is invisible.
When I try to call it from the MouseHover event it says that the button is null. I'm not that good with inheritance so I'm kinda stuck here, any help would be appreciated.
Here is the code (what I'm trying to do is only on MouseHover event):
private void Button1_Click(object sender, EventArgs e)
{
FlowLayoutPanel flP = new FlowLayoutPanel();
PictureBox picB = new PictureBox();
Label laB = new Label();
Button btn = new Button();
picB.Size = new Size(130, 70);
laB.Size = new Size(130, 20);
flP.Size = new Size(130, 90);
btn.Size = new Size(20, 20);
laB.Text = "Text";
laB.Name = "Name";
flP.Name = "Name";
btn.Text = "X";
btn.Name = "Name";
btn.Visible = false;
flP.Controls.Add(picB);
flP.Controls.Add(laB);
picB.Controls.Add(btn);
flP.Location = new System.Drawing.Point(3, 3);
laB.Location = new System.Drawing.Point(3, 70);
btn.Location = new System.Drawing.Point(100, 5);
mainFLP.Controls.Add(flP);
picB.MouseHover += picB_MouseHover;
picB.DoubleClick += picB_DoubleClick;
}
private void picB_MouseHover(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
Button bt = pb.Parent as Button;
//bt.Visible = true;
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++)
{
FlowLayoutPanel flP = new FlowLayoutPanel();
PictureBox picB = new PictureBox();
Label laB = new Label();
Button btn = new Button();
picB.Size = new Size(130, 70);
laB.Size = new Size(130, 20);
flP.Size = new Size(130, 90);
btn.Size = new Size(20, 20);
flP.Name = i.ToString();
laB.Name = "Link";
laB.Text = "Name";
btn.Text = "X";
btn.Name = "b" + i.ToString();
btn.Visible = false;
flP.Controls.Add(picB);
flP.Controls.Add(laB);
picB.Controls.Add(btn);
flP.Location = new System.Drawing.Point(3, 3);
laB.Location = new System.Drawing.Point(3, 70);
btn.Location = new System.Drawing.Point(100, 5);
mainFLP.Controls.Add(flP);
picB.MouseHover += picB_MouseHover;
picB.DoubleClick += picB_DoubleClick;
}
}
private void picB_DoubleClick(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
FlowLayoutPanel flp = pb.Parent as FlowLayoutPanel;
flp.Dispose();
}
One method can be to store the Button variable in tag property of your picture box:
PictureBox picB = new PictureBox();
Button btn = new Button();
picB.Tag = btn;
and later, in your MouseHover Hanlder
private void picB_MouseHover(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
Button bt = pb.Tag as Button;
//bt.Visible = true;
}
It is null because the sender of the event is the picture, not the button.
You can simply declare the button at the class level
private Button btn;
private void Button1_Click(object sender, EventArgs e)
{
FlowLayoutPanel flP = new FlowLayoutPanel();
PictureBox picB = new PictureBox();
Label laB = new Label();
btn = new Button();
and then directly make it visible
private void picB_MouseHover(object sender, EventArgs e)
{
bt.Visible = true;
}
Edit
Or you can define a Dictionary to associate the found PictureBox with the corresponding Button
private var btnDict = new Dictionary<PictureBox,Button>();
and when you create them, you can also link them,
PictureBox picB = new PictureBox();
Label laB = new Label();
Button btn = new Button();
btnDict.Add(picB,btn);
so that you can retrieve the button with a command like
PictureBox pb = (PictureBox)sender;
var btn = btnDict[pb];