Get the Text of dynamically created button in C#? - c#

I am dynamically creating buttons in C# with this logic
for (int i = 1; i <= vap; ++i)
{
newButtons[i] = new Button();
newButtons[i].BackColor = Color.Gray;
newButtons[i].Name = "Button4" + i.ToString();
newButtons[i].Click += new EventHandler(NewButtons_Click);
newButtons[i].Location = new System.Drawing.Point(width,height);
newButtons[i].Size = new System.Drawing.Size(76, 38);
tabPage5.Controls.Add(newButtons[i]);
}
This is creating a button and the click event is also working but my problem is I don't know how to get the text of the newly created button. On form load I am putting the text of button from database and this also happening correctly, but I want to know how to get the text of dynamically created buttons.

You won't be able to get the text until after you populate it from the database (careful not to try and get the text too early).
But this should work:
string buttonText = FindControl("Button41").Text;
Update
Since you want the button text from within the click event, you can access the sender object:
Button button = sender as Button;
string buttonText = button.Text;

You just have to set the Text property of the button when you add it.

Using something along the lines of...
string BtnTxt = FindControl("ExampleButton1").Text;
should work fine.
This may cause problems later on however if you are trying to pull text content of buttons in a random order.

Related

How to find index of controls in flowlayoutpanel in C#

I am new to C#.
I have Form1 and flowlayoutpanel. I dynamically add Buttons to flowlayoutpanel and the buttons details comes from a database table.
I want to know the name of the first button in the flowlayoutpanel.
for (i = 0; i < DataTable.Rows.Count; i++)
{
Button btn = new Button();
btn.Name = DataTable.Rows[i]["Name"].ToString();
btn.Text = DataTable.Rows[i]["PostCode"].ToString();
flowlayoutpanel.Controls.Add(btn);
}
String First_Button_Name = ...........
If you want to get the name of the first button to be added to the FlowLayoutPanel, regardless of how those buttons got there, use:
string firstButtonName = flowlayoutpanel.Controls.OfType<Button>().FirstOrDefault()?.Name;
This is the value you are searching for:
DataTable.Rows[0]["Name"].ToString()
but make sure you have at least an element before you try to get this value.

Creating Dynamic Controller Names in C# with WinForms for TableLayoutPanel

While working on a small app that pulls test cases, runs, and results from an SQL Server Database, I encountered a dilemma in my methodology for attempting to create dynamic controller names in a TableLayoutPanel in WinForms. I am creating the rows dynamically when the user chooses the particular test case, and from there the TableLayoutPanel will open another window with the test steps preloaded and two radio buttons to indicate whether or not the test passed. My issue is that when I select one of the radio buttons on the right of the step, I get the same console read every single time. I need to be able to determine which exact radio button the user has pressed so I can therefore determine what row it's in and subsequently what test either passed or failed. My main code is as follows:
FormManualTest.cs (section when adding to the TableLayoutPanel)
private void addRowToolStripMenuItem_Click(object sender, EventArgs anotherEvent)
{
tableLayoutTest.RowStyles.Clear(); // Clear row styles to ensure a clean start when adding to the TableLayoutPanel
List<RadioButton> listOfRadioControls = new List<RadioButton>(); // Create array of radio buttons
List<UserCustomStep> listOfStepControls = new List<UserCustomStep>(); // Create array of custom controls
for (int i = 0; i < 5; i++)
{
UserCustomStep step = new UserCustomStep(Counter, "Step: " + i + " Push the button to elicit a response."); // Creates new user custom step control instance
RadioButton pass = new RadioButton();
pass.Text = "Pass";
pass.AutoSize = true;
RadioButton fail = new RadioButton();
fail.Text = "Fail";
fail.AutoSize = true;
fail.Margin = new Padding(3,3,20,3); // Needed to see the fail button without having to scroll over
listOfStepControls.Add(step); // Add step to UserCustomStep array
listOfRadioControls.Add(pass); // Add radio buttons to the RadioButton array
listOfRadioControls.Add(fail);
listOfRadioControls[i * 2].CheckedChanged += (s, e) => // Subscribes the pass radio button to listen for when a user has clicked on it
{
Console.WriteLine("Pass " + i + " was clicked");
};
listOfRadioControls[(i * 2) + 1].CheckedChanged += (s, e) => // Subscribes the fail radio button to listen for when a user has clicked on it
{
Console.WriteLine("Fail " + i + " was clicked");
};
tableLayoutTest.Controls.Add(listOfStepControls[i], 0, i); // Adds CustomStep to first column
tableLayoutTest.Controls.Add(listOfRadioControls[i*2], 1, i); // Adds Pass Radio Button to second column
tableLayoutTest.Controls.Add(listOfRadioControls[(i * 2) + 1], 2, i); // Add Fail Raido Button to third column
Counter++; // Increment couter to add subsequent steps underneath the previous ones.
}
}
Screenshots of App with Console Readout:
After Test Case Has Been Clicked and Radio Button Has Been Pressed
(From clicking this I would expect the console to read "Pass 1 was clicked")
Console Read:
Click Fail Button:
(I know from this image below that since the Pass button doesn't remain clicked I'm somehow using the same controller for all 5 of them)
Console Read
So from all of these issues that I've been presented with, I know that I'm somehow using the same controller for all 5 instances regardless of the fact that I'm storing everything in a controller array and grabbing from there. The for loop will have to be converted to a for each loop later, but that still doesn't solve my issue. I believe that if I could say something like:
RadioButton (pass+id) = new RadioButton();
or something similar while looping through to dynamically create the name for the controls, then each one would be a completely separate control and I could go from there. Any help would be greatly appreciated! I come from a heavy web background so my normal skills to remedy this in JS land aren't coming in handy as of right now. Thanks again for the assistance.
The Name property is optional, you don't need to specify it and it doesn't need to be unique. You can use property Tag for your own purpose (you can assign there ID or event instance of some object).
However you can also create your own control/usercontrol which encapsulate the whole row, and you can declare your own properties exactly for your purpose.

radiobuttonlist in updatepanel both dynamically created don't retain value on postback

I'm dynamically creating controls on my page (aspx) using the code below, but when the asynchronous postback triggers the radio button doesn't retain it's selection and the method uptheup doesn't get called. I'm guessing it's to do with the view state not returning the values, but I thought if the control ID was the same and it's created in Page_Init the in Page_Load the value should automatically be set from view state??
What I want is if someone selects No for the textbox to become visible. The control IDs are set from a database and are the same each time it loads as the code is used several times (I've replaced the IDs for easy reading below)
the following is called from Page_Init
RadioButtonList rbtnl = new RadioButtonList();
rbtnl.ID = "rbl_1";
rbtnl.Items.Add("Yes");
rbtnl.Items.Add("No");
rbtnl.AutoPostBack = true;
rbtnl.EnableViewState = true;
rbtnl.SelectedIndexChanged += new EventHandler(uptheup);
rbtnl.ClientIDMode = System.Web.UI.ClientIDMode.AutoID;
scriptmanager1.RegisterAsyncPostBackControl(rbtnl);
TextBox tbx = new TextBox();
tbx.ID = "tb-1";
tbx.CssClass = "form-control";
tbx.Visible = false;
UpdatePanel upx = new UpdatePanel();
upx.ID = "up-1";
upx.ContentTemplateContainer.Controls.Add(rbtnl);
upx.ContentTemplateContainer.Controls.Add(tbx);
upx.UpdateMode = UpdatePanelUpdateMode.Always;
upx.EnableViewState = true;
upx.ChildrenAsTriggers = true;
plcEvalBody.Controls.Add(upx);
OK so the code is fine and it works well, the issue was me assuming the IDs weren't the issue - they are - NEVER USE $ in control ID's. I'd used this to separate two sections of the ID and it is not valid.

Adding a twin tabPage to tabControl through a user command

I'm a newbie in c# and probably going to ask a very easy question, but I've not been able to find anything on the web to help.
I have a tabControl with a TabPage which is containing a TextBox object; this object, when the event "Text changed" is invoked, will perform the change of the parent tabPage's name.
The textbox where I typed "text changed by me" has a method which is managing changing the name of the tabPage:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (this.textBox1.Text != "")
this.tabControl2.SelectedTab.Text = this.textBox1.Text;
else
this.tabControl2.SelectedTab.Text = "(no name)";
}
Into the current page menu is contained a control to add a new page, which runs this method when the user click on it:
private void addNewPageToolStripMenuItem_Click(object sender, EventArgs e)
{
int numPagine;
string strPagine;
numPagine = this.tabControl2.TabCount;
strPagine = numPagine.ToString();
this.tabControl2.TabPages.Add("new page" + strPagine);
}
...and here is the output, which is expected since I'm just asking to add a new empty tabPage:
So, my question is: how can I make possible that when the user is clicking on "Add new page", rather than creating an empty new tabPage the program is rather creating a page like the first one (i.e. containing a textbox into the same position which has a method to change the text of the parent tabPage that I have just created?
Here is an example.
//..
// create the new page
TabPage tpNew = new TabPage("new page..");
// add it to the tab
this.tabControl2.TabPages.Add(tpNew);
// create one labe with text and location like label1
Label lbl = new Label();
lbl.Text = label1.Text;
lbl.Location = label1.Location;
// create a new textbox..
TextBox tbx = new TextBox();
tbx.Location = textBox1.Location;
tpNew.Controls.Add(lbl);
tpNew.Controls.Add(tbx);
// add code to the new textbox via lambda code:
tbx.TextChanged += ( (sender2, evArgs) =>
{
if (tbx.Text != "")
this.tabControl2.SelectedTab.Text = tbx.Text;
else
this.tabControl2.SelectedTab.Text = "(no name)";
} );
For more complicated layout you may want to consider creating a user control..
You also may want to create the first page with this code; the, of course with real values for text and positions!
For creating a UserControl you go to the project tag and right click Add-UserControl-UserControl and name it, maybe myTagPageUC. Then you can do layout on it like on a form. A rather good example is right here on MSDN
The problem is that is has no connection to the form, meaning you'll have to code all sorts of references to make it work..
I'm not really sure if you may not be better off writing a complete clonePage method instead. It could work like the code above, but would loop over the Controls of the template page and check on the various types to add the right controls..
It really depends on what is more complicated: the Layout or the ties between the pages and the form and its other controls..

How to change the location of a textbox

I have a problem, im making me own custom SharePoint webpart.
everything is going well, but the problem is that i can't figure out how to change the location of the textboxes and labels.
anyone knows how i can change the locations?
I am trying to accomplish it in C#.
problem SOLVED.
With the help of component ids. set position of that particular component.
"How to change the location of the textboxes and labels"
In this example i'm using a Button (Action performed on Button Click) and i am also adding how to Generate a TextBox and a Label (When you press this Button).
Just because this is usually a common process within setting locations to a control.
private void button1_Click(object sender, EventArgs e)
{
// Settings to generate a New TextBox
TextBox txt = new TextBox(); // Create the Variable for TextBox
txt.Name = "MyTextBoxID"; // Identify your new TextBox
// Create Variables to Define "X" and "Y" Locations
var txtLocX = txt.Location.X;
var txtLocY = txt.Location.Y;
//Set your TextBox Location Here
txtLocX = 103;
txtLocY = 74;
// This adds a new TextBox
this.Controls.Add(txt);
// Now do the same for Labels
// Settings to generate a New Label
Label lbl = new Label(); // Create the Variable for Label
lbl.Name = "MyNewLabelID"; // Identify your new Label
// Create Variables to Define "X" and "Y" Locations
var lblLocX = lbl.Location.X;
var lblLoxY = lbl.Location.Y;
//Set your Label Location Here
lblLocX = 34;
lblLoxY = 77;
// Adds a new Label
this.Controls.Add(lbl);
}
}
Note: This is just an example and will not work after postback.
I hope this answers to your and everyone's question.

Categories