C#Windows窗体应用程序,带有标题

时间:2017-10-17 20:12:50

标签: c# winforms button readline

我想创建由用户命名的按钮。

这就是我所拥有的(对不起,我在这方面做得很糟糕,但我正在努力:D)

string 1;

private void button1_Click(object sender, EventArgs e)
{
    if (1 = null)
    {
        Console.Write("Give Button's name");
        1= Console.ReadLine();
        button1.Name = 1;   
    }
}

我还希望按钮打开用户选择的文件路径。有没有办法做到这一点?

(对不起我的愚蠢问题和其他任何事情......:D)

2 个答案:

答案 0 :(得分:2)

根据您的评论:  1.使用TextBox(称为txtUserInput)获取用户的输入  2.使用OpenFileDialog帮助用户选择文件

private void button1_Click(object sender, EventArgs e)
{
    if (!String.IsNullOrEmpty(txtUserInput.Text))
    {
        button1.Text = txtUserInput.Text;
        var openFileDialog = new OpenFileDialog();
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            MessageBox.Show("You chose: " + openFileDialog.FileName);
        }
    }
}

答案 1 :(得分:0)

您无法将控制台应用程序输入与Windows窗体输入混合使用。你需要决定你想要什么。从评论中看,您似乎想要一个Windows窗体应用程序,因此您需要一些方法来获取用户输入,如文本框。

在表单上,​​您需要创建一个文本框,并为其命名为tbxUserInput。之后,您可以更改单击方法以执行以下操作:

private void button1_Click(object sender, EventArgs e)
{
    button1.Text = tbxUserInput.Text
}

有一件事是你分配了按钮的Name属性而不是Text属性。 text属性是按钮上实际显示的内容。

阅读有关Windows窗体的一些教程可能是个好主意。