简体   繁体   中英

How do I get the information typed into a TextBox?

I want to utilize the information typed into a TextBox in the future part of this program. This program sets it all up in a form and when I type something into the textbox and press the Enter key, it goes to line 32, the Console.WriteLine , correctly and shows the quoted part "do calculations here" but not the value, txtFolder, typed inside the TextBox.

using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

public class MainForm : Form {

  private TextBox txtFolder;

  // set up the window
  public MainForm() {
    InitializeComponents();
  }

  private void InitializeComponents() {
    this.Text = "Textbox";
    Width = 500;

    // get textbox entry
    Label sbfldr           = new Label();
    sbfldr.Location        = new Point(10,140);
    sbfldr.Size            = new Size (315,20);
    sbfldr.Text = "Enter something into textbox and press the ENTER key";
    this.Controls.Add(sbfldr);
    TextBox txtFolder      = new TextBox();
    txtFolder.Location     = new Point(325,139);
    txtFolder.KeyUp += getFolder;
    txtFolder.Parent = this;
    this.Controls.Add(txtFolder);
  }

  private void getFolder(object sender, KeyEventArgs e) {
    if (e.KeyCode == Keys.Enter) {
      Console.WriteLine("do calculations here "+txtFolder);
      e.Handled = true;
    }
  }

  public class Program {
    public static void Main(string[] args) {
      Application.Run( new MainForm() );
    }
  }
}

You can get the value of a TextBox using the Text property:

string myValue = myTextBox.Text;

In your code, you would have this:

Console.WriteLine("do calculations here " + txtFolder.Text);

Here is the documentation for TextBox.Text

Change this:

Console.WriteLine("do calculations here "+txtFolder);

To this:

Console.WriteLine("do calculations here "+txtFolder.Text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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