Display a string item from a list without repetitions - c#

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

Related

C# Windows Forms(Non-SQL): Populate text box on new form with an integer +1 above the highest number currently in a DataGridView row

I've working on a C# Forms application, the MainScreen contains a DataGridView in which the first row is an auto-generated incremented number. When manually coding data into the DataGridView, I'm able to set this ID field with the incremented number.
What I'm trying to do now is create an "Add" form, I need the first text box to be populated with the next incremented number when it opens.
What is the simplest way to know what the current last number used in the DataGridView is?
For example, if I have 2 rows, obviously the new "Add" row would have an ID of 3, but how can I code the Text box to dynamically grab that information and increment +1 each time the form is opened?
Edit:
To provide more context, I will have a button for Add, and a button for Modify. The modify button will not open the Modify form unless a row is selected. Here is my code to populate the text box on the modify form, based on the selected row:
ModPartIDBox.Text = Inventory.AllParts[Inventory.CurrentIdxPrt].PartID.ToString();
The "Add" form obviously doesn't check and doesn't care if a row is selected, so I can't lean on just calling that already filled field, so I just need a way to grab highest number.
Keep a list of all objects that you are going to store in your DataGridView so you can access their data later on, such as:
var items = new List<MyObject> {}
Where MyObject looks a little something like this:
public class MyObject
{
public int PartId { get; set; }
}
and then when you need to pull the highest number, you can use a LINQ expression:
int highestNumber = items.OrderByDescending(x => x.PartId).First().PartId;

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"

Uncheck Dropdown Item at Runtime in Unity

This seems trivial but I'm having difficulty.
A user selects an item in a dropdown and this creates a little checkmark next to the item.
I want to deselect it in my code and remove that checkmark.
Any ideas?
You can change which item is selected with Dropdown.value. At-lease, one item must be selected.
I want to deselect the item selected. Restore it back to it's original
state.
Get the original item in the Start or Awake function:
public Dropdown dropDown;
private int originalState;
void Awake()
{
originalState = dropDown.value;
}
When you want to restore it back, restore to that value you saved:
void restoreDropDown()
{
dropDown.value = originalState;
}
Unfortunately original state of a dropdown is dropdown.value = -1 and there is no way resetting it once modified. It is always greater than 0 once modified, even if you assign -1 to it.
The only workaround is to create a prefab of Dropdown and Destroy & Instantiate it from prefab when resetting. In this case you need to create all the listeners dynamically from the code which renders all editor assignments of a Dropdown useless. You need to use an initializer script.

Visual Basic Express 2013 - List Boxes

I have to write a program to determine which students have a specified major based upon the button that is clicked. For example, if the Search Computer Science button is clicked, then the program should pull all the CS student's names and put them in the results List box. The text box above the results list box should contain the specified major being searched and the text box below should contain a message indicating the total number of students with that specific major. Can someone help me please? Not even sure where to start. Thank you!
I can't upload the picture of my form because of low reputation. but the form has 3 list boxes. The first list box (lstStudents) has all of the students' names. The second list box (lstMajors) is the majors each student belong in. So, the student in Items(0) of the lstStudents list box, is in the major at Items(0) of the lstMajors list box.
Not enough info to provide a proper answer, but this is what you need to do:
When a button is clicked, you need to get the major.
Then you need to iterate through all the options in the Majors listbox, and if it matches the major in step 1, then copy/move the student in the students listbox that is at the same position, into the results listbox.
Set the value of the top textbox to the major you determined in step 1.
Set the value of the bottom textbox the the number of students in the results listbox.
Make sense?
Good luck with your assignment!

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

Categories

Resources