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 ...
Related
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);
Now solved. Thanks for your answers!
This is my code right now:
//Listbox scripts is the name of my folder
private void Form1_Load(object sender, EventArgs e)
{
foreach (var file in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + #"Listbox scripts"))
{
string file2 = file.Split('\\').Last();
listBox1.Items.Add(file2);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("SetText", new object[]
{
File.ReadAllText(string.Format("./Listbox scripts/{0}", listBox1.SelectedItem.ToString()))
});
}
I'm new to coding in C# and I have a textbox that has the names of text files in a directory and when I click on the text file in the listbox it's supposed to load the text from it into my textbox (named 'ScriptBox')
Here's my code:
private void Form1_Load(object sender, EventArgs e)
{
string User = System.Environment.MachineName;
textBox1.Text = "{CONSOLE} Welcome to Linst, " + User + "!";
directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + #"Scripts");
files = directory.GetFiles("*.txt");
foreach (FileInfo file in files)
{
listBox1.Items.Add(file.Name);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedFile = files[listBox1.SelectedIndex];
ScriptBox.Text = File.ReadAllText(selectedFile.FullName); //these parts are the parts that dont work
}
Thanks in advance!
Add the below into your Form1.cs. What this is going to do is when a user clicks a listbox item, its going to call (raise an event) the "listBox1_MouseClick" method and set the text of the textbox to the text of the listbox item. I just quickly created an app and implemented the below and it works.
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.Text;
}
And add the below to the Form1.Designer.cs where the rest of your list box properties are. The below is subscribing to an event, the listBox1_MouseClick method in Form1.cs, so when a user clicks on a listbox item, the listBox1_MouseClick method is going to run.
this.listBox1.MouseClick += new MouseEventHandler(this.listBox1_MouseClick);
I hope the above makes sense.
Your code is nice, and perfect but it just need a little validation check in list index selection
Try thing in your listbox_selectedIndexChanged
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex!=-1)
{
FileInfo selectedFile = files[listBox1.SelectedIndex];
ScriptBox.Text = File.ReadAllText(selectedFile.FullName);
}
}
I actually don't see a problem with your code, could it be a typo somewhere?
I did this and it worked for me:
private void Form1_Load(object sender, EventArgs e)
{
foreach (var file in System.IO.Directory.GetFiles(#"c:\"))
{
listBox1.Items.Add(file);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
textBox1.Text = System.IO.File.ReadAllText(listBox1.SelectedItem.ToString());
}
}
Is it possible to add the same string typed in the texbox in the two lines of the listbox, as shown in the image below?
private void metroButton10_Click(object sender, EventArgs e)
{
listBox2.Items.Add("Next station: " + metroTextBox1.Text);
listBox2.Items.Add(Environment.NewLine)
metroTextBox1.Clear();
listBox2.SelectedIndex = 0;
}
Use the following code.
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add("Next Station: "+textBox1.Text);
listBox1.Items.Add("Station: " + textBox1.Text);
}
You Can't use new line. Because listbox can contain one item in a line.
Sure you can add same string typed in the textbox in the two lines of the listbox as;
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add("NextStation: " + textBox1.Text);
listBox1.Items.Add("Station: " +textBox1.Text);
}
I need to accept more than one value from the same textbox and store it into an arrya . I need something close to the below code :
string[] countries = new string[3];
private void accept_Click(object sender, EventArgs e)
{
countries[0] = textBox1.Text+" 1 ";
countries[1] = textBox1.Text + " 2 ";
countries[2] = textBox1.Text + " 3 ";
}
private void finish_Click(object sender, EventArgs e)
{
foreach (string coun in countries)
MessageBox.Show("You have entered " + coun);}
}
}
How do you split the different countries?
If you split it on each space you can do it like this:
string[] countries = textBox1.Text.Split(null);
If that is not a good solution maybe try and explain the expected output.
You can simply use a multiline textbox and split the input on newlines:
var countries = countriesTextBox.Text.Split(Environment.NewLine);
Why not just use a list? The following code will add the country's name entered in the textbox to the List<string> every time you click the accept button.
var countries = new List<string>();
private void accept_Click(object sender, EventArgs e)
{
countries.Add(textBox1.Text);
}
private void finish_Click(object sender, EventArgs e)
{
foreach (string coun in countries)
MessageBox.Show("You have entered " + coun);
}
If you want to allow the user to enter multiple countries at once, the following will work if you put a comma in between each country name when entering them into the textbox:
var countries = new List<string>();
private void accept_Click(object sender, EventArgs e)
{
var input = textBox1.Text.Split(',');
countries.AddRange(input);
}
private void finish_Click(object sender, EventArgs e)
{
foreach (string coun in countries)
MessageBox.Show("You have entered " + coun);
}
Okay so I'm trying to display multiple values in hashtable through my button. But it keeps on showing me the first value but not the others.
public Form1()
{
InitializeComponent();
}
Hashtable Info = new Hashtable();
private void button1_Click(object sender, EventArgs e)
{
string a = textBox1.Text;
string b = textBox2.Text;
Info.Add(a,b);
label4.Text = a + " " + b;
}
private void button3_Click(object sender, EventArgs e)
{
foreach (DictionaryEntry DE in Info)
{
label4.Text = ""+ DE.Key +DE.Value; //this only shows the first added value. How do I show the remaining values?
}
}
This is because each iteration of the loop replaces the previous value in the text. You can fix this by clearing out the text before going into the loop, and using += instead of =:
label4.Text += " "+ DE.Key +DE.Value
A better approach would be to use string.Join for this:
private void button3_Click(object sender, EventArgs e) {
label4.Text = string.Join(
", "
, Info.Select(p => string.Format("{0}-{1}", p.Key, p.Value))
);
}
I recommend you don't keep changing label4.Text in the loop, but build a string in the loop and change it once at the end. Something like:
StringBuilder allEntries = new StringBuilder();
foreach (DictionaryEntry DE in Info)
{
allEntries.Append(DE.Key);
allEntries.Append(DE.Value);
}
label4.Text = allEntries.ToString();
[I edited an earlier version of the code that didn't compile.]
KC