0

我有一个 10 行 4 列的 GUI。它的目的是让我在每个框中输入输入,然后在底部单击提交按钮,它会给我一个包含所有输入的文件。我遇到的问题是如何编码我的“按钮”来做到这一点?帮助?

我的菜单条也有问题。似乎一切都是正确的,但我的视觉不会接受。我的代码有问题吗?

-- 这是我第一次使用 C# 和视觉,所以任何帮助将不胜感激!

Public partial class Form 1 : Form
{


int count;
    Form mdiChild;
    TextBox editTextBox;

    public Form1()
    {
        InitializeComponent();
        count = 1;
    }

    private void newToolStripMenuItem_Click(object sender, EventArgs e)
    {
        mdiChild = new Form();
        mdiChild.Text = “Document” + count.ToString();
        mdiChild.MdiParent = this;
        editTextBox = new TextBox();
        editTextBox.Multiline = true;
        editTextBox.Dock = Dockstyle.Fill;
        mdiChild.Controls.Add(editTextBox);
        mdiChild.Show();
        count++;
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();

        ofd.Title = “Open a Text File”;
        ofd.Filter = “Text File (*.txt)|*.txt|All Files (*.*)|*.*”;

        DialogResult dr = ofd.ShowDialog();
        If (dr == DialogResult.OK)
        {
            System.IO.StreamReader sr = new System.IO.StreamREader(ofd.FileName);

            Form activeChildForm = this.ActiveMdiChild;

            If (activeChildForm != null)
                 activeTextBox.Text = sr.ReadToEnd();

        {
            System.IO.StreamReader sr = new System.IO.StreamREader(ofd.FileName);

            Form activeChildForm = this.ActiveMdiChild;

            If (activeChildForm != null)
                 activeTextBox.Text = sr.ReadToEnd();

            sr.Close();
        }
    }
}

    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SaveFileDialog sfd = new SaveFileDialog();

        sfd.Title = “Save a Text File”;
        sfd.Filter = “Text File (*.txt)|*.txt|All Files (*.*)|*.*”;

        DialogResult dr = sfd.ShowDialog();
        If (dr == DialogResult.OK)
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName);

            Form activeChildForm = this.ActiveMdiChild;

            If (activeChildForm != null)
            {
                TextBox activeTextBox = (TextBox)activeChildForm.ActiveControl;

                If (activeTextBox != null)
                     sw.Write(activeTextBox.Text);

                sw.Close();
        }
    }
}

    private void cutToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Form activeChildForm = this.ActiveMdiChild;

        if (activeChildForm != null)
        {
            TextBox activeTextBox = (TextBox)activeChildForm.ActiveControl;
            if (activeTextBox != null)
                 activeTextBox.Cut();
        }
    }

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Form activeChildForm = this.ActiveMdiChild;

        if (activeChildForm != null)
        {
            TextBox activeTextBox = (TextBox)activeChildForm.ActiveControl;
            if (activeTextBox != null)
                 activeTextBox.Copy();
        }
    }

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Form activeChildForm = this.ActiveMdiChild;

        if (activeChildForm != null)
        {
            TextBox activeTextBox = (TextBox)activeChildForm.ActiveControl;
            if (activeTextBox != null)
                 activeTextBox.Paste();
        }
    }

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        String file_name = “C:\\test1.txt”;

        System.IO.StreamReader objReader;
        objReader = new System.IO.StreamReader(file_name)l

        textBox1.Text = objReader.ReadToEnd();

        objReader.Close();
    }

    Private void helpToolStripMenuItem_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process.Start (“    “);
4

2 回答 2

0

可以说,如果您在网格中显示它并且使用 Winform Grid.And 您以某种方式在您的单击事件后面的 GUI 中输入信息,即按钮单击事件添加此代码

System.Data.DataTable GridToDT = new System.Data.DataTable();

                GridToDT = (System.Data.DataTable)GridMain.DataSource;

此代码为您提供了一个数据表,其中显示了所有信息现在使用此表您可以做任何您想做的事情..将其保存在数据库中或将其写入 Excel 任何内容...希望这有助于标记为答案,如果您感觉如此。

于 2012-07-24T05:17:34.413 回答
0

你应该把代码放在你的按钮点击中。您可能必须修改代码,因为您的问题有点令人困惑。我不确定您是否想要来自控件或 gridview 的输入,所以我为 gridview 编写了代码

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

        const string path = @"C:\Kelimeler\Test.txt";
        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Server=.;Database=Northwind; UID=sa;PWD=1234");
            SqlCommand cmd = new SqlCommand("Select ShipperID, CompanyName,Phone FROM Shippers", conn);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dtSource = new DataTable();
            da.Fill(dtSource);

            dt.DataSource = dtSource;

            if (dt != null)
            {
                if (!File.Exists(path))
                {
                    File.Create(path);
                }

                foreach (DataGridViewRow row in dt.Rows)
                {
                    for (int i = 0; i < dt.ColumnCount; i++)
                    {
                        File.AppendAllText(path, row.Cells[i].Value.ToString());
                        File.AppendAllText(path, ", ");
                    }
                }
            }
        }
    }
}
于 2012-07-24T07:28:18.850 回答