Easiest way to make multiple checkboxes in Xamarin? C#/XML - c#

I need to create 8005 checkboxes in an app on Xamarin.
The app I am making takes thousands of phrases and pastes them into a text box so that the user can edit them.
The way I am going about this is by putting 27 buttons on the first page, one for each letter (an extra for numbers) and after selecting a number it brings you to another page with a checkbox for each phrase. After selecting each phrase that you want, there is a get results button to paste them all into a text editor.
I am having to copy and paste the checkbox code over and over and over, adding a different ID to each checkbox with a different phrase.
This is the code I am using:
<CheckBox
android:text="Living the American dream "
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/checkBox1" />

I haven't used xamarin, but I wonder if it would be better to use a list/datagrid/table where each row is a checkbox - you need only populate a collection with the data you want to display, and configure how the row is to be displayed. Then the checkboxes will be dynamically created. Furthermore, the list/datagrid/table will create a subset of rows only those that are visible - which will reduce your memory footprint.

If you want to create these many controls then it is better to create it dynamically using code rather then using xml. Just like this :
var checkBoxes = new CheckBox[0];
for (int i = 0; i <= 8005; i++)
{
var checkBox = new CheckBox(this);
checkBox.Text = "your phrase";
checkBox.LayoutParameters = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
parentLayout.AddView(checkBox);
Array.Resize(ref checkBoxes, i + 1);
checkBoxes[i] = checkBox;
}
And then you can get CheckBox list which is checked by user on your button click like this :
var selectedCheckBoxes = checkBoxes.Where(c => c.Checked);

Related

how to identify if multiple indexes are selected from the listbox control in c# winforms?

I am developing an application and I have a requirement to place the fields
on front end just like take checkbox. If the user selected particular fields on the checkbox, then based on the selection I will generate crystal report from sql database.
So up to 10 fields checkbox is good enough. But the fields are increased up to 30 and the checkbox count is also increased on the form.
So I decided to take listbox. But in listbox how to identify if
multiple items are selected from the user?
In the listbox, I have set SelectionMode property to MultiSimple.
But if I select two or more items, listbox takes only the index of the first item.
Code :
if(listbox1.SelectedIndex==0)
{
//my code for first field.
}
if(listbox1.SelectedIndex==1)
{
//my code for second field.
}
Note : I wrote a method for getting a dynamic sql query based on the user
selected items. So in my method createSQLquery(), I want to identify the
indexes.
I want to identify which items the user has selected from the frontend and based up on that i will proceed to write my code.
Thanks
There are three ways in which you can find
1)
foreach (object item in listbox.SelectedItems)
{
// do domething
}
2)
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
// do domething
}
}
3)
var selected = ListBox1.GetSelectedIndices().ToList();
var selectedValues = (from c in selected
select ListBox1.Items[c].Value).ToList();
You can use the ListBox.SelectedIndices property to get the indexes of multiple selected items.
Gets a collection that contains the zero-based indexes of all
currently selected items in the ListBox.

foreach record in database show in user control c#

I am trying to show data from a database to a user control I created which consists of TexBoxes and buttons. So far I am able to pull data from the database and show it on my user control but it only show the last record so only one record eventhough I know there is another record that exists. I want it to show multiple user controls with the amount of data(records) are in the database.
<%
List<ActionGame> actionGame = GameService.GetItems(id);
foreach (ActionGame GameItem in actionGame)
{
textBox1.Text = GameItem.Name;
textBox2.Text = GameItem.Type;
textBox3.Text = GameItem.Price;
}
%>
Like is there a way I could re use the user control multiple times or any other way I could show all the records from my database in the user control
Consider using a ListBox
What you're doing is, that you're always updating the Text Property of the TextBox with the last received data - overwriting the previous data of the record before.
Yes, it is possible, but it seems a little awkward to use textboxes for this.
You could just append to each textbox instead of overwriting in each iteration of the loop:
// Note: Clear the contents of each textbox before reaching this point
foreach (ActionGame GameItem in actionGame)
{
textBox1.Text += GameItem.Name + Environment.NewLine;
textBox2.Text += GameItem.Type + Environment.NewLine;
textBox3.Text += GameItem.Price + Environment.NewLine;
}
...but I would imagine this might give a messy result.
I would imagine some kind of list view would be better for your purpose.
Tip: See the documentation for ListView, and search for some examples that fit your need.
Another option is to use a Repeater. That should allow you to create as many textboxes as you need dynamically (example of usage).
For normal selects from database a Datagridview is very popular as the form of data displayed is exactly the same as in database workbench. What you are doing is replacing text in your Textboxes for each item in the list.

Reference a button as a variable for use in a loop

Hi Sorry if the answer has been given already, but I have searched high and low for this and have drawn a blank.
I have a grid of buttons that disappear when clicked. This is good. The buttons are named tile1, tile2, tile3 etc... their numbers are contiguous.
Their click_handlers currently each manually 'disappear' the sending buttons like this...:
tile1.visible = false
I am looking for a simple way to reference the tile names and numbers so I can
disappear the buttons in groups by referencing their names and numbers (ie disappear the 1st 3 in one command (or loop) followed by the next 2 together followed by the next 6 together... you get the picture).
Create a reset loop that resets the buttons' visibility property from within the code without having to manually type every button name.
So in an ideal world it might look something like...:
for (i=1; i<=MAX_TILE_NUMBER; ++i)
{
string currentTile = "tile" + i
currentTile.visible = false
}
I can feel it in my bones that there is an easy way to concatenate the string portion of the button name with the index "i" in my for loop and use this to reference the button controls one by one or in groups but I have not yet found it. please save me from imminent hair-rending insanity.
Many thanks.
Thanks
JB
Just put your buttons to a collection, a dictionary should fit best for you.
var buttons = new Dictionary<string, Button> { { "tile1", tile1 }, ... };
buttons.Add("tile2", tile2);
etc.
and then operate with this dictionary. For example, make the first three buttons invisible
var firstThree = buttons.Take(3);
foreach (var b in firstThree)
b.Value.Visible = false;
You can pick buttons however you wish by forming LINQ queries on this dictionary.
There is function to find control by name
see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.find%28VS.80%29.aspx

Coded UI Test - Click Control Based On Other Things On Table Row

im making a Coded UI test in Visual Studio 2010 for a web application in C# and i wish to click a button in the 6th column of a table based on the inner text of column 1? is this possible?
So for instance i have a table with Names in column one and other info and then a button in column 6. All auto generated.
Im assuming if i can get the Row number from the cell with say "John Smith" in it i can then press the button for this row in column 6.
Any ideas? i have tried googling and looking at what parameters i can pass in but im coming up stumped.
Something based on the following may help.
Access the HTML table by copying code from that generated by the Coded UI recorder for an assertion or a click on the cells. You should have something like:
HtmlCell theTable = new HtmlCell(this.UItheWindow.UItheTable.UIItemTable);
Cells of the table can be accessed by adding properties such as:
theTable.FilterProperties[HtmlCell.PropertyNames.RowIndex] = "0";
theTable.FilterProperties[HtmlCell.PropertyNames.ColumnIndex] = "6";
UITestControl cell = theTable.Find();
HtmlCell wantedCell = (HtmlCell)cell;
The above might be used as the body of a method returning the value of wantedCell. The name should now be available as:
wantedCell.InnerText;
Accessing the button to be clicked should follow a similar approach.
Another approach uses the GetChildren method to traverse the table. Start by getting theTable as above. Then have something like
UITestControlCollection children = theTable.GetChildren();
Loop through the children checking row properties. When the required row is found, call the GetChildren of that row and get the loop through them to find the required column. Some points: You may need to loop on columns before looping over rows. You may be able to directly index into the UITestControlCollection for the rows and the columns rather than needing to loop and check values. Depending on exactly how the table was written there may be additional levels between the table and the wanted cells, so you may need to examine children, grand children, great grand children, great great ..., and so on.
I have a couple of extension methods that I use to tackle content in tables(Hand coding, rather than using the recorder) -
This extension method for a table gets the first row in the table that contains the requested text in one of its cells or controls
public static HtmlRow GetRow(this HtmlTable table, string cellContent)
{
if((UITestControl)table == (UITestControl)null)
throw new ArgumentNullException("table");
if(cellContent.Length > 80)
cellContent = cellContent.Substring(0, 80); //Our table cells only display the first 80 chars
//Locate the first control in the table that has the inner text that I'm looking for
HtmlControl searchControl = new HtmlControl(table);
searchControl.SearchProperties.Add(PropertyNames.InnerText, cellContent);
//Did we find a control with that inner text?
if(!searchControl.TryFind())
{
//If not, the control might be an HtmlEdit with the text
HtmlEdit firstTableEdit = new HtmlEdit(table);
//Get all of the HtmlEdits in the table
UITestControlCollection matchingEdits = firstTableEdit.FindMatchingControls();
//Iterate through them, see if any have the correct text
foreach (UITestControl edit in matchingEdits)
{
if(cellContent == ((HtmlEdit)edit).Text)
searchControl = (HtmlControl)edit;
}
}
//We have(hopefully) found the control in the table with the text we're searching for
//Now, we spiral up through its parents until we get to an HtmlRow
while (!(searchControl is HtmlRow))
{
searchControl = (HtmlControl)searchControl.GetParent();
}
//The control we're left with should be an HtmlRow, and should be an Ancestor of a control that has the correct text
return (HtmlRow)searchControl;
}
Once you're able to get the correct row, it becomes relatively easy to get the correct control in that row(Or the correct control in a given cell in that row)
In your example, you've got a button in the 6th column of the row. The button probably has some properties associated with it, but even without them if we can correctly limit our search it will still work. Assume our table is named UITableCustomers - Declare a new button and limit it to only the 6th(Index 5) cell in the row containing "John Smith"
Mouse.Click(new HtmlInputButton(UITableCustomers.GetRow("John Smith").Cells[5]));
Obviously, this call is going to fail if the given text doesn't exist in a control in the table.
Your question is not very clear to me but check out the documentation on jQuery.trigger
This method will let you emulate a clickevent. I hope this is what you need.

User can't scroll down to all rows in datagridview

I have a simple form contain a textbox, gridview and a save button.
The user enter a customer name in the text box and press enter then i fetch data from database table then show them in the gridview and everything is good, but the problem accrued when the rows count is big and the scroll bar of the grid view appears so the user can scroll down to add new row or to browse couple last rows, but the surprise that i cant scroll down to all rows because the scroll bar only allow me to scroll to before the last couple or tree rows, so i have to select a row and then go down with down arrow on keyboard and it's so annoying and not professional.
Information might help that there is no spacial code that i use here to add rows to the grid just the normal way like this :
joinsGridView.Rows.Clear();
for (int i = 0; i < dataList.Count; ++i)
{
RegisterJointFormData item = dataList[i];
joinsGridView.Rows.Add();
DataGridViewComboBoxCell joinNameCombo = joinsGridView.Rows[i].Cells["JoinName"] as DataGridViewComboBoxCell;
joinNameCombo.Value = dataList[i].Join.JoinTypeID;
joinTypeBindingSource.Position = joinTypeBindingSource.IndexOf(item.Join.JoinType);
joinsGridView.Rows[i].Cells["JoinPrice"].Value = item.Join.JoinType.Price;
joinsGridView.Rows[i].Cells["Discount"].Value = item.Join.Discount;
joinsGridView.Rows[i].Cells["PayedMoney"].Value = item.PayedMoney;
joinsGridView.Rows[i].Cells["RegisterDate"].Value = item.Join.RegisterDate.ToShortDateString();
joinsGridView.Rows[i].Cells["Duration"].Value = item.Join.JoinType.Duration;
joinsGridView.Rows[i].Cells["SessionsAttened"].Value = item.SessionsAttend;
}
Another info that i am using Entity Framework to connect to database and get data.
I hope i described the problem right and sorry for my bad English :)
At last i found the problem and it's very silly as i expected, the problem was that i am disabling the grid at form load and after the user enter customer name i fill the grid with data then enable it and it's fixed by enabling the grid first then add the data to the grid.

Categories

Resources