I managed to get a clear button on a TextBox line with little code. This deletes the corresponding line and itself when clicking. But the button appears only once. It should be generated with each click on each additional line. Can someone help me there? Many Thanks.
Is WinForm VS2010 C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Daten_zu_TextBox_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Button btn1 = new Button();
btn1.Name = "btn";
btn1.Click += new EventHandler(btn1_Click);
btn1.Size = new Size(18, 18);
btn1.Text = "X";
btn1.ForeColor = Color.Red;
textBox1.Controls.Add(btn1);
string sent = ("\t" + "Testline1");
textBox1.AppendText(sent);
textBox1.AppendText(Environment.NewLine);
}
void btn1_Click(object sender, EventArgs e)
{
textBox1.Controls.Clear();
textBox1.Text = textBox1.Text.Remove(1, textBox1.Lines[0].Length + 0 );
textBox1.Text = textBox1.Text.Remove(0, textBox1.Lines[0].Length + 0);
}
}
}
private int ButtonCount2 = 0;
private int Btn2Y = 18;
private void button2_Click(object sender, EventArgs e)
{
Button btn2 = new Button();
btn2.Name = "btn2" + ButtonCount2;
btn2.Click += new EventHandler(btn2_Click);
btn2.Size = new Size(18, 18);
btn2.Location = new Point(1, Btn2Y * ButtonCount2);
ButtonCount2++;
btn2.Text = "X";
btn2.ForeColor = Color.Red;
textBox2.Controls.Add(btn2);
string sent = ("\t" + "Testline" + ButtonCount2);
textBox2.AppendText(sent);
textBox2.AppendText(Environment.NewLine);
}
void btn2_Click(object sender, EventArgs e)
{
var bt2 = sender as Button;
var bt2Id = textBox2.Controls.IndexOf(bt2);
textBox2.Controls.Remove(bt2);
var lines = textBox2.Text.Replace("\r\n", "\n").Split('\n').ToList();
lines.RemoveAt(bt2Id);
textBox2.Text = string.Join("\r\n", lines);
foreach (Button btn2 in textBox2.Controls)
{
var Id2 = int.Parse(btn2.Name.Replace("btn2", ""));
if (Id2 > bt2Id)
{
var b2 = textBox2.Controls.Find(btn2.Name, false)[0];
var loc2 = btn2.Location;
b2.Location = new Point(loc2.X, loc2.Y - Btn2Y);
}
}
ButtonCount2--;
}
The second button looks like this.
You're putting all the X buttons on the same place, on top of textbox. Implement some logic that increases Y location of your button. That can be something like this:
//... your code here ...
btn1.ForeColor = Color.Red;
//increase Y
// for each control that is inside of textBox,
// lower your newly created button by count * 18
// so that btn1.Top will be 0, 18, 36 etc...
btn1.Top = textBox1.Controls.Count * 18;
textBox1.Controls.Add(btn1);
also, as WPFUser noticed, on click on X button, you're removing all the X buttons. I'm guessing you should remove last, bottom one
EDIT 2. Each button should remove its corresponding line, like this (didn't test it :))
void btn1_Click(object sender, EventArgs e)
{
//remove right line
text1.Text = text1.Lines[text1.Controls.IndexOf((Control)sender)].Remove(0);
//remove button
text1.Controls.Remove(text1.Controls.OfType<Button>().Last());
}
#jadolo ,Your code is correct ,It add new button every time but every button will located at same location so those all button will not visible to you, Add assign location property of button like this,so all button are displayed correctly.
Form1.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
int i = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
void btn1_Click(object sender, EventArgs e)
{
textBox1.Controls.Clear();
textBox1.Text = textBox1.Text.Remove(1, textBox1.Lines[0].Length + 0);
textBox1.Text = textBox1.Text.Remove(0, textBox1.Lines[0].Length + 0);
}
private void button1_Click_1(object sender, EventArgs e)
{
Button btn1 = new Button();
btn1.Name = "btn"+i++;
btn1.Click += new EventHandler(button1_Click_1);
btn1.Size = new Size(18,18 );
btn1.Text = "X";
btn1.Location = new Point(0, i);
i += 18;
btn1.ForeColor = Color.Red;
this.textBox1.Controls.Add(btn1);
string sent = ("\t" + "Testline1");
textBox1.AppendText(sent);
textBox1.AppendText(Environment.NewLine);
}
}
}
Design :
Output :
I hope this will help you.
Below is tested working codes. The button will coincide with text for the textbox font size being the default 8.
private int ButtonCount = 0;
private int BtnY = 13;
private void button1_Click(object sender, EventArgs e)
{
Button btn1 = new Button();
btn1.Name = "btn" + ButtonCount;
btn1.Click += new EventHandler(btn1_Click);
btn1.Size = new Size(18, 18);
btn1.Location = new Point(1, BtnY * ButtonCount);
btn1.Tag = ButtonCount; // the last edit
ButtonCount++;
btn1.Text = "X";
btn1.ForeColor = Color.Red;
textBox1.Controls.Add(btn1);
string sent = ("\t" + "Testline" + ButtonCount);
textBox1.AppendText(sent);
textBox1.AppendText(Environment.NewLine);
}
void btn1_Click(object sender, EventArgs e)
{
var bt = sender as Button;
var btId = textBox1.Controls.IndexOf(bt);
textBox1.Controls.Remove(bt);
var lines = textBox1.Text.Replace("\r\n", "\n").Split('\n').ToList();
lines.RemoveAt(btId);
textBox1.Text = string.Join("\r\n", lines);
foreach (Button btn in textBox1.Controls)
{
//var Id = int.Parse(btn.Name.Replace("btn", "")); // the last edit
var Id = (int)btn.Tag; // the last edit
if (Id > btId)
{
var b = textBox1.Controls.Find(btn.Name, false)[0];
var loc = btn.Location;
b.Location = new Point(loc.X, loc.Y - BtnY);
}
}
ButtonCount--;
}
Related
I’am switching between buttons by adding/removing them dynamically. After 20 switches the whole application starts to be more and more slow.
It seem’s It is related to function Dispose(). When I use it the problem become worst. You can see how I used it in pasted code.
I’am using Visual Studio 2010 Express.
Any idea what I’am doing wrong?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Tracker
{
public partial class Tracker : Form
{
Button New_Trace = new Button();
Button Continue_Trace = new Button();
Button Save_Trace = new Button();
Button New_Point = new Button();
Button Exit_Trace = new Button();
public Tracker()
{
InitializeComponent();
Init_Menu1();
}
public void Init_Menu1()
{
New_Trace.Left = 20;
New_Trace.Top = 40;
New_Trace.Text = "New Trace";
this.New_Trace.Click += new System.EventHandler(this.New_Trace_Click);
this.Controls.Add(New_Trace);
Continue_Trace.Left = 100;
Continue_Trace.Top = 40;
Continue_Trace.Text = "Cont. Trace";
this.Continue_Trace.Click += new System.EventHandler(this.Continue_Trace_Click);
this.Controls.Add(Continue_Trace);
Save_Trace.Left = 180;
Save_Trace.Top = 40;
Save_Trace.Text = "Save Trace";
this.Save_Trace.Click += new System.EventHandler(this.Save_Trace_Click);
this.Controls.Add(Save_Trace);
}
public void Init_Menu2()
{
New_Point.Left = 20;
New_Point.Top = 40;
New_Point.Text = "New Point";
this.New_Point.Click += new System.EventHandler(this.New_Point_Click);
this.Controls.Add(New_Point);
Exit_Trace.Left = 180;
Exit_Trace.Top = 40;
Exit_Trace.Text = "Exit";
this.Exit_Trace.Click += new System.EventHandler(this.Exit_Trace_Click);
this.Controls.Add(Exit_Trace);
}
public void remove_Menu1()
{
if (this.Controls.Contains(New_Trace))
{
// this.New_Trace.Click -= new System.EventHandler(this.New_Trace_Click);
this.Controls.Remove(New_Trace);
//New_Trace.Dispose();
}
if (this.Controls.Contains(Continue_Trace))
{
// this.Continue_Trace.Click -= new System.EventHandler(this.Continue_Trace_Click);
this.Controls.Remove(Continue_Trace);
//Continue_Trace.Dispose();
}
if (this.Controls.Contains(Save_Trace))
{
// this.Save_Trace.Click -= new System.EventHandler(this.Save_Trace_Click);
this.Controls.Remove(Save_Trace);
//Save_Trace.Dispose();
}
}
public void remove_Menu2()
{
if (this.Controls.Contains(New_Point))
{
//this.New_Point.Click -= new System.EventHandler(this.New_Point_Click);
this.Controls.Remove(New_Point);
//New_Point.Dispose();
}
if (this.Controls.Contains(Exit_Trace))
{
//this.Exit_Trace.Click -= new System.EventHandler(this.Exit_Trace_Click);
this.Controls.Remove(Exit_Trace);
//Exit_Trace.Dispose();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void New_Trace_Click(object sender, EventArgs e)
{
remove_Menu1();
Init_Menu2();
}
private void Continue_Trace_Click(object sender, EventArgs e)
{
}
private void Save_Trace_Click(object sender, EventArgs e)
{
}
private void New_Point_Click(object sender, EventArgs e)
{
}
private void Exit_Trace_Click(object sender, EventArgs e)
{
remove_Menu2();
Init_Menu1();
}
}
}
Try changing visible of button to false/true instead of deleting and recreating it.
button.Visible = true; //set visible to visible
button.Visible = false; //set visible to invisible
button.Visible = !(button.Visible); //set visible to visible if button was invisible or to invisible, if button was Visible
Also In VS 2015 you have profiling tools, which can help you (but I'm not sure If they're in VS 2010 too)
Please, mark my answer as correct, if I helped you :D
I am facing an issue over here. I want to remove the dynamic created button by press the X button.
The function will be user press X button, then press the button he/she want to delete, the button will be remove.
For now my program is able to create a new button but I do not know how to delete it, here is my coding, please help me , thanks very much!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class graphtest : Form
{
public graphtest()
{
InitializeComponent();
}
private Point Origin_Cursor;
private Point Origin_Control;
private bool BtnDragging = false;
private void button1_Click(object sender, EventArgs e)
{
var b = new Button();
b.Width = 54;
b.Height = 58;
b.Image = Image.FromFile(#"C:\Users\prod01\Desktop\Mote.png");
b.Text = "";
b.Name = "button";
//b.Click += new EventHandler(b_Click);
b.MouseUp += (s, e2) => { this.BtnDragging = false; };
b.MouseDown += new MouseEventHandler(this.b_MouseDown);
b.MouseMove += new MouseEventHandler(this.b_MouseMove);
this.Controls.Add(b);
}
private void b_MouseDown(object sender, MouseEventArgs e)
{
Button ct = sender as Button;
ct.Capture = true;
this.Origin_Cursor = System.Windows.Forms.Cursor.Position;
this.Origin_Control = ct.Location;
this.BtnDragging = true;
}
private void b_MouseMove(object sender, MouseEventArgs e)
{
if (this.BtnDragging)
{
Button ct = sender as Button;
ct.Left = this.Origin_Control.X - (this.Origin_Cursor.X - Cursor.Position.X);
ct.Top = this.Origin_Control.Y - (this.Origin_Cursor.Y - Cursor.Position.Y);
}
}
private void graphtest_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
for (int ix = this.Controls.Count - 1; ix >= 0; ix--)
{
if (this.Controls[ix] is Button) this.Controls[ix].Dispose();
}
}
}
}
Here is how you can do that
//a list where you save all the buttons created
List<Button> buttonsAdded = new List<Button>();
private void button2_Click(object sender, EventArgs e)
{
Button myText = new Button();
myText.Tag = counter;
myText.Location = new Point(x2,y2);
myText.Text = Convert.ToString(textBox3.Text);
this.Controls.Add(myText);
//add reference of the button to the list
buttonsAdded.Insert(0, myText);
}
//atach this to a button removing the other buttons
private void removingButton_Click(object sender, EventArgs e)
{
if (buttonsAdded.Count > 0)
{
Button buttonToRemove = buttonsAdded[0];
buttonsAdded.Remove(buttonToRemove);
this.Controls.Remove(buttonToRemove);
}
}
Ref :- how to delete a button run time?
i need to use a button to make a number of textboxes with related buttons each button will add 1 to the textbox (that i want to be related to it HOW?)
i have a windows form with button1 and three panels
==========================================
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace AdvancedCounter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (panel3.Controls != null)
{
var btn = panel3.Controls.Find("_B", true).First();
btn.Click += new EventHandler(Btn_Click);
}
}
int a = 0;
int counter=0;
private void button1_Click(object sender, EventArgs e)
{
counter++;
Button btn = new Button();
btn.Location = new Point(0, 100);
btn.Text = "ADD";
btn.Name ="_B";
btn.Dock = DockStyle.Left;
btn.Click += new EventHandler(Btn_Click);
TextBox txt = new TextBox();
txt.Name = "_T";
txt.Location = new Point(500, 100);
txt.Dock = DockStyle.Left;
txt.Text = a.ToString();
panel3.Controls.Add(txt);
panel3.Controls.Add(btn);
foreach (var item in panel3.Controls.Find("_B", true))
{
item.Text = "ass";
}
}
private void Btn_Click(object sender, EventArgs e)
{
// MessageBox.Show(sender.ToString());
// throw new NotImplementedException();
var txtbx= panel3.Controls.Find("_T", true).First();
var btnbx = panel3.Controls.Find("_B", true).First();
a++;
// find[1].Dispose();
txtbx.Text = a.ToString();
}
}
}
First you are giving same Name property for every TextBox and Button, you should not do that. Instead do like this for example:
btn.Name = "_B_"+ counter;
So you will have different Name for Buttons and TextBoxes. And in event handler:
private void Btn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string index = btn.Name.Split('_')[2];
string tbName = "_T_" + index;
var txtbx= panel3.Controls.Find(tbName, true).First();
a++;
txtbx.Text = a.ToString();
}
first sorry for my English. I am programming application in windows forms. It is something like Packet Tracer. I have four buttons. When I click on them, they dynamicaly create pictureboxes with picture of Router or Switch,.... Each time I click on the button, new picture box(Switch or Router,...), is created. I can also move with this pictureboxes by mouse.
I need to create a button, which connects selected pictureboxes with line(Cable). This pictureboxes should be selected by click on them. It sholud be able to move with this objects(movable line).
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
PictureBox[] picturebox = new PictureBox[100];
public Form1()
{
InitializeComponent();
}
private void router_Click(object sender, EventArgs e)
{
++a;
picturebox[a] = new PictureBox();
picturebox[a].Name = "picturebox" + a;
picturebox[a].Location = new Point(0 + (a-1) *100,100);
picturebox[a].Size = new Size(70, 70);
picturebox[a].BorderStyle = BorderStyle.None;
picturebox[a].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[a]);
picturebox[a].Image = Image.FromFile(#"D:\\router.jpg");
picturebox[a].Refresh();
picturebox[a].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[a].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[a].MouseUp += new MouseEventHandler(picMouseUp);
}
private void Form1_Load(object sender, EventArgs e)
{
}
int x = 0;
int y = 0;
bool drag = false;
private void picMouseDown(object sender, MouseEventArgs e)
{
// Get original position of cursor on mousedown
x = e.X;
y = e.Y;
drag = true;
}
private void picMouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
PictureBox pb = (PictureBox)sender;
// Get new position of picture
pb.Top += e.Y - y;
pb.Left += e.X - x;
pb.BringToFront();
}
}
private void picMouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
private void switch1_Click(object sender, EventArgs e)
{
++b;
picturebox[b] = new PictureBox();
picturebox[b].Name = "picturebox" + b;
picturebox[b].Location = new Point(0 + (b - 1) * 100, 180);
picturebox[b].Size = new Size(70, 70);
picturebox[b].BorderStyle = BorderStyle.None;
picturebox[b].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[b]);
picturebox[b].Image = Image.FromFile(#"D:\HP ProBook 450\Desktop\Grafika\switch1.png");
picturebox[b].Refresh();
picturebox[b].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[b].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[b].MouseUp += new MouseEventHandler(picMouseUp);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void pc_Click(object sender, EventArgs e)
{
++c;
picturebox[c] = new PictureBox();
picturebox[c].Name = "picturebox" + c;
picturebox[c].Location = new Point(0 + (c - 1) * 100, 260);
picturebox[c].Size = new Size(70, 70);
picturebox[c].BorderStyle = BorderStyle.None;
picturebox[c].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[c]);
picturebox[c].Image = Image.FromFile(#"D:\HP ProBook 450\Desktop\pc.jpg");
picturebox[c].Refresh();
picturebox[c].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[c].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[c].MouseUp += new MouseEventHandler(picMouseUp);
}
private void server_Click(object sender, EventArgs e)
{
++d;
picturebox[d] = new PictureBox();
picturebox[d].Name = "picturebox" + d;
picturebox[d].Location = new Point(0 + (d - 1) * 100, 340);
picturebox[d].Size = new Size(70, 70);
picturebox[d].BorderStyle = BorderStyle.None;
picturebox[d].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[d]);
picturebox[d].Image = Image.FromFile(#"D:\HP ProBook 450\Desktop\server.png");
picturebox[d].Refresh();
picturebox[d].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[d].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[d].MouseUp += new MouseEventHandler(picMouseUp);
}
}
}
THank you for your help.
You need to invalidate the parent when you add a picturebox or when you move a picturebox:
(picMouseMove and 4 times in the click handlers, it would be better to use 1 function)
Invalidate();
This is an example OnPaint, drawing lines between the pictureboxes as they are located in the Controls collection: (your picturebox array seems very weird, you always add controls at index 1, always overwriting the previous entry?! i'd suggest using a List if you need to keep a reference to them)
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var pictureBoxes = Controls.OfType<PictureBox>().ToArray();
if (pictureBoxes.Length > 1)
{
for (int i = 1; i < pictureBoxes.Length; i++)
{
DrawLineBetween(e.Graphics, pictureBoxes[i - 1], pictureBoxes[i]);
}
}
}
This function can be used to draw a line between 2 of your boxes:
private void DrawLineBetween(Graphics g, PictureBox from, PictureBox to)
{
g.DrawLine(Pens.Black,
new Point(from.Left + from.Width / 2, from.Top + from.Height / 2),
new Point(to.Left + to.Width / 2, to.Top + to.Height / 2));
}
----- full sample below -----
I refactored your full example, and added the code above to start you off with a working example:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
List<PictureBox> pictureboxes = new List<PictureBox>();
public Form1()
{
InitializeComponent();
}
private void AddPictureBox(string imagePath)
{
var pb = new PictureBox();
pb.Name = "picturebox" + pictureboxes.Count;
pb.Location = new Point(pictureboxes.Count * 100, 100);
pb.Size = new Size(70, 70);
pb.BorderStyle = BorderStyle.None;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(pb);
pb.Image = Image.FromFile(imagePath);
pb.Refresh();
pb.MouseDown += new MouseEventHandler(picMouseDown);
pb.MouseMove += new MouseEventHandler(picMouseMove);
pb.MouseUp += new MouseEventHandler(picMouseUp);
pictureboxes.Add(pb);
Invalidate();
}
private void router_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\\router.jpg");
}
private void Form1_Load(object sender, EventArgs e)
{
}
int x = 0;
int y = 0;
bool drag = false;
private void picMouseDown(object sender, MouseEventArgs e)
{
// Get original position of cursor on mousedown
x = e.X;
y = e.Y;
drag = true;
}
private void picMouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
PictureBox pb = (PictureBox)sender;
// Get new position of picture
pb.Top += e.Y - y;
pb.Left += e.X - x;
pb.BringToFront();
Invalidate();
}
}
private void picMouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
private void switch1_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\Grafika\switch1.png");
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void pc_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\pc.jpg");
}
private void server_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\server.png");
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (pictureboxes.Count > 1)
{
var arr = pictureboxes.ToArray();
for (int i = 1; i < arr.Length; i++)
{
DrawLineBetween(e.Graphics, arr[i - 1], arr[i]);
}
}
}
private void DrawLineBetween(Graphics g, PictureBox from, PictureBox to)
{
g.DrawLine(Pens.Black,
new Point(from.Left + from.Width / 2, from.Top + from.Height / 2),
new Point(to.Left + to.Width / 2, to.Top + to.Height / 2));
}
}
}
I have 2 textbox and 1 button textbox1,textbox2 and 1 increment button.Both textbox is initialize by 1.If i will click on textbox1 and then click on incr button the value belongs to textbox1 will be increase only.And whenever i will click on textbox2 and again i will click on incr button only textbox2 value will be increment.
How will i do this?
You didn't say whether you were in WinForms or in WPF, so I won't show any code.
Have a field TextBox activeTextBox in your class. In the GotFocus events of each textbox, set activeTextBox = this text box. In the button click, convert activeTextBox's text to an integer, add one, and convert back to string and set the text back.
Edit:
activeTextBox is a field you will need to set up, the designer won't make it for you. If you set the GotFocus event of textBox1 set activeTextBox = textBox1, and similar for textBox2, then activeTextBox will always have the 'current' text box. Then in the button's click event, you can do whatever you need to do on activeTextBox. You shouldn't need to access textBox1 or textBox2 from the button click handler at all.
This can be done on the client side using javascript. On focus of textbox1 update a hiddenfield value. similarly for the textbox2. then on button click, based on hidden f
Create a windows form application and paste this code on form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
TextBox textbox1 = new TextBox(), textbox2 = new TextBox();
Button button1 = new Button();
int FocusedTextBox = 0;
public Form1()
{
InitializeComponent();
this.Load += new System.EventHandler(this.Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += new EventHandler(button1_Click);
textbox1.Text = textbox2.Text = "1";
textbox1.Location = new Point(100, 100);
textbox2.Location = new Point(100, 140);
button1.Location = new Point(100, 180);
textbox1.Click += new EventHandler(textbox1_Click);
textbox2.Click += new EventHandler(textbox2_Click);
textbox1.ReadOnly = true;
textbox2.ReadOnly = true;
this.Controls.Add(textbox1);
this.Controls.Add(textbox2);
this.Controls.Add(button1);
}
void textbox2_Click(object sender, EventArgs e)
{
FocusedTextBox = 2;
}
void textbox1_Click(object sender, EventArgs e)
{
FocusedTextBox = 1;
}
void button1_Click(object sender, EventArgs e)
{
if (FocusedTextBox ==1)
textbox1.Text = (int.Parse(textbox1.Text) + 1).ToString();
else if (FocusedTextBox == 2)
textbox2.Text = (int.Parse(textbox2.Text) + 1).ToString();
}
}
}
private int pickedbox = 0
[...]
private void textBox1_Enter(...)
{
pickedbox = 0;
}
private void textBox2_Enter(...)
{
pickedbox = 1;
}
private void button1_Click(...)
{
switch(pickedbox)
{
case 0:
textBox1.Text = int.Parse(textBox1.Text)++;
break;
case 1:
textBox2.Text = int.Parse(textBox2.Text)++;
break;
}
}
if (textBox1.Focused)
{
textBox1.Text = (Convert.ToInt32(textBox1.Text) + 1) + "";
}
else if (textBox2.Focused)
{
textBox2.Text = (Convert.ToInt32(textBox2.Text) + 1) + "";
}
else if (textBox3.Focused)
{
textBox3.Text = (Convert.ToInt32(textBox3.Text) + 1) + "";
}
But ".Focused" is always returing False.
Why ?