I will sound like absolutely a noob, but I am so stressed out that I am not able to do any research properly.
Basically I got 127 road names in a list, and I want to display them one by one in a random sequence in a label and I will answer them if its right one increment to green box if its wrong then increment to yellow box but at same time it will display me right answer in a label and then on pressing submit button it will take me to next road name, here is what I have done until now,
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
RL = RoadLocationNames();
i = 1;
red = 1;
y = 1;
}
List<KeyValuePair<string, string>> RL;
int i,red, y;
private void button1_Click(object sender, EventArgs e)
{
Random random = new Random();
int r = random.Next(RL.Count);
lbLocation.Text = RL.ElementAt(r).Key;
if (tbRoad.Text.ToLower() == RL.ElementAt(r).Value.ToLower())
{
Green.Text = i.ToString();
i++;
RL.Remove(RL.ElementAt(r));
}
else
{
label3.Text = RL.ElementAt(r).Value.ToString();
Red.Text = i.ToString();
i++;
}
Yellow.Text = y.ToString();
y++;
}
public List<KeyValuePair<string, string>> RoadLocationNames()
{
List<KeyValuePair<string, string>> RLNs = new List<KeyValuePair<string,string>>();
RLNs.Add(new KeyValuePair<string, string>("Road Name", "Location Name"));
return RLNs;
}
Now My internet is so slow that I barely can upload any screenshot of my form but I will try to.. I got exam tomorrow and I want to do preparation using this app, but I am not sure about,
How to display a road name instead of (road name and its answer which is what my code is doing now) it could be because I am doing everything in buttton submit, I need help with logic and code (sorry but am in rush)
Yes, it's because you're doing everything in your submit. I suggest you split your code into the following:
Initialisation - create the list of road names and locations, then run setup (next step).
Setup - Clear the user's current answer, pick a road name/location pair randomly and display the question part on the form.
User submission - check answer, increment appropriate counter, if the answer is right run setup again, otherwise display what the answer should have been.
Initialisation can be done in your form constructor. Setup should be a private method. User submission should be your click handler.
(It looks like you're incrementing i for both right and wrong answers which is probably incorrect. This is why you pick better variable names than a single letter to make mistakes like that more obvious.)
Related
i have a problem in Unity3d scripts.
I'm trying to make a sort of combination to open a box.
To open this box, i have to insert a combination of 3 buttons correctly.
This 3 buttons(That are simple GameObject, already placed in my scene) have already an animation, when my character collide with one of them, this one will fall down (same animation to other 2).
So, the combination that i want to insert is "the first correct button is "Button n*2", the second correct button is "Button n*1" and the third correct button is "Button n*3"", but i really don't have idea of how i have to do this.
I tried with if statements but if for example the combination is 123-312-123 the animation of the boxes will show up.
I want that only if i do the combination 213 the box is open, than if i go wrong i have to repeat the combination.
Can anyone help me?
Simple way, have a collection of right sequence:
int[] solution = new int[]{2,1,3};
then anytime a button is used, add its value to another collection:
List<int> sequence = new list<int>();
void OnPress(int buttonValue)
{
if(sequence.Contains(buttonValue)){ return; } // Don't add twice
sequence.Add(buttonValue);
if(sequence.Count == solution.Length)
{
if(CompareSequence())
{
// win
}
else
{
sequence.Clear();
}
}
}
bool CompareSequence()
{
// this should not be since we checked before but just to be sure
if(solution.Length != sequence.Count){ return false; }
for(int i = 0; i < solution.Length; i++)
{
if(solution[i] != sequence[i]){ return false; }
}
return true;
}
Each action on button would pass its own value that gets added to the list.
When the list and the solution are same length, they get compared. If they are same, you move to win section, if not, the sequence is cleared and user needs to refill content.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a list with names. These names are linked to a label. When I click the label a name at random designated for that label is selected.
What I need is for when a button is clicked, it selects a random name for each label.
for example;
I have 5 names in a list and each individual list is linked to an individual label. One name will be selected for a label at random. I need all 10 of my labels to select a random name, when a button is clicked.
Hope that makes sense. I am using Visual Studio 2010 and c# on a form. Many Thanks
The question is very vague.
However, a method to choose a random string from a list of strings would look like this:
public string ChooseRandomName(List<string> names)
{
Random rnd = new Random();
return names[rnd.Next(0, names.Count)];
}
Now, the only thing to do is to call this method with the proper input and attribute the output to the proper label. Not sure how your labels are given, but something like:
label1.Text = ChooseRandomName(listOfNamesForLabel1);
I hope this helps.
public string ChooseRandomName(List<string> names,Random rnd)
{
return names[rnd.Next(0, names.Count)];
}
Random rnd = new Random();
label1.Text = ChooseRandomName(listOfNames,rnd);
label2.Text = ChooseRandomName(listOfNames,rnd);
Dont create intialize random object every time. other wise it will give same value. check this link
So this is what I have. This is part of the code I need (dont worry with what its about). The goalkeeper label is called "goalkeeper" my button should be called "pickTeam" but when clicked shows up as "button1_Click" private void button1_Click(object sender, EventArgs e)
I need it so that when the button clicked, every label (I can do myself once figured out) will choose a random name that has been given. I.E; Begovic, De Gea etc etc.
i have a method for each position. This is the Goalkeeper, once I know this I can do it for the other positions. Thanks
private void goalkeeper_Click(object sender, EventArgs e)
{
Random rand = new Random();
List<string> goalkeepers = new List<string>();
goalkeepers.Add("Neuer");
goalkeepers.Add("De Gea");
goalkeepers.Add("Lloris");
goalkeepers.Add("Begovic");
for (int i = 0; i < 4; i++)
{
int index = rand.Next(0, 4);
goalkeeper.Text = goalkeepers[index];
}
}
I'm a C# student and I'm a little stuck at on my midterm project.
I dropped my project and spec here: https://www.dropbox.com/sh/eo5ishsvz4vn6uz/CE3F4nvgDf
If you run the program, it will come to the last area I left off at..
private void btnAddScore_Click(object sender, EventArgs e)
{
for (int i = 0; i < 3; i++)
{
tempScore = Convert.ToDecimal(txtScore.Text);
Form1.scoreList = tempScore; (was Form1.scoreList[i] = tempScore;)
}
txtScoresList.Text += Convert.ToString(tempScore) + " ";
}
There's a main form, a secondary add form, and a third and fourth form, all the controls are in place, just the wiring is what's left over.
(1) In the above code, there are supposed to be 3 scores passed to the main form, which, along with a student name string, are to populate the ListBox on the main form. I can't figure out how to access that ListBox, anytime I type "listStudents" nothing happens.
(2) I'm also not sure how to limit an input of only 3 scores when I'm clicking the "add" button 1 time, which means I know my for loop is probably completely wrong. I don't know if I should save those scores to an array, list, or individual vars, being that it can be 3 (or more, but 3 is fine) scores.
(3) When I hit "OK" on the AddNewStudent form, do I write my code there to populate the main form ListBox, or does it go in the main form?
Update:
private void Form1_Load(object sender, EventArgs e)
{
lbStudents.Items.Clear();
//something like
foreach (decimal i in scoreList2)
{
scoreList = scoreList2.ToString(); //gives me a cannot implicitly convert error
}
lbStudents.Items.Add(tempInfo1 + " " + scoreList2);
}
//I want the listbox to populate like "Name - |100| |90| |80|"
This code seems to me, to be correct, for getting the ListBox populated, but I'm unsure of how to add the entire contents of the list to a string, and then add that to the listbox.
This will get your code building and running.
Change the following declaration in form1
public static decimal[] scoreList = new decimal[3];
to
public static List<decimal> scoreList = new List<decimal>();
and update your btnAddScore_Click handler to
//save scores to temp static var, populate noread txtbox txtScoresList with scores
for (int i = 0; i < 3; i++)
{
//save score to static var for trans-form data sending
tempScore = Convert.ToDecimal(txtScore.Text);
Form1.scoreList.Add(tempScore);
}
The rest is not too difficult, you should be able to work it out.
I'm not very experienced on c#. I'm working with winforms and I'm looking for a way to create something like a list of elements with this template , something like the autocompletion list of visual studio.
Is it possible to do? Shall I use listbox or listview?
EDIT
Sorry my question wasn't clear I don't want to create an autocomplete but what i want to create is something like this a list of things with an icon next to the text of that thing.
As I understand from your question, you can create custom UserControl or create a Form and put ListBox in it. If you use From be sure that you change border style layout, just set it to none. After creation for use it you should create form and show it where you want like this:
FrmAutoComplete x = new FrmAutoComplete();
x.Show();
you can put this form in ToolTipItem and show it.
Good luck.
THis is a quick and dirty example of using images in your Listview control. Since I don;t have a lot of information about what you plan to do, I tried to keep is simple.
In short, you need to load some images into one of the ImageLists (Large or Small) built into the Listview control and assign them keys so that you can assign them to specific list items as you add them.
The trick to this is determining which image to use for a specific list item (assuming there are different images assigned to different list items depending on some differentiating factor. For this example, I used an arbitrary assignment of "cars" or "trucks," and simply decided that the first five items in the list would be cars, and the last five would be trucks. I then assigned each image appropriately, using the image key as I added each listview item. You can do this for more complex scenarios, and when using the image key, it does not matter what order the items are added.
For this use case, you will want to create or use images with dimensions of 16 x 16 pixels. I went ahead and added two images to my project resource file, then simply accessed them using the project Properties.Resources name space. There are other ways to do this as well, but this is the most convenient for me.
Hope that helps.
public partial class Form1 : Form
{
static string CAR_IMAGE_KEY = "Car";
static string TRUCK_IMAGE_KEY = "Truck";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.SetupListview();
this.LoadListView();
}
private void SetupListview()
{
var imgList = new ImageList();
imgList.Images.Add("Car", Properties.Resources.jpgCarImage);
imgList.Images.Add("Truck", Properties.Resources.jpgTruckImage);
var lv = this.listView1;
lv.View = View.List;
lv.SmallImageList = imgList;
}
private void LoadListView()
{
for(int i = 1; i <= 10; i++)
{
string currentImageKey = CAR_IMAGE_KEY;
if(i > 5) currentImageKey = TRUCK_IMAGE_KEY;
var item = this.listView1.Items.Add("Item" + i.ToString(), currentImageKey);
}
}
Working with a WPF application.
I am very much wonderin if it is possible to get a function luke the examle below into a class function (im not yet very experianced with C#).
private void counter01_Tick(object sender, EventArgs e)
{
if (counter01Ticks > 0)
{
//subtract 1 each time
counter01Ticks--;
//subtrack 1 secon each time
counter01Span = counter01Span.Subtract(TimeSpan.FromSeconds(1));
//update the progressbar
progBar01.Value++;
//get the % to show
progBar01Text.Text = Convert.ToString(Math.Round(((progBar01.Value / progBar01.Maximum) * 100), 0)) + "%";
//Label1 will show the count down.
string countDown = counter01Span.ToString();
TimeRemain01.Content = countDown;
}
else
{
counter01.Stop();
resetCounter01();
WarningMessage msgWarnOne = new WarningMessage();
msgWarnOne.warnMessage.Text = Properties.Settings.Default.msgScout01;
msgWarnOne.ShowDialog();
}
}
It is just a part of a counter. but i want to add more counters to my application later on.
Therefore i marked all the parameters with a number (01) in my code.
So what i do not want to do, i copy-paste the code and change the number for every counter, but rather have the number as a input number or something.
Would that be possible?
if i9 would understand it for this small part of code, i think i will be able to do it with the other parts too (above is only the tick form a counter).
#Users that user the answer below:
http://www.c-sharpcorner.com/uploadfile/mahesh/user-control-in-wpf/
Has helped me understand it better and migh be usefull to read too.
Yes you can put all of this (together with your XAML declaration) into a WPF-user control and put multiple of these into other Windows/Controls/...
Just look at the tutorial I linked in - should explain everything you need.
You can use method Control.FindControl;
Another approach is to make user control - and therefore you will work with only 1 array of controls in every counter_Tick