我有一个包含10行和4列的GUI。它的目的是让我输入每个框的输入,在底部,单击提交按钮,它将给我一个包含所有输入的文件。我遇到的问题是如何编写“按钮”来执行此操作?帮助
我的menustrip也遇到了问题。似乎一切都是正确的,但我的视觉效果不会接受它。我的代码有问题吗?
- 这是我第一次使用C#和visual,所以任何帮助都会非常感激!
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 (“ “);
答案 0 :(得分:0)
让我们说如果你在网格中显示它并使用Winform Grid.And你以某种方式在GUI中输入信息 点击事件后面的按钮点击事件添加此代码
System.Data.DataTable GridToDT = new System.Data.DataTable();
GridToDT = (System.Data.DataTable)GridMain.DataSource;
此代码为您提供了一个数据表,其中显示了所有信息现在使用此表您可以执行任何您想要的操作...即将其保存在数据库中或将其写入Excel任何内容...希望这有助于标记为如果你这么回答。
答案 1 :(得分: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, ", ");
}
}
}
}
}
}