Reference a button as a variable for use in a loop - c#

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

Related

How can I know the index of duplicated text from a selectedtext? [duplicate]

Ok, I'm trying to do something a little specific here. I want to get the location of the selected text in a textbox.
To elaborate- I can use location to select text. If I have a textBox1 I could do:
textBox1.SelectionStart = 1;
textBox1.SelectionLength = 4;
That would start at the second letter and select 4 letters.
What I want to do is the opposite: when the user selects text, I want to find out what the start is and what the length is (or what the start is and what the end is. Either will work).
I thought about just searching the string for the selectedtext (textBox1.SelectedText). The problem comes if it is a common word or a string that is used multiple times. For instance.
This is a cat. This is a cat. This is a cat.
If they select the second sentence, using SelectedText to search the string for that specific sentence does me no good. It could be either of the 3.
So, my question is: When the user clicks a button, how do I determine the exact elements that are selected by the user, so that I can later manipulate those specific elements? Important to note the later part- I likely will not only want to manipulate the text when the button is pressed. I will also want to manipulate it later, at a time when the text may no longer be highlighted. This means I'll want to store SOMETHING to tell me what specific parts of the sentence I'm dealing with. If that solution isn't viable, is there a solution you can think of where, in the above "this is a cat" example, the user could select the second sentence, hit a button, and then later I know which sentence was selected when he hit that button?
According to the documentation, SelectionStart and SelectionLength can be both set and read. Just use those.
You dont even need to know the position of selected text to manipulate them, to edit the text that you have selected in the text you can simple set the SelectedText property to the new edited value.
// if textBox1.text = "Hello World World"; with first "World" selected
textBox1.SelectedText = textBox1.SelectedText.Replace("World", "Raj");
// then it becomes "Hello Raj World"

Display a string item from a list without repetitions

I'm making a little quiz game with multiple choices for practicing. Each answer button has a random item taken from a string list. I mean, the list contains elements and with this script I "paste" a random string element from that list in the UI text box to show an answer:
public Text answerText;
[SerializeField]
private int randomAnswerIndex;
void Start () {
randomAnswerIndex = Random.Range(0, languageAnswers.Count);
answerText.text = languageAnswers[randomAnswerIndex];
}
Each answer button has this script referenced, but I don't know how to avoid that an item text from the list shows twice, i.e., the word "English" in two different buttons at the same time. How can I solve it? If it's with an "if" statement, how can I access to the different button texts from the script? Thanks!
You can remove a string at the selected index each time a string was selected for an answerText.
Make sure to do a copy to don't loose the inital list of strings in case of reuse and that all scripts will take the same list.
languageAnswers.Remove(randomAnswerIndex);

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

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);

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.

Listbox item stay focused after selection

I don't know if the title express what I want. I have a ListBox in WPF where I generate many elements. When I click on a element while still generating I want my selected item to not move down the list, so I cannot see it anymore, I want to stay in the exact position where I click on it.
If this is possible, can someone point some ideas on how to do it in C#?
Thanks.
Assuming that this is even a good idea and that you are using winforms
Step 1:
Determine the index of the selected item in the source.
Step 2:
When your adding items to the ListBox split the ListBox at the index where the item previously was insert the item at that point, then add on the remainder of the items, while making sure that you've removed the item if it is now elsewhere in the list.
Code:
//Let's assume that you know how to get the position of the item when it is clicked and save the
//item to a variable called OriginalItem
public void PutTheItemInTheSameSpot()
{
var listboxitems = (List<Integer>)YourListBox.DataSource;
var originalClikedItem = OriginalItem;
var topPart = new List<Integer>();
for (i = 0; i < itemPosition; i++)
{
topPart.Add(listboxItems[i]);
}
topPart.Add(originalClickedItem);
var bottomPart = listboxitems.Remove(toppart);
YourListBox.DataSource = toppart.AddRange(bottomPart);
}
Saw your edit about it being WPF
The could should work in idea.
Just a thought: you could try having your view respond to an event whenever an item is added to your ListBox. In the event handler, you could force the selected item to scroll into view presumably keeping it in the current "viewable" position:
listBox.ScrollIntoView(listBox.SelectedItem);
I've never tried this before so it may or may not produce the desired affect?

Categories

Resources