0

The Form layout

Im trying to create a windows form that allows the user to enter values into a jagged array in the form of a characters Name, Strength and Dexterity. Once they enter those values into a text box it has to be saved so users can view the information for characters that have been entered by using the List Box

    public Form1()
    {
        InitializeComponent();
        String[][] Arr = new String[3][];
        Arr[0] = new String[20];
        Arr[1] = new String[20];
        Arr[2] = new String[20];

    }

So ive created Strings for the user to enter the values for the 3 attributes. Now im not sure where to actually add the code (and what the code is). Should i put a line such as Arr[0] = NameTxtbox.Text

  • Are you just asking how to assign a click handler to a button? Double-click on the button in the form editor. I imagine some brief tutorials on WinForms development could go a long way here. Also note that multiple arrays of values are a poor substitute for a single array of custom objects. – David May 03 '18 at 19:06
  • here is the doc: https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox(v=vs.110).aspx you can add event listeners on `TextChanged` event – Joel Harkes May 03 '18 at 19:08
  • I know how to assign a click handler to a button but what im not sure of is how to save the info from the 3 text boxes into the array, in the sense of Name must be saved into Arr[0] then Strength thats in the 2nd text box must be saved into Arr[1] – Jace Yatrakos May 03 '18 at 19:09
  • @JaceYatrakos: Well, `Arr[0]` is itself an *array*, so you can't save a single string to that. Where in your weird data structure do you want to save that value? Did you mean to save it to `Arr[0][0]`? This would probably be *a lot* easier for you if you just create a custom object for your data and maintain an array (or generic list) of that object. – David May 03 '18 at 19:12
  • @David Sorry im really unsure of what you mean, im still trying to get the hang of C# forms so do forgive me. But are you saying that what i should rather do is have the info from the text box stored into a single array using the Textbox.TextChanged line? – Jace Yatrakos May 03 '18 at 19:15

3 Answers3

1

Don't use multiple arrays of values as a substitute for a single array of objects.

Consider a simple object:

public class Character
{
    public string Name { get; set; }
    public string Dexterity { get; set; }
    public string Strength { get; set; }
}

Now, if you want your form to maintain a list of Character objects, then create a class-level property in your form:

private List<Character> Characters { get; set; } = new List<Character>();

Now whenever you save a character from the inputs (such as when clicking the "save" button on your form), you simply add it to the list:

Characters.Add(new Character
{
    Name = NameTxtbox.Text,
    Dexterity = DexterityTxtbox.Text,
    Strength = StrengthTxtbox.Text
});

So instead of trying to maintain a complex array of arrays of individual values, you have a simple collection of meaningful objects. You can bind your ListBox to that same collection of objects.

David
  • 208,112
  • 36
  • 198
  • 279
  • So i havent touched on object creation so im piecing it together so let me make sure i understand it correctly: Create the Object Character with the variables Name,Dexterity and Strength, then the values in the text box are stored into the Name, Dexterity and Strength then whenever i save the character using the Save button it then calls a new instance of the Character Object to allow for another entry into the list? – Jace Yatrakos May 03 '18 at 19:23
  • @JaceYatrakos: That's the general idea, ya. Basically any time you have multiple values which conceptually describe a *thing* then that *thing* is itself an object of some kind. This is basically a variation of the refactoring pattern Replace Array With Object: https://refactoring.com/catalog/replaceArrayWithObject.html You'll find that it's much easier to logically group values into meaningful objects. And the improved semantics of giving those objects meaningful names makes your code easier for you to read and maintain. – David May 03 '18 at 19:25
  • Okay so i only have one more question in relation to your solution. Do i create the public class Character inside the public Form or outside it. Then do i put the rest like the Character.Add inside the Character class – Jace Yatrakos May 03 '18 at 19:32
  • @JaceYatrakos: I'd create it outside, keep it as a separate concern. As your logic grows, this becomes a "model" in your "domain". You might have custom objects like `Character` or `Weapon` or `AttackType` or anything that is a thing in the system you're building. Ideally most of the logic of the domain would belong to models, and the UI (the forms) just invokes that logic. Having a folder called "Models" and putting your custom things there would be a good start. – David May 03 '18 at 19:34
  • So im getting errors when i try add the Character.Add should it be written like this, Character.Add(new Character) { Name = NameTxt.Text; Dexterity = DexTxt.Text; Strength = StrTxt.Text; } Also should this be added within the Character class – Jace Yatrakos May 03 '18 at 19:36
  • @JaceYatrakos: You have a typo in your code. Notice your extra `)` after `Character`. – David May 03 '18 at 19:38
  • Im still confused, if you could please show me the actual code. `code` public class Character { public string Name { get; set; } public string Dexterity { get; set; } public string Strength { get; set; } Characters.Add(new Character { Name = NameTxtbox.Text, Dexterity = DexterityTxtbox.Text, Strength = StrengthTxtbox.Text }); } – Jace Yatrakos May 03 '18 at 19:43
  • @JaceYatrakos: The actual code is in the answer I posted. What I'm showing there is the entire definition for the `Character` class, as well as a property for your form to store a collection of characters and a line of code for your form (likely in a button click event) to add an object to the collection of characters. That is literally the "actual code". – David May 03 '18 at 19:45
0

As @David well points out your best bet is to learn a bit about winforms.

Here is some of microsofts documentation: https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox(v=vs.110).aspx

The Textbox fires events to TextChanged which you can 'listen' to to execute your own code: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged(v=vs.110).aspx

textBox = new TextBox()
textBox.TextChanged += () => {
   // your code here.
   var input = textBox.Text;
   Arr[1] = input.ToCharArray();
}
Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
0

Inside of your Save Button handler add this

This Line :

string Info = textBox1.Text + " - " + textBox2.Text + " - " + textBox3.Text;

Concat all the strings the user entered into one string with - for spacing.

And This line

listBox1.Items.Add(Info);

adds the string to the listbox.

EDIT :

This is how you store them in an array.

string[] array = new[]{textBox1.Text , textBox2.Text , textBox3.Text};

Rainbow
  • 6,772
  • 3
  • 11
  • 28