How do I store first control in flowLayoutPanel c#? - c#

I am new to C# and I am using windows forms.
I have flowLayoutPanel and I programmatically add some buttons to it .
What I am trying to do is: I want to save the first button located in the flowLayoutPanel into ButtonToSave object.
flowLayoutPanel1.FlowDirection= FlowDirection.LeftToRight
private void AddButtons_Click(object sender, EventArgs e)
{
Button btn = new Button();
flowLayoutPanel1.Controls.Add(btn);
}
private void StoreTheFirstButton_Click(object sender, EventArgs e)
{
Button ButtonToSave = new Button();
ButtonToSave = "First button in flowLayoutPanel1"
}
Anyone knows how to save the first button located in flowLayoutPanel1 into ButtonToSave when StoreTheFirstButton event is raised?
Thank you

Try code below. You may want to look at my response at following posting (How to (create and) add components to a Table Layout?) :
private void AddButtons_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn.Click += new EventHandler(StoreTheFirstButton_Click);
flowLayoutPanel1.Controls.Add(btn);
}
private void StoreTheFirstButton_Click(object sender, EventArgs e)
{
Button button = sender as Button;
Button ButtonToSave = button;
//ButtonToSave = "First button in flowLayoutPanel1";
}

Related

copy paste and cut clicks do not exist in current context

Am creating a context menu programmatically as to have right click options on my datagrid. Here is my code:
public partial class Form1 : Form
{
//string fileExcel;
public Form1()
{
InitializeComponent();
fillCari();
FillCombo();
ContextMenuStrip mnu = new ContextMenuStrip();
ToolStripMenuItem mnuCopy = new ToolStripMenuItem("Copy");
ToolStripMenuItem mnuCut = new ToolStripMenuItem("Cut");
ToolStripMenuItem mnuPaste = new ToolStripMenuItem("Paste");
//Assign event handlers
mnuCopy.Click += new EventHandler(mnuCopy_Click);
mnuCut.Click += new EventHandler(mnuCut_Click);
mnuPaste.Click += new EventHandler(mnuPaste_Click);
//Add to main context menu
mnu.Items.AddRange(new ToolStripItem[] { mnuCopy, mnuCut, mnuPaste });
//Assign to datagridview
dataGridView1.ContextMenuStrip = mnu;
}
This particular block I keep getting the error that it does not exist in current context. Any idea why?
mnuCopy.Click += new EventHandler(mnuCopy_Click);
mnuCut.Click += new EventHandler(mnuCut_Click);
mnuPaste.Click += new EventHandler(mnuPaste_Click);
Updated question:I created the strips from the context menu but not sur ehow to implement the copy commands.
public partial class Form1 : Form
{
//string fileExcel;
public Form1()
{
InitializeComponent();
fillCari();
FillCombo();
ContextMenuStrip mnu = new ContextMenuStrip();
dataGridView3.ContextMenuStrip = mnu;
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
}
I had figured it out for awhile now. Hopefully it helps anyone in the future. Keep in mind after creating your contextMenuStrip and adding a "copy" field to the strip you will then bind it to the dataGridView. This is done by going into properties of the context menu and looking for the dataGridView name and bind it to it. After that open the click event in properties on the contextMenu and add this code in it:
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
dataGridView3.Select();
DataObject o = dataGridView3.GetClipboardContent();
Clipboard.SetDataObject(o);
}
You should now have your right click copy button working.
For the click event you need event handlers:
// event click
mnuCopy.Click += new EventHandler(mnuCopy_Click);
mnuCut.Click += new EventHandler(mnuCut_Click);
mnuPaste.Click += new EventHandler(mnuPaste_Click);
// event handler method
void mnuPaste_Click(object sender, EventArgs e) {
// paste logic
}
void mnuCut_Click(object sender, EventArgs e) {
// cut logic
}
void mnuCopy_Click(object sender, EventArgs e) {
// copy logic
}

Troubles with dynamic c# asp.net controls update

I'm currently working on a website using c# and asp.net. For this purpose, I need to create dynamic controls but I enconter some issues. I already read official documentation and searched for lots of tutorial but unfortunately, no one allowed me to fix this problem.
Here is a very simplified example of what I'm trying to do;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
CreateControls();
else
UpdatePage();
}
protected void CreateControls()
{
Button button1 = new Button();
button1.ID = "_Button1";
button1.Text = "Button1";
button1.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button1);
Button button2 = new Button();
button2.ID = "_Button2";
button2.Text = "Button2";
button2.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button2);
}
protected void UpdatePage()
{
Button button1 = ((Button)_Panel.FindControl("_Button1"));
button1.Text = "I went through UpdatePage and changed";
Button button2 = ((Button)_Panel.FindControl("_Button2"));
button1.Text = "I went through UpdatePage and changed";
}
protected void _ClickEvent(object sender, EventArgs e)
{
}
The aim here would just be to change the buttons' text when clicking on one of them. "Page_Load" method is correctly called as well as the "UpdatePage" one, but in the latter, Button1 and Button2 controls have disappeared (they are not in the panel controls anymore) and an NullPointer exception is obviously raised.
Would anybody have an explanation ? I know I probably missed something about page life cycle but could not find any clear solution anywhere.
Thanks a lot !
You are creating the controls the first time the page is loaded, but the Page_Load event is too late to add controls to the page and have WebForms know about that.
On the initial page load, somewhere between the OnInit and Page_Load, WebForms makes a note of what controls are currently on the page and sets them up in the view state and all of that stuff, so that they next time you post back it knows those controls should be there. If you don't add your controls until Page_Load, WebForms isn't really paying attention any more to what you're adding to the page, so they next time you post back it doesn't know to put those controls on the page.
Move your CreateControls call into the OnInit method. This will tell WebForms at the appropriate time to create the controls (about the same time that any controls from the .aspx markup get added, although slightly later). Then WebForms will be aware of those controls and will apply any view state necessary (if it's a postback), and then finally on Page_Load you can muck with the control data with your UpdatePage call.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateControls();
}
Think of OnInit as "put all of the controls on the page and wire up event handlers".
Think of Page_Load as "put data into the controls that are already there".
The controls that you are creating dynamically will be lost on postback. Try this:
protected void Page_Load(object sender, EventArgs e)
{
CreateControls();
UpdatePage();
}
protected void CreateControls()
{
Button button1 = new Button();
button1.ID = "_Button1";
button1.Text = "Button1";
button1.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button1);
Button button2 = new Button();
button2.ID = "_Button2";
button2.Text = "Button2";
button2.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button2);
}
protected void UpdatePage()
{
Button button1 = ((Button)_Panel.FindControl("_Button1"));
button1.Text = "I went through UpdatePage and changed";
Button button2 = ((Button)_Panel.FindControl("_Button2"));
button1.Text = "I went through UpdatePage and changed";
}
protected void _ClickEvent(object sender, EventArgs e)
{
}
Will try it:
protected String TextButton1
{
get { return (String) ViewState["TextButton1"]; }
set { ViewState["TextButton1"] = value; }
}
protected String TextButton2
{
get { return (String)ViewState["TextButton2"]; }
set { ViewState["TextButton2"] = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateControls();
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
UpdatePage();
}
}
protected void CreateControls()
{
Button button1 = new Button();
button1.ID = "_Button1";
button1.Text = String.IsNullOrEmpty(TextButton1) ? "The First Value" : TextButton1;
button1.Click += new System.EventHandler(_ClickEvent1);
_Panel.Controls.Add(button1);
Button button2 = new Button();
button2.ID = "_Button2";
button2.Text = String.IsNullOrEmpty(TextButton2) ? "The First Value" : TextButton2;
button2.Click += new System.EventHandler(_ClickEvent2);
_Panel.Controls.Add(button2);
}
protected void UpdatePage()
{
Button button1 = ((Button)_Panel.FindControl("_Button1"));
button1.Text = String.IsNullOrEmpty(TextButton1) ? "The First Value" : TextButton1;
Button button2 = ((Button)_Panel.FindControl("_Button2"));
button2.Text = String.IsNullOrEmpty(TextButton2) ? "The First Value" : TextButton2;
}
protected void _ClickEvent1(object sender, EventArgs e)
{
TextButton1 = "test";
Button b = (Button) sender ;
b.Text = TextButton1;
}
protected void _ClickEvent2(object sender, EventArgs e)
{
TextButton2 = "test";
Button b = (Button)sender;
b.Text = TextButton2;
}

add a button click event dynamically in asp.net 4.5 c#

I have some questions to this post [1]: How can i create dynamic button click event on dynamic button?
The solution is not working for me, I created dynamically an Button, which is inside in an asp:table controller.
I have try to save my dynamic elements in an Session, and allocate the Session value to the object in the Page_Load, but this is not working.
Some ideas
edit:
...
Button button = new Button();
button.ID = "BtnTag";
button.Text = "Tag generieren";
button.Click += button_TagGenerieren;
tabellenZelle.Controls.Add(button);
Session["table"] = table;
}
public void button_TagGenerieren(object sender, EventArgs e)
{
TableRowCollection tabellenZeilen = qvTabelle.Rows;
for (int i = 0; i < tabellenZeilen.Count; i++)
{
...
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["table"] != null)
{
table = (Table) Session["table"];
Session["table"] = null;
}
}
}
It is not a good practice to store every control into Session state.
Only problem I found is you need to reload the controls with same Id, when the page is posted back to server. Otherwise, those controls will be null.
<asp:PlaceHolder runat="server" ID="PlaceHolder1" />
<asp:Label runat="server" ID="Label1"/>
protected void Page_Load(object sender, EventArgs e)
{
LoadControls();
}
private void LoadControls()
{
var button = new Button {ID = "BtnTag", Text = "Tag generieren"};
button.Click += button_Click;
PlaceHolder1.Controls.Add(button);
}
private void button_Click(object sender, EventArgs e)
{
Label1.Text = "BtnTag button is clicked";
}
Note: If you do not know the button's id (which is generated dynamically at run time), you want to save those ids in ViewState like this - https://stackoverflow.com/a/14449305/296861
The problem lies in the moment at which te button and it's event are created in the pagelifecycle. Try the page_init event for this.
Create Button in page load
Button btn = new Button();
btn.Text = "Dynamic";
btn.Click += new EventHandler(btnClick);
PlaceHolder1.Controls.Add(btn)
Button Click Event
protected void btnClick(object sender, EventArgs e)
{
// Coding to click event
}

Create a button which creates button

I am a begginer in C# and I want to create a button which creates button.
but these buttons never appear...
please find my code :
private void addstrat3_i_Click(object sender, EventArgs e)
{
panel3strat.Width += 200;
Button addstrat3_2 = new Button();
addstrat3_2.Size = new Size(210, 41);
addstrat3_2.Location = new Point(50,50);
addstrat3_2.Visible = true;
}
Thanks a lot
You have to add the button (or any other controls) on the form using the Controls property, for sample:
private void addstrat3_i_Click(object sender, EventArgs e)
{
panel3strat.Width += 200;
Button addstrat3_2 = new Button();
addstrat3_2.Size = new Size(210, 41);
addstrat3_2.Location = new Point(50,50);
addstrat3_2.Visible = true;
// add control
this.Controls.Add(addstrat3_2);
}
You need to add the button to the form.
this.Controls.Add(addstrat3_2);

how to create dynamic link labels in window form application

On clicking Go button the name given in textbox should be displayed as a link label and this should increment dynamically.
Here my code:
private void buttongo_Click(object sender, EventArgs e)
{
linkLabelName.Text = textBoxName.Text;
}
private void btnGo_Click(object sender, EventArgs e)
{
LinkLabel link = new LinkLabel();
link.Text = txtText.Text;
panTable.Controls.Add(link);
}

Categories