Are the names of the textboxes inside a stack panel global variables? - c#

I would like to dynamically create a series of StackPanels which include several TextBoxes and TextBlocks. I need to be able to get their values, so do I have to make their names globally unique or just unique within the StackPanel?

You cannot access to the TextBoxes which is added to the StackPanel children collection like a normal TextBox. But you can get their values by their name like this:
private static string GetChildValue(Panel panel, string name)
{
var children = panel.Children;
foreach (var child in children)
{
if (!(child is TextBox))
continue;
var textBox = (TextBox)child;
if (textBox.Name.Equals(name))
return textBox.Text;
}
return string.Empty;
}
private static void DoSomething()
{
var stackPanel = new StackPanel();
stackPanel.Children.Add(new TextBox { Name = "TextBox1", Text = "TextBox1 Value" });
stackPanel.Children.Add(new TextBox { Name = "TextBox2", Text = "TextBox2 Value" });
stackPanel.Children.Add(new TextBox { Name = "TextBox3", Text = "TextBox3 Value" });
stackPanel.Children.Add(new TextBox { Name = "TextBox4", Text = "TextBox4 Value" });
var textBox1Value = GetChildValue(stackPanel, "TextBox1");
}

Related

How to access entrys created from the xaml.cs file

I've made a void method that adds pages to my tabbed page. In this, it creates entry boxes but they are unnamed. After the person has filled in the entry boxes I want to create a report from it. How do I access the entry boxes for the information?
This is the part that adds the new entry:
grid.Children.Add(new Entry
{
AutomationId = "weerstand" + lusnummer.ToString(),
Text = "[weerstand]",
TextColor = Color.Black,
FontSize = 18,
}, 2, rownumber);
//button and bottom
Button Reportbutton = new Button
{
Text = "Report",
BackgroundColor = Color.FromHex("#093d80"),
Padding = 24,
TextColor = Color.White,
FontSize = 36,
TextTransform = TextTransform.None,
};
Reportbutton.Clicked += GenerateReport_OnClicked;
grid2.Children.Add(
Reportbutton, 0, 0);
//make page
StackLayout stacklayout1 = new StackLayout()
{
Children =
{
grid,
grid2
}
};
ScrollView pagescroll = new ScrollView()
{
Content = stacklayout1
};
ContentPage page = new ContentPage()
{
Title = "LDTB-" + number.ToString(),
Content = pagescroll
};
Children.Add(page);
}
private async void GenerateReport_OnClicked(object sender, EventArgs e)
{
//yes or no
bool answer = await DisplayAlert("Rapport", "Wilt u het rapport maken", "Yes", "No");
//Generate report
if (answer == true){
//WHAT GOES HERE TO ACCESS THE Entry?
}
}
}
You can traverse your Grid and access the value of entries in it.
You can refer the following code:
var children = grid.Children;
foreach(View child in children){
if (child is Entry) {
string value = ((Entry)child).Text;
System.Diagnostics.Debug.WriteLine("one value is = " + value);
}
}

How to use Eto.Forms TreeGridView

I've been at this for a while now and cannot seem to figure out how to get the Eto.Forms TreeGridView Control to properly render. I'm trying to just add a few GridViewItem's at the moment and I just get a small gray bar at the top:
Here is my code:
List<ITreeGridItem> treeGridItems = new List<ITreeGridItem>();
foreach (var contentType in contentTypes)
{
treeGridItems.Add(new TreeGridItem(contentType.Name));
}
Content = new DocumentPage(new TreeGridView
{
DataStore = new TreeGridItemCollection(treeGridItems)
}, new Padding(20));
I'm not even really sure where to start, I just want to get a tree with text to show for each node at the moment and I can't even do that.
After a bit of trial and error I figured out how to use the tree view:
var treeGridView = new TreeGridView
{
BackgroundColor = Colors.White
};
treeGridView.Columns.Add(new GridColumn
{
HeaderText = "Content Type",
DataCell = new TextBoxCell(0)
});
treeGridView.Columns.Add(new GridColumn
{
HeaderText = "Create",
DataCell = new CustomCell
{
CreateCell = r =>
{
TreeGridItem item = r.Item as TreeGridItem;
ContentTypeTag tag = (ContentTypeTag)item.Tag;
var contentType = _siteManager.CurrentSite.ContentTypes.First(x => x.Name.Equals(tag.ClassName));
void Click(object btnSender, EventArgs btnArgs)
{
//Your Event
}
var button = new LinkButton
{
Style = "primary-link-btn",
Text = $"Create {contentType.Name.ToSentenceCase()}",
Command = new Command(Click)
};
return button;
}
}
});
treeGridView.Columns.Add(new GridColumn
{
HeaderText = "Show All",
DataCell = new CustomCell
{
CreateCell = r =>
{
TreeGridItem item = r.Item as TreeGridItem;
ContentTypeTag tag = (ContentTypeTag)item.Tag;
var contentType = _siteManager.CurrentSite.ContentTypes.First(x => x.Name.Equals(tag.ClassName));
void Click(object btnSender, EventArgs btnArgs)
{
//Your Event
}
var button = new LinkButton
{
Style = "primary-link-btn",
Text = $"Show All {contentType.Name.ToSentenceCase()}",
Command = new Command(Click)
};
return button;
}
}
});
var treeGridItemCollection = new TreeGridItemCollection();
foreach (var contentType in _siteManager.CurrentSite.ContentTypes)
{
var item = new TreeGridItem
{
Values = new string[] { contentType.Name.ToSentenceCase(), "Create", "Show All" },
Tag = new ContentTypeTag
{
ClassName = contentType.Name
}
};
treeGridItemCollection.Add(item);
}
treeGridView.DataStore = treeGridItemCollection;
You create the header columns to start and then create a TreeGridItemCollection and set the datastore to that. The values for each column of the row is set in a string array to the Values property of the TreeGridItem.

Filling a DataGrid with selfmade arraylist

I'm making a generic converter which reads any file you throw in it which "maps" it's data and gives it like this:
I got 3 classes:
DataValue, which has a value, row, and column property.
DataArray, which is a list of DataValues.
DataArrayList, which is a list of DataArrays.
I try to use put it in a datagrid with non pre-setup columns, since it should be able to read Anything.
Here's some code:
public void FillRows(DataArrayList arrayList)
{
DataArray headers = arrayList.First();
AddColumns(headers);
DataList = new List<DataArray>();
lijstitems = new List<DataValue>();
foreach (DataArray dataArrays in arrayList)
{
DataList.Add(new DataArray { datavalues = dataArrays.datavalues });
}
DataContext = this;
}
Now this gives me the general name of the arrays, as a test i tried to display it in textboxes. Now i wonder how i would get a setup which displays each DataValue as a new column.
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataArray array = (DataArray)DataGrid.SelectedItem;
var arraychar = array.datavalues.ToArray();
textBox1.Text = arraychar[0].value.ToString();
textBox2.Text = arraychar[1].value.ToString();
textBox3.Text = arraychar[2].value.ToString();
textBox4.Text = arraychar[3].value.ToString();
textBox5.Text = arraychar[4].value.ToString();
}
This displays the correct values i put in.
Define a column per value you want to display in the DataGrid:
DataGrid.AutoGenerateColumns = false;
DataGrid.Columns.Add(new DataGridTextColumn() { Binding = new Binding("[0]") });
DataGrid.Columns.Add(new DataGridTextColumn() { Binding = new Binding("[1]") });
DataGrid.Columns.Add(new DataGridTextColumn() { Binding = new Binding("[2]") });
DataGrid.Columns.Add(new DataGridTextColumn() { Binding = new Binding("[3]") });
DataGrid.Columns.Add(new DataGridTextColumn() { Binding = new Binding("[4]") });

How to bind CheckBoxes from GridView to source DataTable?

I have to create some form, in which users can create multiple tests for some data. If user want to validate data, he must check Checkbox in appropiate row and column for a test and then, click button titled "Validate tests". If user want to add new test (i.e. Test2), he must press "Add test" button.
All data are stored into DataTable (dtTests) and displaying by GridView (dgvTests) on aspx page.
My question is, how can i get checked-values for each checkboxes ?
Example website:
In Example, user add two tests (look on image: http://i.stack.imgur.com/cR8l5.jpg).
In Test1, he want to validate years: 1990, 1993, 1999
In Test2, he want to validate years: 1990, 1993, 1998, 1999
New Test column in dtTests:
DataColumn dc = new DataColumn("Test" + i.ToString())
{
DataType = typeof(Boolean),
ReadOnly = false
};
dtTests.Column.Add(dc);
I add new TestX column to gridView in this way:
TemplateField cbxTest = new TemplateField();
cbxTest.HeaderTemplate = new CheckBoxTemplate(DataControlRowType.Header, "Test" + i.ToString(), "Test" + i.ToString());
cbxTest.ItemTemplate = new CheckBoxTemplate(DataControlRowType.DataRow, "Test" + i.ToString(), "Test" + i.ToString());
dgvTests.Columns.Add(cbxTest);
Here's class for CheckBoxTemplate:
public class CheckBoxTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string bindName;
public CheckBoxTemplate(DataControlRowType _type, string _colName, string _bindName)
{
templateType = _type;
columnName = _colName;
bindName = _bindName;
}
public void InstantiateIn(Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal() { Text = String.Format("<b>{0}</b>", columnName) };
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
CheckBox cbx = new CheckBox();
cbx.DataBinding += new EventHandler(this.cbx_DataBinding);
container.Controls.Add(cbx);
break;
default:
break;
}
}
private void cbx_DataBinding(Object sender, EventArgs e)
{
CheckBox cbx = (CheckBox)sender;
GridViewRow row = (GridViewRow)cbx.NamingContainer;
cbx.Checked = Convert.ToBoolean(DataBinder.Eval(row.DataItem, bindName).ToString());
}
}
Here is how you can go about it, first assign an id to your checkboxes cbx.ID = "cbxDoTest.
Then add a RowCommand event handler that will be triggered when your Validate tests button is clicked. Here you need to traverse the e.Row.Cells properties, if the cell belonged to a test column then do a search on the checkbox control (Checkbox cbx = e.Row.Cell[i].FindControl("cbxDoTest"); then check if the cbx.Checked is true or false.
I wrote the flow and how to do it from my head so double check the method/property names.

Creating an ASP.NET Repeater Dynamically in C# Bound to an Object List

I've got a very simple object:
public class DocumentType
{
private int id;
private string name;
public int ID
{
get { return this.id; }
set { this.id = value; }
}
public string Name
{
get { return this.name; }
set { this.name = value; }
}
}
I've got a list of DocumentType objects: List<DocumentType> documentTypes = getDocuments();
I'm working on a custom control where I'm trying to dynamically create a repeater and dynamically bind it to my object list. Here's my code:
private Repeater docList;
docList = new Repeater();
docList.DataSource = documentTypes;
docList.DataBind();
foreach (RepeaterItem repeatItem in docList.Items)
{
// if condition to add HeaderTemplate Dynamically only Once
if (repeatItem.ItemIndex == 0)
{
RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header);
HtmlGenericControl hTag = new HtmlGenericControl("h4");
hTag.InnerHtml = "Header";
repeatItem.Controls.Add(hTag);
}
// Add ItemTemplate DataItems Dynamically
RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);
Label lbl = new Label();
// This part is completely broken!
lbl.Text = string.Format("Content: {0} {1} <br />", (DocumentType)repeaterItem.DataItem).ID, repeaterItem.NamingContainer);
repeatItem.Controls.Add(lbl);
// Add SeparatorTemplate Dynamically
repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Separator);
LiteralControl ltrlHR = new LiteralControl();
ltrlHR.Text = "<hr />";
repeatItem.Controls.Add(ltrlHR);
}
The header and separator work great. I can't figure out how to bind the item template to the current item to get it to display. I know what I have in there right now is completely broken, but I've tried several variations with no luck.
Thanks in advance for any help or pointers in the right direction!
The problem you are having is that you are assuming that the RepeaterItem contains the data. It does not. It contains the information on how to display the individual item. You need to use that index to get back into the data source. I'm not sure if there's a better way, but below is how I got it to work...
List<DocumentType> documentTypes = new List<DocumentType>();
documentTypes.Add(new DocumentType(){ ID=1, Name="Bob"});
documentTypes.Add(new DocumentType() { ID = 2, Name = "Tom" });
documentTypes.Add(new DocumentType() { ID = 3, Name = "Chick" });
Repeater docList = new Repeater();
docList.DataSource = documentTypes;
docList.DataBind();
foreach (RepeaterItem repeatItem in docList.Items)
{
int index = repeatItem.ItemIndex;
DocumentType docType = ((IList<DocumentType>)docList.DataSource)[index];
// if condition to add HeaderTemplate Dynamically only Once
if (index == 0)
{
HtmlGenericControl hTag = new HtmlGenericControl("h4");
hTag.InnerHtml = "Header";
repeatItem.Controls.Add(hTag);
}
// Add ItemTemplate DataItems Dynamically
RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);
Label lbl = new Label();
// This part is completely broken!
lbl.Text = string.Format("Content: {0} {1} <br />", docType.ID, repeaterItem.NamingContainer);
repeatItem.Controls.Add(lbl);
// Add SeparatorTemplate Dynamically
LiteralControl ltrlHR = new LiteralControl();
ltrlHR.Text = "<hr />";
repeatItem.Controls.Add(ltrlHR);
}
StringBuilder sb = new StringBuilder();
docList.RenderControl(new HtmlTextWriter(new StringWriter(sb)));
Text = sb.ToString();

Categories

Resources