C#获取文本框输入的值``````

zelong77 2007-05-09 08:33:50
如题
...全文
1746 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zelong77 2007-05-09
  • 打赏
  • 举报
回复
错误 1 当前上下文中不存在名称“textBox1” D:\我的文档\Visual Studio 2005\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 57 22 ConsoleApplication1
-过客- 2007-05-09
  • 打赏
  • 举报
回复
private void btn_Click(object sender, EventArgs e)
{
  myText = textBox1.Text;
MessageBox.Show(myText);
}
zelong77 2007-05-09
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ConsoleApplication1
{
class Program : System.Windows.Forms.Form
{
String myText = "";
public Program()
{

ct();
}
static void Main(string[] args)
{
Program pro = new Program();
pro.Size = new Size(500, 400);
Application.Run(pro);
}
public void ct()
{

Label lbl = new Label();
lbl.Location = new Point(10,10);
lbl.Text = "终点站:";
lbl.Size = new Size(50,10);
this.Controls.Add(lbl);

TextBox txt = new TextBox();
txt.Location = new Point(60, 10);
//txt.Text = "123";
txt.Size = new Size(300,10);
this.Controls.Add(txt);

Button btn = new Button();
btn.Location = new Point(380,10);
btn.Text = "搜索(s)";
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(btn);

ListBox lst = new ListBox();
lst.Location = new Point(10,50);
lst.Size = new Size(450,300);
this.Controls.Add(lst);

//String myText = txt.Text;
//MessageBox.Show(myText);
myText = txt.Text;
}

void btn_Click(object sender, EventArgs e)
{

myText =
MessageBox.Show(myText);

}
}
}
zelong77 2007-05-09
  • 打赏
  • 举报
回复
textBox1就是我先定义的TextBox名吧??
在事件里点不出来啊````
声明以后可以点出来不过获取不到值``
shinaterry 2007-05-09
  • 打赏
  • 举报
回复
慢慢来吧。。。

就是LS说的那个...

努力点喔。。。
-过客- 2007-05-09
  • 打赏
  • 举报
回复
那也没什么不同,直接这样就行了

private void button2_Click(object sender, EventArgs e)
{
  string myText = textBox1.Text;
}

如果通过点击按钮后得到的字符串,还要在别的地方用到,可以用一个全局变量来保存

string myText = "";
private void button2_Click(object sender, EventArgs e)
{
  myText = textBox1.Text;
}
zelong77 2007-05-09
  • 打赏
  • 举报
回复
就是这个```
不过我是在点击事件里获取```
不在一个类不知道怎么弄了`````呵呵```
刚刚学`````
-过客- 2007-05-09
  • 打赏
  • 举报
回复
-_-#楼主不会是说这样的吧

string myText = textBox1.Text;

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧