0

私は次のフォームを持っています。最初のボタンはテキストファイルを開き、フォームのリッチテキストボックスにファイルを表示します。2番目のボタンは別のウィンドウを開きます。私が欲しいのは、そのウィンドウにそのテキストファイルにあるデータが事前に入力されていることです...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HomeInventory2
{
    public partial class Form2 : Form
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Run(new Form1());
        }
    }
}

入力する必要のあるフォーム

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HomeInventory2.Domain;
using HomeInventory2.Business;

namespace HomeInventory2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            CreateInventory create = new CreateInventory();
            create.ItemAmount = textBoxAmount.Text;
            create.ItemCategory = textBoxCategories.Text;
            create.ItemProperties = textBoxValue.Text;
            create.ItemValue = textBoxValue.Text;

            InventoryMngr invtryMngr = new InventoryMngr();
            invtryMngr.Create(create);

        }
    }
}
4

1 に答える 1

2

オーバーロードする新しいコンストラクターを作成しますstring。新しいを開くときFormに、テキストの日付を渡してテキストボックスに入力します。

//in the new form that opens up
public Form1(string prepopulated)
{
    InitializeComponent();
    myRichTextbox.Text = prepopulated;
}

そして、次のようにクリックイベントから呼び出します。

//in the first form
private void button2_Click(object sender, EventArgs e)
{
    Application.Run(new Form1(richTextBox1.Text));
}

コンテンツが単純なテキストファイルよりも複雑な場合は、代わりにRichTextBox.Documentを使用して、文字列の代わりにそれを渡すことができます。オーバーロードをに変更します

Form1(FlowDocument prepopulated)

このように呼んでください

Application.Run(new Form1(richTextBox1.Document));
于 2012-10-17T17:15:51.637 に答える