Click here to Skip to main content
15,898,770 members
Home / Discussions / C#
   

C#

 
QuestionCreating & writing to a text file from two different forms Pin
Member 140427683-Nov-18 9:45
Member 140427683-Nov-18 9:45 
AnswerRe: Creating & writing to a text file from two different forms Pin
Dave Kreskowiak3-Nov-18 10:06
mveDave Kreskowiak3-Nov-18 10:06 
GeneralRe: Creating & writing to a text file from two different forms Pin
Member 140427684-Nov-18 4:03
Member 140427684-Nov-18 4:03 
GeneralRe: Creating & writing to a text file from two different forms Pin
Eddy Vluggen4-Nov-18 4:15
professionalEddy Vluggen4-Nov-18 4:15 
GeneralRe: Creating & writing to a text file from two different forms Pin
OriginalGriff4-Nov-18 6:34
mveOriginalGriff4-Nov-18 6:34 
AnswerRe: Creating & writing to a text file from two different forms Pin
Richard MacCutchan3-Nov-18 10:07
mveRichard MacCutchan3-Nov-18 10:07 
AnswerRe: Creating & writing to a text file from two different forms Pin
OriginalGriff3-Nov-18 20:28
mveOriginalGriff3-Nov-18 20:28 
AnswerRe: Creating & writing to a text file from two different forms Pin
BillWoodruff4-Nov-18 6:47
professionalBillWoodruff4-Nov-18 6:47 
As you can see from the responses here, there are many ways you could implement this; without understanding your UI goals in more detail, I think we're limited in terms of how helpful we can be.

Issues to consider in using multiple Forms:

1. shown modal ? allow user to cancel while incomplete ?
2. data in each field cleared with use, or persistent ?
3. validation ? allow empty fields ? required fields ?

My preference is to offload all serialization, or printing. functions, to a single static Class which might look something like this:
using System;
using System.IO;
using System.Text;

namespace YourNameSpace
{
    public static class WriteManager
    {
        public static string FileName = "Test";

        public static string FilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

        public static string ExtensionType = ".txt";

        private static string CurrentPath;

        static WriteManager()
        {
            SetWritePath(FileName, FilePath, ExtensionType);
        }

        public static void SetWritePath(string name, string path, string extension)
        {
            FileName = name;
            FilePath = path;
            ExtensionType = extension;
            CurrentPath = Path.Combine(path, name + extension);
        }

        static StringBuilder stringBldr = new StringBuilder();

        public static void WriteFile(StringBuilder sb1, StringBuilder sb2)
        {
            stringBldr.Clear();

            stringBldr.Append(sb1.Append(sb2));

            File.WriteAllText(CurrentPath, stringBldr.ToString());
        }

        public static void ReadFile()
        {
            // you write this
        }
    }
}
In this model, the 'WriteFile method requires two 'StringBuilder instances as inputs. I'd implement a 'GetData method in each of the Forms that has the responsibility to read the fields and turn them into a ready to save 'StringBuilder. The call in the main Form that triggers saving might look like:
// in the Main Form
StringBuilder sb1 = new StringBuilder();
public StringBuilder GetData()
{
    sb1.Clear();

    sb1.AppendLine(textBox1.Text);
    sb1.AppendLine(textBox2.Text);
    sb1.AppendLine(textBox3.Text);
    sb1.AppendLine(textBox4.Text);

    return sb1;
}

private void SaveFileButton_Click(object sender, EventArgs e)
{
    WriteManager.WriteFile(this.GetData(), form2.GetData());
}
Keep in mind this is a sketch, but, the architecture is one that has proved useful foe me.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

Questionbest mothod for save file database C# Pin
arman02-Nov-18 4:40
arman02-Nov-18 4:40 
AnswerRe: best mothod for save file database C# Pin
OriginalGriff2-Nov-18 5:16
mveOriginalGriff2-Nov-18 5:16 
AnswerRe: best mothod for save file database C# Pin
Gerry Schmitz2-Nov-18 6:06
mveGerry Schmitz2-Nov-18 6:06 
AnswerRe: best mothod for save file database C# Pin
Eddy Vluggen3-Nov-18 2:07
professionalEddy Vluggen3-Nov-18 2:07 
QuestionList of surrounding points Pin
Bernhard Hiller2-Nov-18 3:16
Bernhard Hiller2-Nov-18 3:16 
AnswerRe: List of surrounding points Pin
Dar Brett2-Nov-18 5:15
Dar Brett2-Nov-18 5:15 
GeneralRe: List of surrounding points Pin
Bernhard Hiller4-Nov-18 21:44
Bernhard Hiller4-Nov-18 21:44 
AnswerRe: List of surrounding points Pin
Gerry Schmitz2-Nov-18 5:50
mveGerry Schmitz2-Nov-18 5:50 
GeneralRe: List of surrounding points Pin
BillWoodruff9-Nov-18 13:28
professionalBillWoodruff9-Nov-18 13:28 
AnswerRe: List of surrounding points Pin
Daniel Pfeffer4-Nov-18 23:53
professionalDaniel Pfeffer4-Nov-18 23:53 
AnswerRe: List of surrounding points Pin
BillWoodruff9-Nov-18 13:26
professionalBillWoodruff9-Nov-18 13:26 
GeneralRe: List of surrounding points Pin
Bernhard Hiller11-Nov-18 21:33
Bernhard Hiller11-Nov-18 21:33 
GeneralRe: List of surrounding points Pin
BillWoodruff11-Nov-18 22:26
professionalBillWoodruff11-Nov-18 22:26 
GeneralRe: List of surrounding points Pin
Bernhard Hiller13-Nov-18 21:57
Bernhard Hiller13-Nov-18 21:57 
GeneralRe: List of surrounding points Pin
BillWoodruff13-Nov-18 22:05
professionalBillWoodruff13-Nov-18 22:05 
GeneralRe: List of surrounding points Pin
Bernhard Hiller13-Nov-18 22:22
Bernhard Hiller13-Nov-18 22:22 
Question[SOLVED] I need help with NIM GAME IN C# Pin
RES7B4-Nov-18 5:23
RES7B4-Nov-18 5:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.