简体   繁体   English

C#Windows窗体应用程序,带有读取行的按钮名称

[英]C# Windows Form Application, button name with readline

I want to create button that is named by the user. 我想创建由用户命名的按钮。

This is what I have (sorry I'm so bad at this but I am trying :D) 这就是我所拥有的(对不起,我很抱歉,但是我正在尝试: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;   
    }
}

I also want the button to open a file path selected by the user. 我还希望按钮打开用户选择的文件路径。 Is there a way to do that? 有没有办法做到这一点?

(sorry for my stupid question and anything else... :D) (对不起我的愚蠢问题和其他任何事情...:D)

Based on yr comments: 1. Use a TextBox(called txtUserInput) to get the user's input 2. Use OpenFileDialog to help user to choose a file 基于您的评论: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);
        }
    }
}

You can't mix Console Application input with Windows Forms input. 您不能将控制台应用程序输入与Windows窗体输入混合使用。 You need to decide what you want. 您需要决定想要什么。 From the comments, it looks like you want a Windows Form Application so you will need some way to get user input like a text box. 从注释中看,您似乎需要Windows窗体应用程序,因此您将需要某种方式来获取用户输入,例如文本框。

On your form you will need to create a text box and give it a name like tbxUserInput . 在您的表单上,您将需要创建一个文本框,并为其命名,例如tbxUserInput After that you can change you click method to do something like this: 之后,您可以更改单击方法以执行以下操作:

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

One thing that was wrong was that you were assigning the Name property of the button and not the Text property. 错误的一件事是您分配的是按钮的Name属性,而不是Text属性。 The text property is what is actually displayed on the button. 文本属性是按钮上实际显示的内容。

It may be a good idea to read some tutorials about Windows Forms. 阅读一些有关Windows窗体的教程可能是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM