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.
Related
Basically I'm making a program that allows you to add to a stackpanel another stackpanel with several horizontally aligned textboxes with the press of a button. So far, everything is working as intented. Here's my code so far ,Stacker is the name of the parent stackpanel and it starts off empty:
private void Add_Click(object sender, RoutedEventArgs e)
{
Stacker.Children.Add(NewXD(Stacker.Children.Count + 1));
}
public System.Windows.Controls.StackPanel NewXD(int XD)
{
System.Windows.Controls.StackPanel NewP = new StackPanel();
NewP.Orientation = Orientation.Horizontal;
System.Windows.Controls.TextBox HAHA = new TextBox();
HAHA.Name = "question" + XD.ToString();
//HAHA.Text = HAHA.Height.ToString()+" "+HAHA.Width.ToString();
HAHA.Height = Double.NaN;
HAHA.Width = 120;
HAHA.FontSize=20;
NewP.Children.Add(HAHA);
for (int i = 1; i < 6; i++)
{
System.Windows.Controls.TextBox newBox = new TextBox();
newBox.Name = "answer"+XD.ToString()+"_"+i.ToString();
newBox.Height = Double.NaN;
newBox.Width = 120;
NewP.Children.Add(newBox);
}
System.Windows.Controls.ComboBox correct = new ComboBox();
correct.Name = "correct" + XD.ToString();
for (int i = 1; i < 6; i++)
{
System.Windows.Controls.ComboBoxItem newItem = new ComboBoxItem();
newItem.Name = "ans" + XD.ToString() + "_" + i.ToString();
newItem.Content = i.ToString();
correct.Items.Add(newItem);
}
NewP.Children.Add(correct);
return NewP;
}
I apologize for the lack of seriousness in some of my code.
Now, what I need to do is for the child stackpanels to also contain independent file pickers that work like the one sampled in this thread: Open file dialog and select a file using WPF controls and C#
What I don't know how to perform is that each of these generated buttons have the same basic funcion but are linked with each of their corresponding textbox.
Thanks in advance :)
Edit: As I was writing this it occured to me that perhaps I could use the help of the child stackpanel's array-like properties to choose the corresponding textbox, because the file selector's textbox and button will always be the last two items in the stackpanel, but I'm not very sure how to perform this.
For functionality you can create an EventHandler that will be assigned to each button. Your event handler will then open File Dialog...
Buttons have Tag property which you could use to identify your TextBoxes, or you could derive from Button class and add AssociatedTextBox property for example.
I have an application in which there are several tabs. One of those creates several group boxes and in each of these group boxes I need 10 radio buttons ranging from 1-10. My problem is that I cannot get the radio button to show up and work properly. When I create them if I add them to the current tabs controls all the radio buttons will display but the winform treats them all as one set of radio.
I need the radio buttons in each groupbox to be a set. If I add the buttons to the groupbox the radio buttons will not display. I have played around with the order in which I add the radio button to the groupbox, call the radio buttons show() method, add the groupbox to the tabs control, and call the groupbox's show() method but no matter what configuration I try these in I can't seem to get the radio buttons to display. I also tried to change the childIndex of the radio button but that didn't work either.
Some of you may suggest to just use a drop down or upDownNumaric but I actually have the UpDownNumaric working but the customer wants it changed to a set of radio buttons. The code I currently have:
groupBoxLocation.Y += 45;
GroupBox newGroupBox = new GroupBox();
newGroupBox.Location = groupBoxLocation;
newGroupBox.Text = reader["Description"].ToString().Trim();
newGroupBox.Size = new Size(425, 40);
newGroupBox.Name = ("PS_L_" + newGroupBox.Text).Replace(" ", "").Trim();
RadioButton rateValue;
radioButtonsLocation = new Point(newGroupBox.Location.X - 30, newGroupBox.Location.Y + 15);
tabControl1.TabPages[3].Controls.Add(newGroupBox);
newGroupBox.Show();
for (int i = 0; i < 10; ++i)
{
rateValue = new RadioButton();
radioButtonsLocation = new Point(radioButtonsLocation.X + 41, radioButtonsLocation.Y);
rateValue.Location = radioButtonsLocation;
rateValue.Text = (i + 1).ToString().Trim();
rateValue.Width = 40;
rateValue.Name = "PI_V_" + newGroupBox.Text.Replace(" ", "") + "_" + i;
newGroupBox.Controls.Add(rateValue);
newGroupBox.Controls[rateValue.Name].Show();
}
The problem is your initialization of the radioButtonsLocation. The locations are relative to their parent, not relative to the root container, so try changing
radioButtonsLocation = new Point(newGroupBox.Location.X - 30, newGroupBox.Location.Y + 15);
to
radioButtonsLocation = new Point(0,10);
or some similar point based on how you would like your UI to look.
I am adding the dynamically TextBox in the placeholder on the button click in that.
When all the textboxes are loaded I am making changes in the values and again Press another Button to save the values to the SharePoint List. But when I press the Save button and I checked the placeholder by debugging it I found that there were no any control in the placeholder.
I am adding the Controls like follows :
TextBox[] tb = new TextBox[item_ans.Count];
Literal[] lt = new Literal[item_ans.Count];
for (int j = 0; j < item_ans.Count; j++)
{
ans_id.Add(item_ans[j]["ID"].ToString());
tb[j] = new TextBox();
tb[j].ID = "tb_ans" + (j + 1).ToString();
tb[j].Text = item_ans[j]["Title"].ToString();
lt[j] = new Literal();
lt[j].Text = "<br/>";
pl_hd_ans.Controls.Add(tb[j]);
pl_hd_ans.Controls.Add(lt[j]);
}
And on the Save Button click I am Retrieving those TextBoxes Like follows:
int n = Convert.ToInt32(ViewState["totalAns"].ToString());
foreach (var i in ans_id)
{
var item_ans = list_ans.GetItemById(i);
clientContext.Load(item_ans);
clientContext.ExecuteQuery();
for (int k = 0; k < n; k++)
{
TextBox tb = (TextBox)pl_hd_ans.FindControl("tb_ans" + (k + 1).ToString());
item_ans["Title"] = tb.Text;
item_ans.Update();
clientContext.Load(item_ans);
clientContext.ExecuteQuery();
}
}
But in this I check the Placeholder's Controls that were 0. Can Any one please help me to solve this..?
I'm assuming its ASP.NET WebForms what we're talking about here.
When you are adding controls dynamically to the webpage, you have to recreate them on each sequential postback. The reason for this, is that the dynamically created controls are not present in the .aspx file, nor in the viewstate, so asp.net cannot know that it has to recreate these controls. Therefore, you yourself have to recreate them in the initialized-event (before the page-load), including adding any event handlers that you need.
You can google about it.
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.
I would like to know how it's possible to change button properties by a code when we don't know a button name while writing it.
For example, I have a loop like this:
for (int i=0; i<5; ++i) {
int buttonName = "button_" + i;
buttonName.enabled = false;
}
Thanks in advance!
You can access the Controls collection of the parent containing the button like this:
if(parent.Controls.ContainsKey(buttonName))
{
Button myButton = (Button)parent.Controls[buttonName];
myButton.Enabled = false;
}
This will need a little extra work if your buttons are not contained within the same parent; ie. some buttons on a Form, some buttons on a Panel contained within that same form.