I'm making a program that can save data from one combobox and two textboxes. When open the program, the saved data (in textfile) should open automaticly in Combobox with the text that was saved as a textfile. Also there should be one open-button that can open another textfile (if someone did a backup), and a delete button that can delete the selected "data" that is selected in the combobox.
With other words, if I save "Combobox: test 1", "Textbox1: test 2", "Textbox: test 3", and then save another data "Combobox: test a", "Textbox1: test b", "Textbox: test c".
When restarting the program, Combobox should have "test 1" and "test a" that can be choosed from.
The only feature I can get to work is saving the txt-file, but only open all text on the same row (witch I do not want). If this is impossible with the txt-file, It also works if someone can show me how to do this with a XML-file instead.
using System.IO;
namespace openclose
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
string nl = "\r\n";
TextWriter txt = new StreamWriter("test.txt");
txt.Write((comboBox1.Text) + nl + (textBox1.Text) + nl + (textBox2.Text));
txt.Close();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = File.ReadAllText("test.txt");
textBox2.Text = File.ReadAllText("test.txt");
comboBox1.Text = File.ReadAllText("test.txt");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
I did also try to change the "savebutton" with this instead, it does save. But I can't figure out how to make the saved data show upp in the combobox so I can get back the data to textbox1 and textbox2.
string path = #"Test.txt";
if (!File.Exists(path))
{
// Create a file to write to.
string createText = "";
File.WriteAllText(path, createText, Encoding.UTF8);
}
string appendText = comboBox1.Text + Environment.NewLine + textBox1.Text + Environment.NewLine + textBox2.Text + Environment.NewLine;
File.AppendAllText(path, appendText, Encoding.UTF8);
string readText = File.ReadAllText(path);
Console.WriteLine(readText);
Related
I need code what will check if word exist in textBox1 and delete line if contain that word.
Word is crosshair 1
I created cod if checkBox1.checked write some text in textBox1 now i need just that code to serach for word and delete line ... Thanks in advance !!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConfigMaker
{
public partial class createnew : Form
{
public createnew()
{
InitializeComponent();
saveFileDialog1.Filter = "Configuration files | *.cfg";
saveFileDialog1.DefaultExt = "cfg";
}
private void bunifuCheckbox1_OnChange(object sender, EventArgs e)
{
if (bunifuCheckbox1.Checked)
{
textBox1.Text += "crosshair 1" + "\r\n";
}
else
{
}
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string name = saveFileDialog1.FileName;
File.WriteAllText(name, textBox1.Text);
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
var value = trackBar1.Value.ToString();
label3.Text = value;
textBox1.Text += "brightness " + value + "\r\n";
}
private void name_TextChanged(object sender, EventArgs e)
{
textBox1.Text += "name " + '\u0022' + name.Text + '\u0022' + "\r\n";
}
}
}
Here is the code of createnew form.
Now i need to to next, when bunifucheckbox1 unchecked if crosshair 1 exists delete crosshair 1 from textbox1.text
If trackbar1.value = 0, if brightness "value" exists delete brightness "value" from textbox1.text
And if textbox2.Text empty, if name "value" exists delete name "value" from textbox1.text.
I tried to use this code
private void bunifuCheckbox1_OnChange(object sender, EventArgs e)
{
string crosshair = "crosshair 0";
string crosshair1 = "crosshair 1";
if (bunifuCheckbox1.Checked)
{
textBox1.Text += crosshair.Replace(crosshair, crosshair1) + "\r\n";
}
else
{
textBox1.Text += crosshair + "\r\n";
}
}
But it still creating new line with replaced text...
You don't really give us much to go on e.g. multiline, single line TextBox.
At what point do you want the text to be tested, on entry, after entry, on the press of a button?
Have you even triedString.Contains(…)?
I suggest that you have a look at the Microsoft documentation here and choose an example that you understand and if you are still having problems then come back to us.
edit
If you're after changing text in the TextBox in as few lines as possible then you can just use String.Replace.
The Text property of TextBox is a string and String.Replace will only give you a changed string if it can perform the replacement that you ask of it.
So your code should be along the lines of:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox1.Text = textBox1.Text.Replace("crosshair 0", "crosshair 1");
}
else
{
textBox1.Text = textBox1.Text.Replace("crosshair 1", "crosshair 0");
}
}
What I see is that you are always appending to the original text. If your text box contains the string "crosshair 1\r\n", then the line
textBox1.Text += crosshair + "\r\n";
is equivalent to
textBox1.Text = textBox1.Text + crosshair + "\r\n";
which means
obtain the text in the textbox, which is "crosshair 1\r\n", then append "crosshair\r\n" to it, and then set the resulting string ("crosshair 1\r\ncrosshair\r\n", i.e. two lines of text) as the new text of the text box.
It never replaces any part of the text in the text box, which seems to be what you want. If the text in the text box is always exactly just either "crosshair" or "crosshair 1" then you can also simply set the text to "crosshair 1", no need to fiddle with Replace().
So I think you must obtain the text from the text box first, change that and set it again (or simply set it directly):
string oldText = textBox1.Text;
string newText = oldText.Replace(crosshair1, crosshair);
// or simply newText = crosshair;
textBox1.Text = newtext;
This question already has answers here:
Open existing file, append a single line
(9 answers)
Closed 4 years ago.
Basicly I'm writing a fake malware, which will write in a .txt the Session username of the person which clicked on it. The problem is that when someone execute it will erase the previous lines.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text = "Fake Malware";
}
private void Form1_Load(object sender, EventArgs e)
{
string createText = " just clicked on the fake Malware";
File.WriteAllText("//vm-files/users/ngallouj/zig/zig.txt", Environment.UserName + createText + " " + DateTime.Now.ToString("h:mm:ss tt"));
string readText = File.ReadAllText("//vm-files/users/ngallouj/zig/zig.txt");
}
private void zig(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("Link to a warning message on click, nothing important.");
string createText = " just clicked on the fake Malware";
File.WriteAllText("//vm-files/users/ngallouj/zig/zig.txt", Environment.UserName+ createText + " " + DateTime.Now.ToString("h:mm:ss tt"));
string readText = File.ReadAllText("//vm-files/users/ngallouj/zig/zig.txt");
}
}
I think what you are looking for is AppendAllText:
File.AppendAllText("//vm-files/users/ngallouj/zig/zig.txt", "Wow it worked :)");
Here is the fix
private void Form1_Load(object sender, EventArgs e)
{
string createText = " just clicked on the fake Malware";
File.AppendAllText("//vm-files/users/ngallouj/zig/zig.txt", Environment.UserName + createText + " " + DateTime.Now.ToString("h:mm:ss tt"));
string readText = File.ReadAllText("//vm-files/users/ngallouj/zig/zig.txt");
}
private void zig(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("#");
string createText = " just clicked on the fake Malware";
File.AppendAllText("//vm-files/users/ngallouj/zig/zig.txt", Environment.UserName+ createText + " " + DateTime.Now.ToString("h:mm:ss tt"));
string readText = File.ReadAllText("//vm-files/users/ngallouj/zig/zig.txt");
}
}
}
I have a Textbox with MultiLine enabled, in my application this Textbox controller used to insert some text.
All I want to do is to jump to a new line if the user clicks enter.
All I have tried is to find the write command inside my controller Enter event:
private void tbc_Enter(object sender, EventArgs e)
{
}
Is this what you want?
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "This" + Environment.NewLine + "A" + Environment.NewLine + "Multiline" + Environment.NewLine + "Textbox.";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.AppendText(Environment.NewLine);
textBox1.Focus();
}
Been trying to solve this problem for hours now, and its frustrating me, hope anyone here can help me!
What im trying to do, is that i want the textbox to clear the text after i have searched for the word i have entered, and the richtextbox to delete whatever is in it when im searching for a new word.
private void button1_Click(object sender, EventArgs e)
{
string downloadedString;
System.Net.WebClient client;
string playerName = this.inputText.Text;
client = new System.Net.WebClient();
downloadedString = client.DownloadString("http:randomaddress;
string[] stringSeperator = new string[] { "\n" };
string[] result;
result = downloadedString.Split(stringSeperator, StringSplitOptions.None);
foreach (string s in result)
{
Bunch of if statements
}
public void SortString(string s)
{
//string manipulation
richTextBox1.Text += manipulate2 + "--" + " ";
}
public void SortString2(string s)
{
//string manipulation
richTextBox1.Text += manipulate2 + "--" + " ";
}
public void SortString3(string s)
{
//string manipulation
richTextBox1.Text += manipulate2 + "--" + " ";
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void True(object sender, PreviewKeyDownEventArgs e)
{
}
Been trying many different approaches but i just cant make it to work..
But to say again what it is what i want..
textbox is used for search, the input i get from the search gets showed in the richtextbox.
The search word should be cleared after i have entered the word and searched for it, and the richtextbox needs to clear the former search everytime i search again.
I suppose this is very simple, but havent worked with GUI for more than 2-3 days, used to console :)
Thank you in advance!
Oh and btw, everything else works like it should if that wasnt clear!
Ok...just Clear() them both after you have retrieved the search value?
string playerName = this.inputText.Text;
this.inputText.Clear();
richTextBox1.Clear();
// ... rest of your code ...
I have 3 checkboxes with corresponding message in a textbox. My teacher wants the message to remain in the textbox when the checkbox is still checked and hide the text when it is unchecked. In my case when I checked the 3 checkboxes their 3 corresponding messages will appear but when I unchecked one of the checkboxes and the other two are still checked, all the message will disappear. My problem is when I unchecked one of the checkbox and and the other 2 are still checked the corresponding messages with the remaining two checked checkboxes will remain in their textboxes.
private void chkCarWheels_CheckedChanged(object sender, EventArgs e)
{
if (chkCarWheels.Checked == true)
lblMessage.Text = lblMessage.Text + mycar.hasWheels(4);
else
lblMessage.Text = "My " + txtName.Text + " Car";
}
private void chkCarAcceleration_CheckedChanged(object sender, EventArgs e)
{
if (chkCarAcceleration.Checked == true)
lblMessage.Text = lblMessage.Text + mycar.Accelerate();
else
lblMessage.Text = "My " + txtName.Text + " Car";
}
private void chkCarBreakpad_CheckedChanged(object sender, EventArgs e)
{
if (chkCarBreakpad.Checked == true)
lblMessage.Text = lblMessage.Text + mycar.hasBreak();
else
lblMessage.Text = "My " + txtName.Text + " Car";
}
Looks like you need to create message depending on checkboxes states. You can create method, which will do the job and call it when state of some checkbox changed.
private void chkCarWheels_CheckedChanged(object sender, EventArgs e)
{
BuildMessage();
}
private void chkCarAcceleration_CheckedChanged(object sender, EventArgs e)
{
BuildMessage();
}
private void chkCarBreakpad_CheckedChanged(object sender, EventArgs e)
{
BuildMessage();
}
Or the better one - create one event handler for all checkboxes:
// use for chkCarWheels, chkCarAcceleration, chkCarBreakpad
private void chkCar_CheckedChanged(object sender, EventArgs e)
{
BuildMessage();
}
private void BuildMessage()
{
lblMessage.Text = "My " + txtName.Text + " Car";
if (chkCarWheels.Checked)
lblMessage.Text = lblMessage.Text + mycar.hasWheels(4);
if (chkCarAcceleration.Checked)
lblMessage.Text = lblMessage.Text + mycar.Accelerate();
if (chkCarBreakpad.Checked)
lblMessage.Text = lblMessage.Text + mycar.hasBreak();
}
You don't need to compare boolean values with true/false. Use those values directly if (chkCarWheels.Checked). And keep in mind that in C# we use CamelCase names form methods. Also consider to use StringBuilder to build whole message and then assign it to label:
private void BuildMessage()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("My {0} Car", txtName.Text);
if (chkCarWheels.Checked)
sb.Append(mycar.hasWheels(4));
if (chkCarAcceleration.Checked)
sb.Append(mycar.Accelerate());
if (chkCarBreakpad.Checked)
sb.Append((mycar.hasBreak());
lblMessage.Text = sb.ToString();
}
Try this:
private void chkCarWheels_CheckedChanged(object sender, EventArgs e)
{
chkCar();
}
private void chkCarAcceleration_CheckedChanged(object sender, EventArgs e)
{
chkCar();
}
private void chkCarBreakpad_CheckedChanged(object sender, EventArgs e)
{
chkCar()
}
private void chkCar()
{
string msg="";
if (chkCarWheels.Checked)
msg=msg+mycar.hasWheels(4);
if(chkCarAcceleration.Checked)
msg=msg+mycar.Accelerate();
if(chkCarBreakpad.Checked)
msg=msg+mycar.hasBreak();
lblMessage.Text=msg;
}