issue to fill the positions of a one-dimensional array on c# - c#

I´m trying to fill positions on one dimesional array in C#, using three controls: one button, one textbox and one label.
public partial class Form1 : Form
{
string[] VECT;
int i = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
VECT = new string[4];
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = "";
VECT[i] = Convert.ToString(textBox2.Text);
i++;
label5.Text = Convert.ToString(i);
}
}
When the code is compiled, I start to insert alphanumeric values on the textbox, clicking the button to fill this array, but when loading is done, the array doesn't preserve data on it's positions.
Please, I need your help to do a wrigth loading on this array.
Thank you.

Related

Displaying combobox items into graph

I'm trying to show my combobox items (Numbers) from Form1 into a graph in Form2, but I can't get the value's to show in the graph. After multiple tries this is where I'm at right now, but I just can't figure out how to make it work.
Code in Form2:
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
for (int i = 0; i < frm1.comboBox1.Items.Count; i++)
{
int Value = Convert.ToInt16(frm1.comboBox1.Items);
chart1.Series["Saved Results"].Points.AddXY(0, Value);
}
}
When I click on button1 nothing happens :(. Can you help me out? Thanks!
You are converting the item collection to an Int16, which is likely nothing. Use the indexer on the items collection.
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
for (int i = 0; i < frm1.comboBox1.Items.Count; i++)
{
int Value = Convert.ToInt16(frm1.comboBox1.Items[i]);
chart1.Series["Saved Results"].Points.AddXY(0, Value);
}
}

How to display input from textbox in a multiple lined textbox?

I need to make a program that takes the input of a textbox (tb_1) and puts it in the second textbox (tb_2, multilined) when a button is pressed. It also needs to paste the code a specific amount of times, depending on the numericupdown digit. I have come this far, what do i do next? Thanks in advance.
(Note: it should probably be done within a class.)
{
public Form1()
{
InitializeComponent();
}
private void tb_1_TextChanged(object sender, EventArgs e)
{
}
private void btn_1_Click(object sender, EventArgs e)
{
string tekst = tb_1.Text;
tb_2.Text = tekst + Environment.NewLine;
tb_1.Clear();
tb_2.Text = tekst + Environment.NewLine;
}
}
}

How to print an array in forms (C#)

I have a mathematical problem and I´m trying to solve it, the problem is that you have 81 coins, but one is fake and it´s heavier than the others,you have to find out which one is the fake one by using a scale and doing only 4 comparisons.
I´m trying to make it like a game, when a users decides which coin will be the fake one, and the other player has to find it.
I made an array named monedasf and made all the values 0, so when the users type in the coin that wants to be the fake one, the value changes to 1. I´m trying right now to print the array, but I don´t know if I have to print it in a multiline textbox or where, here´s the code I have untill now.
public partial class Form1 : Form
{
public static int[] monedasf = new int[81];
public Form1()
{
InitializeComponent();
for( int i = 0; i<=80;i++)
{
monedasf[i] = 0;
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
int n;
n = Convert.ToInt32(textBox1.Text);
monedasf[n] = 1;
textBox1.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i <= 80; i++)
textBox2.Text = Convert.ToString(monedasf[i]);
}
}
I have only BASIC KNOWLEDGE of programming, that´s why my code might be so primitive :D
Try using something like this:
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i <= 80; i++)
textBox2.Text += monedasf[i].ToString() + " ";
}
If you want you can replace " " with any separator you want, like "\n" for newline.
Actually your code would work too, the problem is you were reseting textBox2's text by using assign operator:
textBox2.Text = Convert.ToString(monedasf[i]); // will clear and then print
textBox2.Text += Convert.ToString(monedasf[i]); // will not clear and print
All you need is not to reset previous text inside.
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = string.Join(", ", monedasf);
}
Use string.Join which is very useful for display.

Quiz in Windows Forms. Check if button is right answer or not

I'm doing a "Who wants to be a millionaire"-like game and I'm stuck at how to check if the answer is the right answer or not.
I've made a struct that I can use to create some questions which looks like this:
struct Question
{
public string question, ansA, ansB, ansC, ansD;
public int correctAnswer;
}
Then I make questions like this:
Question ett = new Question();
ett.question = "Who is the richest?";
ett.ansA = "XXXX";
ett.ansB = "god";
ett.ansC = "vit";
ett.ansD = "röd";
ett.correctAnswer = 1;
And after that I put them in a list and retrieve a random question from there. The question in a label and the answers in four different buttons.
Random rnd = new Random();
int number = rnd.Next(0, NumberOfquestions+1);
var question = QuestionList[number];
lblquestion.Text = question.question;
button1.Text = question.ansA;
button2.Text = question.ansB;
button3.Text = question.ansC;
button4.Text = question.ansD;
The variable names aren't 100% correct because I've changed them for you to understand them since I have the variables in my language.
I'm stuck on how to check if the button clicked is right or not, and I have some thoughts about it but I'm unsure if that will work or if there are any better ways to do it.
One thing I thought was to make a method that check if its right, if the button return a value between 1-4 and check it against my int correctAnswer in the struct.
public int button1_Click(object sender, EventArgs e)
{
return 1;
}
Another thing I thought about was if I could make a method including all of the right answers in the game and search through if that match the text on my button?
Maybe not the best method to use.
namespace quiz
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
programmet();
}
public void btnNewGame_Click(object sender, EventArgs e)
{
}
public void BtnAvsluta_Click(object sender, EventArgs e)
{
this.Dispose();
this.Close();
}
public void programmet()
{
Question ett = new Question();
ett.fråga = "who is richest ?";
ett.svarA = "XXXX";
ett.svarB = "god";
ett.svarC = "vit";
ett.svarD = "röd";
ett.rättsvar = 1;
var QuestionList = new List<Question>();
QuestionList.Add(ett);
QuestionList.Add(tre);
QuestionList.Add(fyra);
QuestionList.Add(fem);
Random rnd = new Random();
int number = rnd.Next(0, numberOfQuestions -1);
var currentQuestion = QuestionList[number];
lblFråga.Text = currentQuestion.fråga;
button1.Text = currentQuestion.svarA;
button2.Text = currentQuestion.svarB;
button3.Text = currentQuestion.svarC;
button4.Text = currentQuestion.svarD;
}
public void button1_Click(object sender, EventArgs e)
{
/// here I want to use CurrentQuestion.question etc.
/// to do the "if(currentquestion.ansA == 1)"
}
}
}
Here is a possible solution:
Define a variable to store the current question (currentQuestion) that is accessible from the Click event handler of each button and from the method in which you select the question.
public void button1_Click(object sender, EventArgs e) {
if (currentQuestion.correctAnswer == 1)
CorrectAnswer();
else
WrongAnswer();
}
public void button2_Click(object sender, EventArgs e) {
// same for button2 ...
}
You could assign the Tags of your answer buttons, so button1.Tag = 1, button2.Tag = 2 etc. Then, for all your answer buttons, set their Click event to the same method, e.g.
void Answer_Click(object sender, EventArgs e)
In the method body, you can then access the Tag of the clicked button using...
int selectedAnswer = (int)((Button)sender).Tag;
To access your current question, make a private property or field in your Form1 class:
private Question CurrentQuestion;
and in your programmet() method, assign to it like this:
this.CurrentQuestion = QuestionList[number];
...so you can use
if (CurrentQuestion.correctAnswer == selectedAnswer) { ... }
Another way is when you're loading the answers onto the form set the appropriate button's tag to true and the rest false. Now in the common click event handler check the tag value of sender cast to a button and you have whether the answer is right
Why not just create two methods called CorrectAnswer() and IncorrectAnswer() and call the appropriate one from each button click. like so.
public void button1_Click(object sender, EventArgs e) {
//if answer is correct
CorrectAnswer();
public void button2_Click(object sender, EventArgs e) {
//if answer is incorrect
IncorrectAnswer();
hope this helps

How to loop usercontrol

I have problem displaying usercontrol,I have a usercontrol which has a panel called rowpanel which has textbox and combobox,Now ,when I Click button_1,I want the usercontrol to be displayed on each click,it is like adding a row on each click,I just don`t know how to loop it,I tried using indexing...
CODE
private void button1_Click(object sender, EventArgs e)
{
AddRow add = new AddRow();
show_pnl.Controls.Add(add);
}
AddRow is usercontrol ...this is a windows application,can I get some help please,
The reason is because they are overlapping each other. To fix it increment the top &/or left as shown here:
private const int gap = 20;
private int count = 0;
private void button1_Click(object sender, EventArgs e)
{
var add = new UserControl1();
add.Top = count * (add.Height + gap);
show_pnl.Controls.Add(add);
count++;
}

Categories

Resources