1

C# help!

MCDiamondHost's Avatar MCDiamondHost10/31/14 8:53 pm
1 emeralds 1.2k 14
11/5/2014 3:53 pm
luigi_vampa's Avatar luigi_vampa
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
Posted by MCDiamondHost's Avatar
MCDiamondHost
Level 13 : Journeyman Modder
14

Create an account or sign in to comment.

14

1
11/05/2014 3:53 pm
Level 52 : Grandmaster Dragonborn
luigi_vampa
luigi_vampa's Avatar
with lead and only programmer Snowyfox


hahaha, it's a lonely world sometimes!
1
11/05/2014 3:34 pm
Level 26 : Expert Princess
Snowyfox
Snowyfox's Avatar
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).
1
11/05/2014 3:33 pm
Level 26 : Expert Princess
Snowyfox
Snowyfox's Avatar
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.

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.
1
11/05/2014 3:32 pm
Level 52 : Grandmaster Dragonborn
luigi_vampa
luigi_vampa's Avatar
Snowyfox
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.


Haha, then op will have to be a little more specific about what he wants from his strings!
1
11/05/2014 3:28 pm
Level 26 : Expert Princess
Snowyfox
Snowyfox's Avatar
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.
1
11/05/2014 2:38 am
Level 52 : Grandmaster Dragonborn
luigi_vampa
luigi_vampa's Avatar
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;
}
}
}

1
10/31/2014 10:07 pm
Level 26 : Expert Princess
Snowyfox
Snowyfox's Avatar
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.
1
11/04/2014 10:59 pm
Level 13 : Journeyman Modder
MCDiamondHost
MCDiamondHost's Avatar
Yeah that would be good
1
10/31/2014 8:56 pm
Level 26 : Expert Princess
Snowyfox
Snowyfox's Avatar
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.
1
10/31/2014 9:14 pm
Level 13 : Journeyman Modder
MCDiamondHost
MCDiamondHost's Avatar
Forgot to add, it can be an array
Thanks.
1
10/31/2014 9:12 pm
Level 13 : Journeyman Modder
MCDiamondHost
MCDiamondHost's Avatar
Im doing this in Visual Studio. And Its a text file.
1
10/31/2014 9:11 pm
Level 42 : Master Droid
GigabyteGiant
GigabyteGiant's Avatar
Possibly a list box?

Also, OP, do you want pseudo-code, or do you want us to make a fully functional example for you?
1
10/31/2014 9:13 pm
Level 13 : Journeyman Modder
MCDiamondHost
MCDiamondHost's Avatar
Pseudocode
1
10/31/2014 9:17 pm
Level 42 : Master Droid
GigabyteGiant
GigabyteGiant's Avatar
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
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome