Frist of all, I'm a beginner in C# and I want to become a game dev one day. So here I am! Asking about this simple question for you, but not for me.
The question is, I wanted my code to change text when I press the button. Imagine you're playing text-based adventure game which I'm trying to make here, and when you press the button it'll change the text each time when you press it.
But my code doesn't, here is my code enter image description here
The code is not wrong, but it doesn't work as I expected. It'll show only the last one but not from the first to last.
If you can help me develop my knowledge about this I'd be thankful so much.
You can cycle through an array or a List.
Here is a very simple exemple:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace TextLineByLine
{
public partial class Form1 : Form
{
List<string> Messages = new List<string>();
int currentLine = -1;
public Form1()
{
InitializeComponent();
// Add some messages
Messages.Add("You open your eyes");
Messages.Add("The bright blue sky is up there");
Messages.Add("And some blood covers the ground");
Messages.Add("You're not sure what happened...");
}
private void button1_Click(object sender, EventArgs e)
{
currentLine++; // increment the line index
if (currentLine < Messages.Count)
{
Title.Text = Messages[currentLine];
}
else
{
Title.Text = "-= Nothing to display =-";
}
}
}
}
Well you can create a string array for your sentences like this and do the following.
int counter = 0;
private void btnAction_Click(object sender, RoutedEventArgs e)
{
string[] messages = new string[4]
messages[0] = "Your first message";
messages[1] = "Your second message";
messages[2] = "Your third message";
messages[3] = "Your fourth message";
title.Text = messages[0];
counter++;
}
if I understand your question well you want a text change each time it is pressed.
You can do it easily by using an Integer for example.
public partial class MainWindow : Window
{
int dialogue = 0;
public MainWindow() { InitializeComponent(); }
private void btnAction_Clickl(object sender, RoutedEventArgs e)
{
switch ( dialogue )
{
case 0:
Title.Text = "Some text";
dialogue++;
break;
case 1:
Title.Text = "Another text";
dialogue++;
break;
default: //just in case you want to reset the text
Title.Text = "First text";
dialogue = 0;
break;
}
}
}
If you want to set it automatic, like just one click and then the text changes every x seconds you can use:
public partial class MainWindow : Window
{
public MainWindow() { InitializeComponent(); }
private void btnAction_Clickl(object sender, RoutedEventArgs e)
{
Dialogue();
}
private async void Dialogue()
{
Title.Text = "Some text";
await Task.Delay(2000); // Where 2000 means 2 seconds in milliseconds
Title.Text = "More text"; // And so on...
}
}
Have a nice day.
Related
So essentially, I have a button, when I press the button, the button should delete the most recent row of text embedded into the rich text box. Press it again, it should delete the next line above that etc.
I tried a using .substring, which it worked, but required me to double click the button instead of a single click..
Furthermore, when I press the add text button it should replace the deleted line. Currently, the delete button button1_click just removes all lines, but all I want the it to do is remove a line of text from the richtextbox.
Edit:
Huge apologies before, I forgot to add the code as mentioned in replies, apologies.
public partial class Form1 : Form
{
string[] texts = new string[10];
int i = 0;
public Form1()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if (TextBox.Text == "")
{
texts[i] = texts[i];
}
else
{
texts[i] += TextBox.Text + "\r\n";
richText.Text += texts[i];
++i;
TextBox.Text = "";
}
}
private void richText_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//i dont know need to do research or im too dumb
richText.Text = "";
for (int i = 0; i < texts.Length; i++)
{
texts[i] = "";
}
i = 0;
//https://stackoverflow.com/questions/22587505/delete-the-last-line-of-rich-text-box
//works, but double click instead of one click
}
}
}
To clarify, the left button adds text from the textbox and the right button is basically the function aforementioned.
enter image description here
Thanks to anyone who helps!
I just finished an exercise from Head First C# where I built a Typing Game. The book leaves it to the reader to figure out how to make it so the player can start a new game once they've lost. After the user loses the game, the window shows the message "Game Over". I would like to have a new window pop up and ask the user if they would like to play again once they've closed out of the game over screen. I'd like there to be two buttons; one that says "no" and one that says "yes". What I'm stuck on is how I should (or would) go about restarting the app if the user decides they want to play again. I'll copy and paste my code below:
namespace _7HeadFirstProject
{
public partial class Form1 : Form
{
Random random = new Random();
Stats stats = new Stats();
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
// Add a random key to the ListBox
listBox1.Items.Add((Keys)random.Next(65, 90));
if (listBox1.Items.Count > 7)
{
listBox1.Items.Clear();
listBox1.Items.Add("Game Over");
timer1.Stop();
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// If the user pressed a key that's in the ListBox...
// ... remove it and then make the game a little faster
if (listBox1.Items.Contains(e.KeyCode))
{
listBox1.Items.Remove(e.KeyCode);
listBox1.Refresh();
if (timer1.Interval > 400)
timer1.Interval -= 10;
if (timer1.Interval > 250)
timer1.Interval -= 7;
if (timer1.Interval > 100)
timer1.Interval -= 2;
difficultyProgressBar.Value = 800 - timer1.Interval;
// The user pressed a correct key, so update the Stats object...
// ...by calling its Update() method with the argument true
stats.Update(true);
}
else
{
// The user pressed an incorrect key, so update the Stats object...
// ...by calling its Update() method with the argument false
stats.Update(false);
}
// Update the labels on the StatusStrip
correctLabel.Text = "Correct: " + stats.Correct;
missedLabel.Text = "Missed: " + stats.Missed;
totalLabel.Text = "Total: " + stats.Total;
accuracyLabel.Text = "Accuracy: " + stats.Accuracy + "%";
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show("Would you like to play again?");
if
}
}
}
DIFFERENT CLASS:
namespace _7HeadFirstProject
{
class Stats
{
public int Total = 0;
public int Missed = 0;
public int Correct = 0;
public int Accuracy = 0;
public void Update(bool correctKey)
{
Total++;
if (!correctKey)
{
Missed++;
}
else
{
Correct++;
}
Accuracy = 100 * Correct / Total;
}
}
}
You have the whole game working so leave that form alone. Add another form to your project and then set the new form as the startup form. You can set it as the startup form by opening Program.cs and modifying this line:
// Instead of Form1 put the name of your new form
Application.Run(new Form1());
Double click the new form and put this code in it:
// Note: Your load method may have a different name.
private void Form2_Load(object sender, EventArgs e)
{
this.StartNewGame();
}
private void GameForm_FormClosed(object sender, FormClosedEventArgs e)
{
if (MessageBox.Show("Continue?", "Continue?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.StartNewGame();
}
}
private void StartNewGame()
{
// Your game form may have a different name so change this to that name
var gameForm = new Form2();
gameForm.FormClosed += GameForm_FormClosed;
gameForm.Show();
}
Every time the user presses the yes button on the dialog, you are creating a brand new instance of the form (of the game). In this new form, you can also have an array which keeps track of the total number of games and what the score of each game was so you can show it in case the user selected No. All you need is something like this:
var games = new List<Stats>();
// keep adding to it every time you call StartNewGame() method.
Try this:
if ((MessageBox.Show("Would you like to play again?", "Message", MessageBoxButtons.YesNo)) ==
DialogResult.Yes)
{
Application.Restart();
}
I have a ComboBox with two resolution (1600x900 and 1280x720), I want them to replace "-screen-width XXXX -screen-height YYY" by the resolution chosen , in my TXT file when I press the "Save and Close" boutton, for the moment I've tried anything because I'm a real beginner at coding, it's my first Program I ever made.
Basically, my program will be an easy way to edit launch options for guys who don't know them
This is what I have in "InitializeComponent();"
public Window1()
{
InitializeComponent();
listResolution.Add("1600x900");
listResolution.Add("1280x720");
widthChoose = 1280;
heightChoose = 720;
windowed = true;
foreach (String item in listResolution)
{
ResolutionBox.Items.Add(item);
}
}
This is what I have for my "Save and Close" boutton (The text replace doesn't work)
private void SaveClose_Click(object sender, RoutedEventArgs e)
{
if (Windowed.IsChecked == true)
windowed = true;
else
windowed = false;
string text = File.ReadAllText(#"Resources\arguments.txt");
text = text.Replace("-screen-fullscreen 1", "-screen-fullscreen 0");
File.WriteAllText("arguments.txt", text);
this.Close();
And I have no event for my comboBox
private void ResolutionBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
Content of my "arguments.txt"
-screen-fullscreen 0 -screen-width 1600 -screen-height 900
Right before
File.WriteAllText("arguments.txt", text);
Add these :
First we take the selected resolution (not sure the purpose of listResolution there)
var selectedResolution = ResolutionBox.SelectedItem.ToString();
split it to width and height
var split = selectedResolution.Split('x');
widthChoose = split[0];
heightChoose = split[1];
then replace 1600 & 900 with the new values :
text = text.Replace("1600",widthChoose)
continue with height.
I'm newbie for C# and i have a little project. I'm stucked somewhere. I explained it here (with sample source codes) :
I have a form application. I'm asking users select an option from 2 buttons. There are 2 buttons (YES and NO) . My codes like this :
public partial class Form1 : Form
{
public int choice=0;
public Form1()
{
if(choice == 0)
{
label.Text = "Please push one of these buttons :";
// And there are buttons below this label
}
else if(choice == 1)
{
label.Text = "You just pushed YES button";
}
else if(choice == 2)
{
label.Text = "You just pushed NO button";
}
}
private void buttonYes_Click(object sender, EventArgs e)
{
choice = 1;
/*
I have to use one of these here for redraw whole form
this.Refresh();
this.Invalidate();
*/
}
private void buttonNo_Click(object sender, EventArgs e)
{
choice = 2;
/*
I have to use one of these here for redraw whole form
this.Refresh();
this.Invalidate();
*/
}
}
As you see, when user click one of YES or NO button, whole constructor function should be re-executed. And label should be "You just pushed YES / NO button".
But when i use this.Refresh() , nothing happening when i clik buttons. Still label is "Please push one of these buttons :" .
When i use this.Invalidate() , all buttons disappering, and label is still "Please push one of these buttons :" .
What should i do ?
Thanks.
PS
I've found this question BEFORE ask this one. But as you see, accepted answer not working for me.
Invalidating or refreshing doesn't call the constructor again. The constructor is called exactly once when the form is created, and invalidating doesn't create a new form. Put the logic for changing stuff in another method and call that from the constructor AND from the event handlers - but do note for posterity that calling instance methods or accessing variables from the constructor isn't really the nicest way of doing this stuff - but for your purposes here it's the simple solution.
public partial class Form1 : Form
{
public int choice=0;
public Form1()
{
UpdateForm();
}
private void UpdateForm(){
if(choice == 0)
{
label.Text = "Please push one of these buttons :";
// And there are buttons below this label
}
else if(choice == 1)
{
label.Text = "You just pushed YES button";
}
else if(choice == 2)
{
label.Text = "You just pushed NO button";
}
}
private void buttonYes_Click(object sender, EventArgs e)
{
choice = 1;
/*
I have to use one of these here for redraw whole form
this.Refresh();
this.Invalidate();
*/
UpdateForm();
}
private void buttonNo_Click(object sender, EventArgs e)
{
choice = 2;
/*
I have to use one of these here for redraw whole form
this.Refresh();
this.Invalidate();
*/
UpdateForm();
}
}
This is how you really should be doing it:
public partial class Form1 : Form
{
public Form1()
{
// Isn't there supposed to be InitializeComponent() here?
// You should assign this in the designer, rather than here.
label.Text = "Please push one of these buttons :";
}
private void buttonYes_Click(object sender, EventArgs e)
{
label.Text = "You just pushed YES button";
}
private void buttonNo_Click(object sender, EventArgs e)
{
label.Text = "You just pushed NO button";
}
}
Since all the button is doing is changing a label, that should be done directly, not though changing a variable and refreshing.
But why didn't it work my way?
All Refresh and Invalidate do is redraw what is already on your form. They don't recreate it.
A constructor is designed to initialize an object once, when you create it. It cannot be called to 'reinitialize' or 'refresh' an object.
To avoid going into too much detail, I recommend you find an article/book on object oriented programming to learn more about constructors and other OOP idioms.
This is the best option if you want to keep the same variables as in the exemple. If you want a shorter version just change the label directly in the Form1 Constructor.
public partial class Form1 : Form
{
public int choice=0;
public Form1()
{
buttonYes.Click += (s,e) => {
choice = 1;
ChangeText(choice);};
buttonNo.Click += (s,e) => {
choice = 2;
ChangeText(choice);};
}
private void ChangeText(int userChoice)
{
if(choice == 0)
label.Text = "Please push one of these buttons :";
else if(choice == 1)
label.Text = "You just pushed YES button";
else if(choice == 2)
label.Text = "You just pushed NO button";
}
}
Shorter Version
public partial class Form1 : Form
{
public Form1()
{
label.Text = "Push a button";
buttonYes.Click += (s,e) => {label.Text = "Yes is pressed";};
buttonNo.Click += (s,e) => {label.Text = "No is pressed";};
}
}
Main Constructor execute when new object created .
use an Method for do this . and i am shore this will work
public string TextSwitcher(int choice)
{
Switch(choice) // choice is an int
{
// 1,2,3 is not an serial no they will pass by parameter
case(1):
return "Please push one of these buttons :";
brake;
case(2):
return = "You just pushed YES button";
brake;
case(3)
return = "You just pushed NO button";
brake;
}
}
private void buttonYes_Click(object sender, EventArgs e)
{
label.Text = TextSwitcher(2);
}
private void buttonNo_Click(object sender, EventArgs e)
{
label.Text = TextSwitcher(3);
}
i hop this will help you . and welcome for you'r thank's .
best of luck
I was wondering if there was a way of adding a sort of animation to text displayed on a form.
What I had in mind when I thought of this was kind of similar to what you can do with text in PowerPoint (i.e. a typewriter-like animation where the text is typed one at a time, have the whole textbox appear with a certain effect etc), I'm just looking to find out what you can do using Windows Forms.
Currently I'm using a textbox to display information on my form application, though in hindsight I realise labels would have worked just as well.
EDIT: Turns out I was using labels after all, I just gave it a name with 'textbox' inside for lack of a better description.
public partial class Form1 : Form
{
int _charIndex = 0;
string _text = "Hello World!!";
public Form1()
{
InitializeComponent();
}
private void button_TypewriteText_Click(object sender, EventArgs e)
{
_charIndex = 0;
label1.Text = string.Empty;
Thread t = new Thread(new ThreadStart(this.TypewriteText));
t.Start();
}
private void TypewriteText()
{
while (_charIndex < _text.Length)
{
Thread.Sleep(500);
label1.Invoke(new Action(() =>
{
label1.Text += _text[_charIndex];
}));
_charIndex++;
}
}
}
Now, I personally wouldn't do this because gratuitous animations tend to annoy users. I'd only use animation sparingly - when it really makes sense.
That said, you can certainly do something like:
string stuff = "This is some text that looks like it is being typed.";
int pos = 0;
Timer t;
public Form1()
{
InitializeComponent();
t = new Timer();
t.Interval = 500;
t.Tick += new EventHandler(t_Tick);
}
void t_Tick(object sender, EventArgs e)
{
if (pos < stuff.Length)
{
textBox1.AppendText(stuff.Substring(pos, 1));
++pos;
}
else
{
t.Stop();
}
}
private void button1_Click(object sender, EventArgs e)
{
pos = 0;
textBox1.Clear();
t.Start();
}
or something like that. It'll tick off ever half second and add another character to the multi-line text box. Just an example of what someone could do.