C# 线程获取/设置控件(TextBox)值
线程读写控件需要用委托(delegate)与Invoke/BeginInvoke来进行
参考内容:http://www.cnblogs.com/runner/archive/2011/12/30/2307576.html
1. 获取TextBox中的值
代码一:
public delegate string GetTextBoxCallBack();
private string GetInputText()
{
try
{
if (this.txtInput.InvokeRequired)
{
GetTextBoxCallBack gtb = new GetTextBoxCallBack(GetInputText);
IAsyncResult ia = txtInput.BeginInvoke(gtb);
return (string)txtInput.EndInvoke(ia); //这里需要利用EndInvoke来获取返回值
}
else
{
return txtInput.Text;
}
}
catch (Exception ex)
{
return "";
}
}
代码二:
private string GetTextCallBack()
{
if (this.txtInput.InvokeRequired)
{
string inputTxt = string.Empty;
this.txtInput.Invoke(new MethodInvoker(delegate { inputTxt = txtInput.Text; }));
return inputTxt;
}
else
{
return this.txtInput.Text;
}
}
2.线程设置TextBox值
代码一:
delegate void SetTextBoxCallback(string text);
private void SetInputText(string text)
{
if (this.txtInput.InvokeRequired)
{
SetTextBoxCallback d = new SetTextBoxCallback(SetInputText);
this.Invoke(d, new object[] { text });
}
else
{
this.txtInput.Text = text;
}
}
代码二:
string changeTxt = "Change Text";
txtInput.Invoke(new Action<String>(p =>
{
txtInput.Text = changeTxt;
}), txtInput.Text);
C# 线程获取/设置控件(TextBox)值的更多相关文章
- WPF非UI线程获取修改控件属性值的方法
public class InvokeHelper { #region delegates private delegate object MethodInvoker(Control control, ...
- Android线程中设置控件
在Android中经常出现多线程中设置控件的值报错的情况,今天教大家封装一个简单的类避免这样的问题,同样也调用实现也非常的方便. 自定义类: /** * Created by wade on 2016 ...
- Jquary获取页面控件的值
一 Jquery获得服务器控件值的方法由于ASP.NET网页运行后,服务器控件会随机生成客户端id,jquery获取时候不太好操作,google了下,总结有以下3种方法: 服务器控件代码:<as ...
- asp.net后台获取html控件的值
1.asp.net后台获取前台type=text控件的值 前台:<input name="txtName" class="username" type=& ...
- C# 动态代码生成控件后其他事件不能获取该控件的值
1.新建web项目,添加两个Button控件,结果如图. 2.Button按钮控件点击事件代码如下 protectedvoid Button1_Click(object sender, EventAr ...
- 在后台获取Textarea控件的值
使用Request.Form方法 1.在前台设置name 属性 <textarea name="Content">hdjfhjdfhdj</textarea> ...
- 后台获取html控件的值
string name = Request.Form["Name1"].ToString(); “xxx” 里的是name值 Request["xx"]取到相同 ...
- .NET后台如何获取前台HMTL控件的值
很多时候我们需要HTML控件,感觉比服务器控件更加简介,清爽,那么如何获取HMTL控件的值呢,请看下面例子: 前台页面代码: <input id="Text1" type=& ...
- HTML控件ID和NAME属性及在CS页面获得.ASPX页面中HTML控件的值
<转载>来自网络 一.ID是在客户端脚本里用!NAME是用于获取提交表单的某表单域信息,在form里面,如果不指定Name的话,就不会发送到服务器端,所以有name属性的控件,必须指定na ...
随机推荐
- sqlserver查找使用了某个字段的所有存储过程
当一个系统中使用了很多的表,并且存在大量的存储过程,当数据库中的某个表删除了某个字段,那么相应的存储过程也需要改动,但是我们不知道哪些存储过程使用了该字段,那我们该怎么办?我们可以从之前的文档一个一个 ...
- sublimit 编辑器 设置默认的编码
1.首选项>>设置 - 用户 2.加上:"default_encoding": "UTF-8"
- bootstrap-datetimepicker.js的漢化注意點
1.要引入bootstrap.css ,datetime.picker.css 2.引入的JS文件如下: <script type="text/javascript" src ...
- SQLServer 2008 已成功与服务器建立连接,但是在登录前的握手期间发生错误。 (provider: SSL Provider, error: 0 - 等待的操作过时。
在用SQL Server 2008 在连接其他电脑的实例时,一直提示“已成功与服务器建立连接,但是在登录前的握手期间发生错误. (provider: SSL Provider, error: 0 - ...
- 【python】python中的enumerate()函数【笔记】
结合实例来理解比较好,网上找了一下这个enumerate用法,自己也记录一下加深印象 看一下相关链接: 链接1:http://www.cnblogs.com/danielStudy/p/6576040 ...
- mysql查看及设置最大连接数
#查看: show variables like '%max_connections%'; #设置: set GLOBAL max_connections = 1000;
- gitlab api 使用
api文档:https://docs.gitlab.com/ee/api/projects.html#project-visibility-level 1.项目查询 http://127.0.0.1: ...
- LevelDB源码分析-Bloom Filter
Bloom Filter bloom filter是leveldb中用来在一个block中检测key是否存在的工具,主要在BloomFilterPolicy类中实现: class BloomFilte ...
- 把nginx当完全tcp端口转发器
在nginx.conf里加入 stream { server { listen 18443; proxy_pass 58.xxx.xxx.xxx:8443; ...
- Ubuntu搜狗拼音输入法崩溃问题
Ubuntu 14.04.5 LTS 环境下搜狗拼音经常崩溃,似乎也没有什么特别好的解决办法. 以下是重启命令 #!/bin/sh >/dev/>& >/dev/>&a ...