1
C# help!
How do you make a script in C# Which picks a string from a list and displays it in a textbox after a button is clicked?
For example you click the button Test and some text appears in a textbox. The text in the textbox is the string.
Thanks.
Niko
For example you click the button Test and some text appears in a textbox. The text in the textbox is the string.
Thanks.
Niko
14
with lead and only programmer Snowyfox
hahaha, it's a lonely world sometimes!
luigi_vampaHaha, then op will have to be a little more specific about what he wants from his strings!
Good point. OP needs to specify if he's looking for a specific string in the file or if he just wants to read off specific lines (or strings separated by some sign).
https://dl.dropboxusercontent.com/conte ... ebizr?dl=1
Here's a Dropbox upload of the project. Usually I just go inside recursive folders and zip up all the actual files, but this time I left it completely intact as it was in the folder; that way, you can unpack it directly in your Project folder and you wouldn't have to do anything to open the project, if you want. There's a project file inside as well.
The main code is in Form1.cs. You can take a look at the rest of the code and other files, but they won't really help you. bin/debug has an executable if you want to see how it looks like.
If you don't want to download and just want Form1.cs's code, here you go.
Here's a Dropbox upload of the project. Usually I just go inside recursive folders and zip up all the actual files, but this time I left it completely intact as it was in the folder; that way, you can unpack it directly in your Project folder and you wouldn't have to do anything to open the project, if you want. There's a project file inside as well.
The main code is in Form1.cs. You can take a look at the rest of the code and other files, but they won't really help you. bin/debug has an executable if you want to see how it looks like.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Mizuarc_Filewriter
{
public partial class Form1 : Form
{
string directory = "null";
string filename = "null";
string[] contents = new string[] { "null" };
string trig = "null";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
directory = FWDirectory.Text;
filename = FWFilename.Text;
contents = FWContents.Lines;
bool i = Equals(filename, "");
if (i == true)
{
FWFilename.Text = "Default.mzc";
filename = "Default.mzc";
}
if (!directory.EndsWith("\\"))
{
if (directory != "")
{
Directory.CreateDirectory(directory);
FWDirectory.AppendText("\\");
directory = directory + "\\";
}
}
if (File.Exists(directory + filename))
{
DialogResult result = MessageBox.Show("There is a file in the same directory\nwith the same filename. Would you\nlike to overwrite it?", "Existing file discovered", MessageBoxButtons.YesNo);
switch (result)
{
case DialogResult.Yes:
{
File.WriteAllLines(directory + filename, contents, Encoding.UTF8);
MessageBox.Show("Wrote to " + directory + filename + ".", "Operation successful!");
break;
}
case DialogResult.No:
{
break;
}
}
}
if (!File.Exists(directory + filename))
{
File.WriteAllLines(directory + filename, contents, Encoding.UTF8);
MessageBox.Show("Wrote to " + directory + filename + ".", "Operation successful!");
}
}
private void FWContents_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click_1(object sender, EventArgs e)
{
MessageBox.Show("Created by the Frost Brigade Production Team,\nwith lead and only programmer Snowyfox.", "About");
}
}
}If you don't want to download and just want Form1.cs's code, here you go.
Snowyfoxluigi_vampaEasy! This little piece tests against the string array to find the value "two" on the click of button1 and then loads the value into textBox1.
string[] srtArray = new string[] { "one", "two", "three" };
private void button1_Click(object sender, EventArgs e)
{
foreach (string str in strArray)
{
if(str == "two")
{
textBox1.Text = str;
break;
}
}
}
Yeah, but...wouldn't you have to hardcode the text that you expect...? I think OP would rather want to modify the Strings of the buttons through a text file, so the thing would be flexible.
One sec, OP, starting Visual Studio and getting the code.
Haha, then op will have to be a little more specific about what he wants from his strings!
luigi_vampaEasy! This little piece tests against the string array to find the value "two" on the click of button1 and then loads the value into textBox1.
string[] srtArray = new string[] { "one", "two", "three" };
private void button1_Click(object sender, EventArgs e)
{
foreach (string str in strArray)
{
if(str == "two")
{
textBox1.Text = str;
break;
}
}
}
Yeah, but...wouldn't you have to hardcode the text that you expect...? I think OP would rather want to modify the Strings of the buttons through a text file, so the thing would be flexible.
One sec, OP, starting Visual Studio and getting the code.
Easy! This little piece tests against the string array to find the value "two" on the click of button1 and then loads the value into textBox1.
string[] srtArray = new string[] { "one", "two", "three" };
private void button1_Click(object sender, EventArgs e)
{
foreach (string str in strArray)
{
if(str == "two")
{
textBox1.Text = str;
break;
}
}
}
Wow, reading from a file. GL HF (even though it's actually easy in C# Visual Studio).
Just of note, you can double click a button to set up the method for the action when pressing it. Just make it use "setText(variable[#])" on the name of your Text Box (if you haven't changed it, it's probably "TextBox1" or something).
Mind me if it's not actually setText. I haven't coded in C# for a while. Typing from memory. I did something a bit similar to this before.
*EDIT* I have Mizuarc Filewriter (a very generic text file writer) (I made it); it doesn't have reading functions but it does have writing functions and a few other handy stuff. Want the source code? It might be of use as a reference or example.
Just of note, you can double click a button to set up the method for the action when pressing it. Just make it use "setText(variable[#])" on the name of your Text Box (if you haven't changed it, it's probably "TextBox1" or something).
Mind me if it's not actually setText. I haven't coded in C# for a while. Typing from memory. I did something a bit similar to this before.
*EDIT* I have Mizuarc Filewriter (a very generic text file writer) (I made it); it doesn't have reading functions but it does have writing functions and a few other handy stuff. Want the source code? It might be of use as a reference or example.
Yeah that would be good
Where is this "list"? Is it an array? Is it a text file? Is it a whole string in a variable, and you have a lot of variables to choose from?
You will need an Action Listener to detect the button. I use Visual Studio, and I don't need to code an Action Listener; I just double-click the button and it automatically creates a stub method for me to do my stuff in.
You will need an Action Listener to detect the button. I use Visual Studio, and I don't need to code an Action Listener; I just double-click the button and it automatically creates a stub method for me to do my stuff in.
Forgot to add, it can be an array
Thanks.
Thanks.
Im doing this in Visual Studio. And Its a text file.
Possibly a list box?
Also, OP, do you want pseudo-code, or do you want us to make a fully functional example for you?
Also, OP, do you want pseudo-code, or do you want us to make a fully functional example for you?
Pseudocode
Firstly, you'll need to read all the values in the text file, and then, if you wish, store them in an array (or a listbox). Now, whenever you click the button, you'll need to set your text box's text value to whatever is currently selected.
Here are a couple links to help you:
http://msdn.microsoft.com/en-us/library/94223t4d.aspx
http://msdn.microsoft.com/en-us/library/aa288424(v=vs.71).aspx
Here are a couple links to help you:
http://msdn.microsoft.com/en-us/library/94223t4d.aspx
http://msdn.microsoft.com/en-us/library/aa288424(v=vs.71).aspx
