例如伪代码;
textBox1.Clear();
textBox1.Lines.Add("1000+");
textBox1.Lines.Add("750-999");
textBox1.Lines.Add("400-749");
...snip...
textBox1.Lines.Add("40-59");
Run Code Online (Sandbox Code Playgroud)
要么
textBox1.Lines.Append("brown");
textBox1.Lines.Append("brwn");
textBox1.Lines.Append("brn");
textBox1.Lines.Append("brow");
textBox1.Lines.Append("br");
textBox1.Lines.Append("brw");
textBox1.Lines.Append("brwm");
textBox1.Lines.Append("bron");
textBox1.Lines.Append("bwn");
textBox1.Lines.Append("brnw");
textBox1.Lines.Append("bren");
textBox1.Lines.Append("broe");
textBox1.Lines.Append("bewn");
Run Code Online (Sandbox Code Playgroud)
TextBox.Lines实现的唯一方法(我可以看到)是:

任何人都可以指向一个基本的Windows窗体TextBox的良好实现,它最初会显示水印文本,当光标进入时它会消失吗?我想我可以通过创造性地使用Enter和Leave事件来创建我自己的东西,但我确信在某个地方有一个完全可用的实现.我看到了WPF实现,如果有必要,我可以嵌套它,但原生的WinForms TextBox派生会更好.
到目前为止,我有这个; 尚未尝试过,但有没有人看到任何明显的问题?
public class WatermarkTextBox:TextBox
{
public string WatermarkText { get; set; }
public Color WatermarkColor { get; set; }
private Color TextColor { get; set; }
private bool isInTransition;
public WatermarkTextBox()
{
WatermarkColor = SystemColors.GrayText;
}
private bool HasText { get { return Text.IsNotNullOrBlankOr(WatermarkText); }}
protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
if (HasText) return;
isInTransition = true;
ForeColor = TextColor;
Text = String.Empty;
isInTransition = false;
}
protected override void OnForeColorChanged(EventArgs e)
{
base.OnForeColorChanged(e);
if (!isInTransition) …Run Code Online (Sandbox Code Playgroud) 我想在右边添加一些空格,<input type="text" />以便在场的右边有一些空的空间.
所以,而不是
,我明白了
.
所以,同样的行为只是右边的一些空白空间.
我尝试过使用padding-right,但似乎没有做任何事情.
有没有办法做到这一点(或伪造它)?
我正在尝试使用WPF中的数据绑定在TextBox中显示格式化的十进制数.
目标1:在代码中设置小数属性时,在TextBox中显示2个小数位.
目标2:当用户与TextBox交互(输入)时,不要惹恼他/她.
目标3:绑定必须更新PropertyChanged上的源.
尝试1:没有格式化.
在这里,我们几乎从头开始.
<TextBox Text="{Binding Path=SomeDecimal, UpdateSourceTrigger=PropertyChanged}" />
Run Code Online (Sandbox Code Playgroud)
违反目标1. SomeDecimal = 4.5将在TextBox中显示"4.50000".
尝试2:在绑定中使用StringFormat.
<TextBox Text="{Binding Path=SomeDecimal, UpdateSourceTrigger=PropertyChanged, StringFormat=F2}" />
Run Code Online (Sandbox Code Playgroud)
违反目标2.说SomeDecimal是2.5,TextBox显示"2.50".如果我们选择all并输入"13.5",我们在TextBox中以"13.5.00"结束,因为格式化程序"有用"地插入小数和零.
尝试3:使用Extended WPF Toolkit的MaskedTextBox.
http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox
假设我正在正确阅读文档,掩码## 0.00表示"两个可选数字,后跟一个必需的数字,一个小数点,还有两个必需的数字.这迫使我说"可以进入的最大可能数字这个TextBox是999.99",但让我说我很好.
<xctk:MaskedTextBox Value="{Binding Path=SomeDecimal, UpdateSourceTrigger=PropertyChanged}" Mask="##0.00" />
Run Code Online (Sandbox Code Playgroud)
违反目标2. TextBox以___.__选择它并输入5.75产量开始575.__.获得5.75的唯一方法是选择TextBox并输入<space><space>5.75.
尝试4:使用Extended WPF Toolkit的DecimalUpDown微调器.
http://wpftoolkit.codeplex.com/wikipage?title=DecimalUpDown
<xctk:DecimalUpDown Value="{Binding Path=SomeDecimal, UpdateSourceTrigger=PropertyChanged}" FormatString="F2" />
Run Code Online (Sandbox Code Playgroud)
违反目标3.DecimalUpDown愉快地忽略UpdateSourceTrigger = PropertyChanged.Extended WPF Toolkit Codeplex页面上的协调员之一建议在http://wpftoolkit.codeplex.com/discussions/352551/上修改ControlTemplate .这符合目标3,但违反了目标2,表现出与尝试2中相同的行为.
尝试5:使用样式数据触发器,仅在用户未编辑时才使用格式.
使用TextBox上的StringFormat绑定到double
即使这个目标满足所有三个目标,我也不想使用它.(A)文本框每行变为12行而不是1行,我的应用程序包含许多很多文本框.(B)我的所有文本框都有一个Style属性,该属性指向设置边距,高度和其他东西的全局StaticResource.(C)您可能已经注意到以下代码将绑定路径设置为两次,这违反了DRY主体.
<TextBox>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Text" Value="{Binding Path=SomeDecimal, StringFormat=F2}" />
<Style.Triggers> …Run Code Online (Sandbox Code Playgroud) 是否可以在WPF中使文本框自动完成?
我找到了一个示例,其中使用了一个组合框,并通过编辑样式模板删除了三角形.
有更好的解决方案吗?
我有这门课:
public partial class Window1 : Window
{
public String Name2;
public Window1()
{
InitializeComponent();
Name2 = new String('a', 5);
myGrid.DataContext = this;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
我想Name2在文本框中显示字符串.
<Grid Name="myGrid" Height="437.274">
<TextBox Text="{Binding Path=Name2}"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
但是不显示字符串.此外,如果Name2使用a定期更新字符串,是否TimerCallback需要执行任何操作以确保在数据更改时更新文本框?
我正在处理下面的代码,检查textField1和textField2文本字段是否有任何输入.
IF当我按下按钮时,声明没有做任何事情.
@IBOutlet var textField1 : UITextField = UITextField()
@IBOutlet var textField2 : UITextField = UITextField()
@IBAction func Button(sender : AnyObject)
{
if textField1 == "" || textField2 == ""
{
//then do something
}
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个Windows窗体应用程序,它要求我调用一个单独的程序来执行任务.该程序是一个控制台应用程序,我需要将标准输出从控制台重定向到我的程序中的TextBox.
从我的应用程序执行程序没有问题,但我不知道如何将输出重定向到我的应用程序.我需要在程序使用事件运行时捕获输出.
在我的应用程序停止并且文本以随机间隔不断变化之前,控制台程序并不意味着停止运行.我试图做的只是从控制台挂钩输出以触发事件处理程序,然后可以使用它来更新TextBox.
我使用C#编写程序代码并使用.NET框架进行开发.原始应用程序不是.NET程序.
编辑:这是我正在尝试做的示例代码.在我的最终应用程序中,我将用代码替换Console.WriteLine来更新TextBox.我试图在我的事件处理程序中设置断点,甚至没有达到.
void Method()
{
var p = new Process();
var path = @"C:\ConsoleApp.exe";
p.StartInfo.FileName = path;
p.StartInfo.UseShellExecute = false;
p.OutputDataReceived += p_OutputDataReceived;
p.Start();
}
static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(">>> {0}", e.Data);
}
Run Code Online (Sandbox Code Playgroud) 在我的HTML页面中,我有一个文本框供用户输入搜索关键字.当他们单击搜索按钮时,JavaScript函数将生成一个URL并在新窗口中运行.
当用户通过鼠标单击搜索按钮时,JavaScript函数可正常工作,但当用户按下ENTER键时没有响应.
function searching(){
var keywordsStr = document.getElementById('keywords').value;
var cmd ="http://XXX/advancedsearch_result.asp?language=ENG&+"+ encodeURI(keywordsStr) + "&x=11&y=4";
window.location = cmd;
}
Run Code Online (Sandbox Code Playgroud) textbox ×10
winforms ×3
wpf ×3
.net ×2
c# ×2
html ×2
autocomplete ×1
console ×1
css ×1
data-binding ×1
decimal ×1
dom-events ×1
input ×1
javascript ×1
keypress ×1
onkeypress ×1
swift ×1
text ×1
watermark ×1
wpf-controls ×1