Make my combobox replace words in .txt file - c#

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.

Related

Need help deleting rows/lines in richtextbox c# win forms

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!

How to change text in textbox when I press the button?

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.

How to change color of text in windows form when you click a button?

When I click a button with a word e.g "cat" I want this word in richTextBox1 change to color red. Of course I made it wrong, but I would like to learn how to change it.
private void btn1(object sender, EventArgs e)
{
Button button = sender as Button;
string wordToColor = button.Text;
ChangeColorOfText(richTextBox1, wordToColor);
}
private void ChangeColorOfText(RichTextBox richTextBox1, string word)
{
ColorDialog colorDialog1 = new ColorDialog();
colorDialog1.Red = richTextBox1.SelectionColor;
int index;
do
{
index = richTextBox1.Find(word);
if (index >= 0)
{
richTextBox1.Select(index, word.Length);
richTextBox1.SelectionColor = ColorDialog.Red;
}
}
while (index >= 0);
}
I had few things that may be a solution for you:
It was a problem for me but it's not a must:
private void button1_Click(object sender, EventArgs e)
{
//Your code for finding and selecting your text
if (!String.IsNullOrEmpty(myRichTextBox.Text))
{
//Your code for selecting your text
myRichTextBox.SelectionStart = 0;
myRichTextBox.SelectionLength = myRichTextBox.Text.Length;
}
myRichTextBox.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
myRichTextBox.SelectionColor = Color.Red;
}
I think your problem was not setting the font before you set the color
(When I added the line myRichTextBox.SelectionFont = new Font("Verdana", 12, FontStyle.Bold); everything worked)
The Second thing I did in the code was to use
myRichTextBox.SelectionColor = Color.Red;
insted of
richTextBox1.SelectionColor = ColorDialog.Red;
it's easier and more efficient.
At the line
private void ChangeColorOfText(RichTextBox richTextBox1, string word)
you dont have to add the RichTextBox richTextBox1 element. (worked fine for me without it).
I deleted ColorDialog colorDialog1 = new ColorDialog();
colorDialog1.Red = richTextBox1.SelectionColor;
and changed richTextBox1.SelectionColor = ColorDialog.Red; torichTextBox1.SelectionColor = Color.Red;
I thought it will work but just when I write the same word in the text box that is in the button and click on the button the form freezes.

Import and display image one by one

I have a C# window form for importing and displaying multiple images.
I am able to import multiple images and display the first image, but got some problems on displaying images one by one.
The program flow: user click the button, then select multiple images. After that, the first image should be displayed on the picture box. When the user click the "next image button", the next image should be shown.
The first image is able to display on the picturebox but no idea on displaying them one by one.
Is there any configuration for achieving this or how to implement it through coding.
Thanks everyone.
My coding:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
InitializeOpenFileDialog();
}
private void InitializeOpenFileDialog()
{
// Set the file dialog to filter for graphics files.
this.openFileDialog1.Filter =
"Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" +
"All files (*.*)|*.*";
// Allow the user to select multiple images.
this.openFileDialog1.Multiselect = true;
this.openFileDialog1.Title = "My Image Browser";
}
private void SelectFileButton_Click(object sender, EventArgs e)
{
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
// Read the files
foreach (String file in openFileDialog1.FileNames)
{
// Create a PictureBox.
PictureBox pb = new PictureBox();
Image loadedImage = Image.FromFile(file);
pb.Height = loadedImage.Height;
pb.Width = loadedImage.Width;
pb.Image = loadedImage;
flowLayoutPanel1.Controls.Add(pb);
}
}
}
}
IMHO, there is a better way for achieving that.
You don't have to add PictureBox control for each image, it will overload your form.
My suggestion is to keep a list of all loaded images, and an indexer of the current shown image.
Code:
Add a PictureBox to your form (let's call it pictureBox1), where you want the images to shown.
In addition, add these properties to your class:
private List<Image> loadedImages = new List<Image>();
private int currentImageIndex;
In your "load images" button click event:
private void SelectFileButton_Click(object sender, EventArgs e)
{
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
loadedImages.Clear();
// Read the files
foreach (String file in openFileDialog1.FileNames)
{
// Create a PictureBox.
loadedImages.Add(Image.FromFile(file));
}
if (loadedImages.Count > 0)
{
currentImageIndex = 0;
pictureBox1.Image= loadedImages[currentImageIndex];
}
}
}
And finally, for Next/Previous buttons click event you can add this code:
// Mod function to support negative values (for the back button).
int mod(int a, int b)
{
return (a % b + b) % b;
}
// Show the next picture in the PictureBox.
private void button_next_Click(object sender, EventArgs e)
{
currentImageIndex = mod(currentImageIndex + 1, loadedImages.Count);
pictureBox1.Image = loadedImages[currentImageIndex];
}
// Show the previous picture in the PictureBox.
private void button_prev_Click(object sender, EventArgs e)
{
currentImageIndex = mod(currentImageIndex - 1, loadedImages.Count);
pictureBox1.Image = loadedImages[currentImageIndex];
}

Can't count words from richtextbox to label?

I'm not sure what's wrong here, but i'm trying to count words in a richtext box, and display that with a label.
I put a richtextbox in a tab control so I can have a tabbed text box. Which seems to make this a lot harder then it should
also this isn't the whole program, I took the parts relating to the richtextbox and word counter
Any help is appreciated :)
public RichTab()
{
InitializeComponent();
TabPage tp = new TabPage("Document");
RichTextBox rtb = new RichTextBox();
rtb.Dock = DockStyle.Fill;
tp.Controls.Add(rtb);
tabControl1.TabPages.Add(tp);
WordCount();
}
public RichTextBox RTTB()
{
RichTextBox rtb = null;
TabPage tp = tabControl1.SelectedTab;
if (tp != null)
{
rtb = tp.Controls[0] as RichTextBox;
}
return rtb;
}
private void WordCount()
{
MatchCollection wordColl = Regex.Matches(RTTB().Text, #"[\W]+");
label2.Text = wordColl.Count.ToString();
}
I would probably just wire up the TextChanged event of the RichTextBox, and count the words there:
rtb.TextChanged += rtb_TextChanged;
Then count the words (using Giorgio Minardi's regex):
private void rtb_TextChanged(object sender, EventArgs e) {
label2.Text = Regex.Matches(((RichTextBox)sender).Text, #"[\S]+").Count.ToString();
}
What's the actual issue ?
Here's a simple routine to count words:
[Test]
public void CountWords()
{
const string sample = "How you doing today ?";
MatchCollection collection = Regex.Matches(sample, #"[\S]+");
var numberOfWords = collection.Count;
//numberOfWords is 5
Assert.IsTrue(numberOfWords == 5);
}

Categories

Resources