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.
Related
I am new to the C# scene, so don't really have much knowledge - This is my first project.
I am looking to create a very basic calorie counter, which eventually will include other functions.
Here's what I have so far;
I want to know how to take the value from the text box on the click of the 'Add' button - Which adds to the total value (bottom right)
I'm looking for any tips/videos to help so anything is appreciated.
TIA
I made a simple implementation for you, you could refer to it:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
bool Flag = Int32.TryParse(textBox1.Text, out int result);//Determine if it is a number
if (textBox1.Text == "")//If it is null, falg takes true and skips the next judgment
{ Flag = true; }
else if (!Flag)
{
MessageBox.Show("Input Error");
textBox1.Text = null;
}
}
private void button1_Click(object sender, EventArgs e)
{
Int32.TryParse(textBox1.Text, out int result);
int total =result + Convert.ToInt32(label3.Text);
label3.Text = total.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
label3.Text = "0";
}
}
}
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.
Help, I'm a novice programming student who got assigned a side task at work; This is the pseudo code I wrote for it:
Write a program with 3 buttons:
Exit/Close, Clear/Reset, Count/Calculate
When the user clicks the count button, the number displayed in the label/textbox should increase by 1 and this should continue infinitely.
When the user clicks the clear/Reset button, the number displayed in the label/textbox should be reset to 0.`{
Application.Exit();
}
private void btnCount_Click(object sender, EventArgs e)
{
int Count = 1;
int Numberdisplayed;
{
do Count++;
while (Count >= 1);
Numberdisplayed = Count + 1;
lblNumberdisplayed.Text = Numberdisplayed.ToString();
}
}
private void btnReset_Click(object sender, EventArgs e)
{
int Count = 0;
lblNumberdisplayed.Text = String.Empty;
lblNumberdisplayed.Text = Count.ToString();
}
}
`
When the user clicks the Exit/Close btn, the application should close.
The only language I am familiar with is c# hence my writing it here and my company runs windows for all end users so I figured why not.
private void btnExit_Click(object sender, EventArgs e)
The code I have so far Image
If you aren't bound to use While loop you can try using session or the below:
private int count;
protected void btnCount_Click(object sender, EventArgs e)
{
count = Int32.Parse(lblCount.Text);
count++;
lblCount.Text = count.ToString();
}
Note: Though not tested, you can try the above.
I think the issue is the Count variable is local to the method, thus the do while never sees the reset.
I've created a program to make bug templates and am having a problem with text not saving correctly.
I have a text file called TemplateTexts that holds all the text for each template, a template looks like this -
REQUIREMENTS
- Example
ADDITIONAL REQUIREMENTS
- Example
- Example
-----COPY BELOW THIS LINE-----
When the program closes it copies all of that into 1 line of a text file. (looks like this)
REQUIREMENTS- Example ADDITIONAL REQUIREMENTS- Example - Example-----COPY BELOW THIS LINE-----
The text file contains 20 lines of templates. The template is saved as 1 line of text into the text file, but when I go to open the program again, it turns that 1 line of text into multiple lines of text like how it is displayed in the first example.
Any idea why this might be happening? or is there a better way to save each template into a text file, possibly by separating it with flags or something?
Here's the code to my program:
public partial class Form1 : Form
{
static String buttonNamesPath = AppDomain.CurrentDomain.BaseDirectory + "/ButtonNames.txt";
String[] ButtonNames = System.IO.File.ReadAllLines(buttonNamesPath);
static String buttonTextPath = AppDomain.CurrentDomain.BaseDirectory + "/ButtonText.txt";
String[] ButtonText = System.IO.File.ReadAllLines(buttonTextPath);
private void SetupTextField()
{
comboBox1.Items.Clear();
comboBox2.Items.Clear();
for (int i = 0; i < ButtonNames.Length; i++)
{
comboBox1.Items.Insert(i, ButtonNames[i]);
comboBox2.Items.Insert(i, ButtonNames[i]);
}
}
public Form1()
{
InitializeComponent();
this.FormClosing += this.Form1_FormClosing;
}
private void Form1_Load(object sender, EventArgs e)
{
SetupTextField();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string comboBoxText;
comboBoxText = comboBox1.SelectedItem.ToString();
int strNumber;
int strIndex = 0;
for (strNumber = 0; strNumber < ButtonNames.Length; strNumber++)
{
strIndex = Array.FindIndex(ButtonNames, x => x.Contains(comboBoxText));
if (strIndex >= 0)
break;
}
ButtonNames[strIndex] = textBox1.Text;
ButtonText[strIndex] = richTextBox2.Text;
SetupTextField();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
System.IO.File.WriteAllLines(buttonNamesPath, ButtonNames);
System.IO.File.WriteAllLines(buttonTextPath, ButtonText);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
string comboBoxText;
comboBoxText = comboBox2.SelectedItem.ToString();
int strNumber;
int strIndex = 0;
for (strNumber = 0; strNumber < ButtonNames.Length; strNumber++)
{
strIndex = Array.FindIndex(ButtonNames, x => x.Contains(comboBoxText));
if (strIndex >= 0)
break;
}
richTextBox1.Text = ButtonText[strIndex];
}
private void label3_Click(object sender, EventArgs e)
{
}
}
I also have 2 text files called ButtonNames.txt and ButtonText.txt
When you ask the RichTextBox for its Text property, it converts the rich text it contains internally to a plain-text string, and apparently by default it uses \n to translate line endings. Notepad doesn't recognize \n as a line ending (because it's looking for the official Windows line ending of \r\n), so it displays everything on one line. If you want to save with \r\n for line endings, use string.Replace on the result of RichTextBox.Text to replace \n with \r\n.
See this question and its answers for more details:
RichTextBox Newline Conversion?
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