i m using vs 2005 C# framework 2.0
i have a simple application
in which i have main button
when user clicks on it 2 things happen
1.for the click on first time main button creates a tabcontrol.
2.each time it creates a tabpage and add it to the tabcontrol created.
each tabpage contains 2 controls
1.textbox (named:ttb)
2.panel (named:search)
each panel contains 2 controls.
1.textbox (stb)
2.button (sbtn)
now what i have done is
when user click on sbtn then the click event of sbtn fires and it displays the contents of stb into ttb.
this is working fine
unless i create a new tab.
when the new tab is created, then in the previos created tab the sbtn doesn't work(i.e. it doesnot copy the contents of stb into ttb)
what i might think is that the problem is related to click event.
plz help me to solve this
regards,
Ahsan Ashfaq
Loading
Nilanka DharmadasaPosted Nov 16, 2009, 6:44 AM
y? you found any problem in my code?? If so let me know pls..
Nilanka DharmadasaPosted Nov 16, 2009, 4:35 AM
When you call 'stb', 'ttb', it refers to the last controls added. So you should add some extra code to overcome that.
You should replace your button click event with following method.
private void sbtn_click(object sender, EventArgs e)
{
try
{
Button x = ((Button)sender);
Panel y = ((Panel)(x.Parent));
string textinstb = "";
foreach (Control t in y.Controls)
{
if (t.Name.Equals("stb"))
{
textinstb = t.Text;
}
}
TabPage z = ((TabPage)(y.Parent));
foreach (Control t in z.Controls)
{
if (t.Name.Equals("ttb"))
{
t.Text = textinstb;
}
}
}
catch
{
}
}
If you find this useful, do not forget to mark this as accepted.
ahsan ashfaqPosted Nov 16, 2009, 3:48 AM
{
if (i == 1)
{
tbctrl.Size = new Size(0, this.Height - (abc.Height+30));
tbctrl.Dock = DockStyle.Bottom;
this.Controls.Add(this.tbctrl);
i++;
}
tabpg1 = new TabPage();
stb = new TextBox();
sbtn = new Button();
ttb = new TextBox();
//Panel1
panel1 = new Panel();
panel1.BackColor = Color.Blue;
panel1.Controls.Add(stb);
panel1.Controls.Add(sbtn);
sbtn.Location = new Point(stb.Height+50,50);
sbtn.Text = "Search";
sbtn.Click+=new EventHandler(sbtn_click);
//end panel1
tabpg1.Controls.Add(panel1);
tabpg1.Controls.Add(ttb);
panel1.Size = new Size(200,200);
panel1.Dock = DockStyle.Bottom;
tbctrl.Controls.Add(tabpg1);
string name;
name = abc.Text;
tabpg1.Text =name ;
}
private void sbtn_click(object sender, EventArgs e)
{
if(stb.Text!="")
this.ttb.Text = this.stb.Text;
}
jingePosted Nov 16, 2009, 3:05 AM