i am programming in Visual Studio 2013, c# winform. Is it posible to choose click event for each button in FLP? Im trying to do something like Steam Library, I already did a lot of things and it now looks like i want to.
This is how it looks (adding)
This is how it looks (library)
(sorry, im not able to add images)
But i don't know how to open choosed game when you click on a button in FLP (in library).
This is how my code looks:
private void btnTest_Click_1(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
if (textBox2.Text != "")
{
Button btn = sender as Button;
Button btnNew = new Button();
btnNew.Text = "";
btnNew.Height = 108;
btnNew.Width = 230;
btnNew.Image = new Bitmap(textBox1.Text);
btnNew.FlatStyle = FlatStyle.Flat;
flpContainer.Controls.Add(btnNew);
btnNew.Click += btnNew_Click;
counter1++;
label1.Text = counter1.ToString(); //label1 is that "Number of games in library:"
System.Windows.Forms.MessageBox.Show("Game was succesfully added to library!");
}
else if (textBox2.Text == "")
{
System.Windows.Forms.MessageBox.Show("You didn't choosed exe file!");
}
}
if (textBox1.Text =="")
{
System.Windows.Forms.MessageBox.Show("You didn't choosed image!");
}
}
private void btnNew_Click(object sender, EventArgs e)
{
Process.Start(textBox2.Text); //textbox2 is the path to exe file, but it change when you want to add another game to library
}
But how to do different click event for each button in FlowLayoutPanel?
Thank you for answers.
EDIT: I want to do that, when you click on a button in library it will open that game (program).
Thank you a lot!
Store the required information in Tag property for future use and get the job done.
Button btnNew = new Button();
btnNew.Text = "";
btnNew.Height = 108;
btnNew.Width = 230;
btnNew.Image = new Bitmap(textBox1.Text);
btnNew.FlatStyle = FlatStyle.Flat;
flpContainer.Controls.Add(btnNew);
btnNew.Click += btnNew_Click;
btnNew.Tag = textBox2.Text;// <--Store it in Tag
Then use it in Click event
private void btnNew_Click(object sender, EventArgs e)
{
Button clickedButton = (Button)sender;
Process.Start((string)clickedButton.Tag);
}
Related
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";
}
I am trying to do a simple World of Warcraft profile picture "extractor". Here is my code:
private void pictureBox1_Click(object sender, EventArgs e)
{
if (textBox4.Text != "")
{
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate("https://worldofwarcraft.com/en-gb/search?q=" + textBox4.Text);
webBrowser1.DocumentCompleted += GetImg;
}
}
private void GetImg(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string result = "";
foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("div"))
if (el.GetAttribute("className") == "Avatar-image")
{
result = (el.OuterHtml).Substring(el.OuterHtml.IndexOf("https"));
result = result.Substring(0, result.IndexOf(")"));
pictureBox1.ImageLocation = result;
DialogResult YESNO = MessageBox.Show("Is this your character?", "Select your char", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (YESNO == DialogResult.Yes)
{
break;
}
}
}
}
The problem is that I click the image once and if the character is the right one I click "Yes" and everything is good. However if I click the image a second time the message box will pop up, then I chose my answer BUT the message box pops up again. If I am to click the image a third time, the message box will appear 3 times...
Any ideas?
You are subscribing to the event an additional time each time you click it on this line:
webBrowser1.DocumentCompleted += GetImg;
I have two Buttons on my Sharepoint 2010 Web Page. One fires (server-side), the other doesn't*. They are set up similarly. Here is the one that does fire:
Button btnSave = new Button();
btnSave.Text = "Save";
btnSave.Click += new EventHandler(btnSave_Click);
this.Controls.Add(btnSave);
. . .
private void btnSave_Click(object sender, EventArgs e)
{
LiteralControl message = null; // this breakpoint is hit, sho' nuff
. . . // code elided for brevity
}
...and here is the one that isn't invoked:
Button btnGeneratePDF = null;
. . .
btnGeneratePDF = new Button();
btnGeneratePDF.Text = "Generate PDF";
btnGeneratePDF.UseSubmitBehavior = true; // trying this after it didn't work without it... (still doesn't work)
btnGeneratePDF.Click += new EventHandler(btnGenPDF_Click);
this.Controls.Add(btnGeneratePDF);
private void btnGenPDF_Click(object sender, EventArgs e)
{
GeneratePDF(listOfListItems); // breakpoint here never reached
}
Why is btnSave's handler invoked when I click it, but btnGeneratePDF's handler is not?
I can get buttons to fire client-side (jQuery) by using HtmlButtons, but I need this one to fire server-side/C#.
UPDATE
Would the fact that I'm creating the pdfgen button inside the click handler of the other Button have anything to do with this? Here's the significant part of that:
private void btnSave_Click(object sender, EventArgs e)
{
try
{
. . . relatively insignificant code elided for brevity
Button btnGeneratePDF = new Button();
btnGeneratePDF.Text = "Generate PDF";
btnGeneratePDF.UseSubmitBehavior = true; // trying this after it didn't
work without it...still doesn't work; don't know why - configuration of this
button is the same as for btnSave
btnGeneratePDF.Click += new EventHandler(btnGenPDF_Click);
this.Controls.Add(btnGeneratePDF);
. . . relatively insignificant code elided for brevity
?
UPDATE 2
Only the first (btnSave) button (and not the identically-created btnGeneratePDF button) is marked as type="submit":
<input type="submit" name="ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$ctl152" value="Save" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$ctl152", "", true, "", "", false, false))" />
I believe only one button of type submit is allowed, but I tried to shove btnSave out of the way when I was done with it by setting its Enabled property to false, but that did nothing (good).
You must recreate the dynamic control in page processing. Here is an example of how to do it:
protected void Page_Load(object sender, EventArgs e)
{
Button b = new Button();
b.ID = "one";
b.Text = "one";
b.Click += B_Click;
this.form1.Controls.Add(b);
// this is where the dynamic control is created if id is in postback
if (Request["two"] != null)
{
Button x = new Button();
x.ID = "two";
x.Text = "two";
x.Click += X_Click;
this.form1.Controls.Add(x);
}
}
private void B_Click(object sender, EventArgs e)
{
Button x = new Button();
x.ID = "two";
x.Text = "two";
x.Click += X_Click;
this.form1.Controls.Add(x);
LabelOutput.Text += " ...one";
}
private void X_Click(object sender, EventArgs e)
{
LabelOutput.Text += " ...two";
}
If you assign the click event handler of the "pdf" button inside the click event of the Save button means that the PDF button is dynamically created and its click event assigned each time you press click on the save button. Only at that time the pdf will get its event assigned.
This is how I got it to work:
0) Declare button at the top:
Button btnGeneratePDF = null;
1) Create and configure it within the Page_Load() event (as suggested by Michael Palermo)
btnGeneratePDF = new Button();
btnGeneratePDF.Text = "Generate PDF";
btnGeneratePDF.UseSubmitBehavior = true; // I don't know if this is necessary
btnGeneratePDF.Click += new EventHandler(btnGenPDF_Click);
btnGeneratePDF.Visible = false;
this.Controls.Add(btnGeneratePDF);
Note that visible is set to false at first
2) Make the "Generate PDF" button visible within the "btnSave" click handler:
btnGeneratePDF.Visible = true;
That's it - it works fine now.
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;
}
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
}