我有一个带有文本框的表单,用于输入 URL。我需要将 (http://) 作为预定义值添加到此文本框,并希望它是只读的,这样用户将无法删除 http://,但他可以在其后写入。
任何帮助将不胜感激。
这里有几个选项:
最简单的方法是使用这些字符在文本框外(左侧)创建一个标签。(简单易懂的用户)
创建第二个只读文本框以在开始时使用,设置其样式以匹配输入文本框并将它们彼此相邻对齐。是的,你会得到一条像素线来分割它们,但我认为这会增加用户体验,使其明显不是为了搞乱(我个人会选择这个选项)
如果您需要样式,您可以滚动您自己的用户控件,该控件使用面板、标签和文本框,并根据需要设置适当的边框样式。(获得您需要的确切样式的最佳方法)
第四种更烦人的方法是处理文本框本身的一个关键事件(例如KeyDown)。有了这个,您可以进行大量检查并更改插入符号的位置以使其正常工作,但相信我,这会让您努力使其完美工作!(太多的努力才能做到正确)
总而言之,我认为选项 2是最好的。当然,如果您使用的是 WPF,您无疑会在样式方面拥有更多的灵活性。
您是否考虑在其旁边放置一个带有“http://”作为文本的标签?然后在接受用户输入时,您只需在 textbox.Text 中附加“http://”即可。
这是另一个想法:
每次按退格键时,计算文本框中的字符数。如果是 == 7,则忽略退格。如果更大,则检查退格后的字符数。如果字符数少于 7,清除文本框并重置文本。
private void a_keyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)8)
{
if (myTextbox.Text.Length == 7)
// do stuff..
}
else if //do stuff...
}
您甚至可以不显示 http:// 并将其附加到 Textbox.Text 代码。首先检查它是否也不是以此开头。
为了澄清我的最后一句话:
string sURL = txtURL.Text.StartsWith("http://") ? txtURL.Text : "http://" + txtURL.Text;
像这样的东西?
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
var textBox = sender as TextBox;
if (!textBox.Text.StartsWith("http://"))
{
textBox.Text = "http://";
textBox.Select(textBox.Text.Length, 0);
}
}
您可以改用 RichTextBox,它允许保护文本:
public Form1() {
InitializeComponent();
richTextBox1.Text = "http://";
richTextBox1.SelectAll();
richTextBox1.SelectionProtected = true;
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.DetectUrls = false; // optional
}
但不幸的是,如果将它的 Multiline 属性设置为 False,它就不能很好地工作。
使用 TextBox 执行此操作的一种实用方法是将其设置为您想要的方式。也适用于粘贴和选择删除:
string protect = "http://";
private void textBox1_TextChanged(object sender, EventArgs e) {
if (!textBox1.Text.StartsWith(protect)) {
textBox1.Text = protect;
textBox1.SelectionStart = textBox1.Text.Length;
}
}
注意:我误读了这个问题,因为不知何故我是从“HTML”标签来到这里的。但是如果你想用 HTML/CSS 做这样的事情,这可能是一种解决方案。
你可以这样做:
<style>
label.mylabel, input.myinput {
display: block;
float: left;
height: 20px;
margin: 0;
padding: 10px 5px 0px 5px;
border: 1px solid #ccc;
font-size: 11px;
line-height: 11px;
}
label.mylabel {
border-right: 0;
}
input.myinput {
border-left: 0;
}
</style>
<label class="mylabel" for="myinput">http://</label>
<input id="myinput" class="myinput" name="myinput" value="">
所以这有两个好处:
当然,您必须在发送表单后手动添加“http://”。
整件事有一个缺点。什么是,如果您的用户想要插入“https://”?:)
干杯,菲利普
如果你想要一个 CSS 方法(加上背景图片),你可以尝试这样的事情:
Enter URL: <input type="text" size="50" class="url" value="www.google.com" />
<style>
input[type="text"].url {
background: url(http://s18.postimage.org/4wkjdpidh/http.png) no-repeat left top transparent;
text-indent: 34px;
}
</style>
然后,只需http://
在处理输入值时将其添加到后面即可。
作为一个选项,您可以添加一个自动大小的标签来TextBox
控制。同样要在标签之后强制文本开始,您需要发送EM_SETMARGINS
以将左填充应用于文本框:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class SampleTextBox : TextBox
{
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hwnd, int msg, int wParam, int lParam);
private const int EM_SETMARGINS = 0xd3;
private const int EC_LEFTMARGIN = 1;
private Label label;
public SampleTextBox() : base()
{
label = new Label() { Text = "http://", Dock = DockStyle.Left, AutoSize = true };
this.Controls.Add(label);
}
public string Label
{
get { return label.Text; }
set { label.Text = value; if (IsHandleCreated) SetMargin(); }
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
SetMargin();
}
private void SetMargin()
{
SendMessage(this.Handle, EM_SETMARGINS, EC_LEFTMARGIN, label.Width);
}
}
您可以在文本框左侧放置一个标签并将其文本属性设置为“http://”。或者您可以附加 2 个文本框,一个是只读的,另一个不是只读的。并在只读的地方写 http:// 。
为事件(可能还有其他,例如剪贴板)创建一个委托keyup
并检查值的开始。
$('#myField').bind('keypress', function(event)
{
if (event.charCode == 0 && event.currentTarget.value.length <= 7)
{
return false;
}
});