如果我们有多个文本框,我想要清除一个文本框而不是其他文本框 [英] If We Have Number Of Textbox And I Want To Clear One Textbox Not Other

查看:89
本文介绍了如果我们有多个文本框,我想要清除一个文本框而不是其他文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c#的新手,我想选择一个文本框...按一个按钮

只清除我用游标选择的文本框。谢谢......

解决方案

所有这些都取决于你如何选择文本框 - 文本框没有选择功能,所以要么你必须将其添加到文本框中,或者您必须添加一个外部控件(例如CheckBox),用户可以使用该控件来指示是否应该选择文本框。



如果这样做,您可以使用TextBox.Tag属性来指示文本框是否被选中并且它很简单。如果我们假设标签中的 null 值表示未选中,而其他任何意味着已选择,那么:

  foreach (控制c 控件中)
{
TextBox tb = c as TextBox;
if (tb!= null && tb.Tag!= null
{
tb.Text = ;
tb.Tag = null ;
}
}

但是如何选择框取决于你。


因为你说了一些关于游标选中的内容我想想也许你想要一个解决方案,你点击一个文本框甚至标签,然后按下删除按钮,它将清除最后得到焦点的文本框。



 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;

命名空间 DeleteSelectedTextbox
{
public partial class Form1:Form
{
private TextBox lastFocusedTextBox = null ;
public Form1()
{
InitializeComponent();
}

private void textBox_Enter( object sender,EventArgs e)
{
TextBox tb =(TextBox)sender;
lastFocusedTextBox = tb;
label1.Text = String .Format( 最后聚焦文本框的名称:{0},tb.Name);

}

private void button1_Click(< span class =code-keyword> object sender,EventArgs e)
{
if (lastFocusedTextBox!= null
{
lastFocusedTextBox.Clear();
}
}
}
}





为所有人使用相同的事件处理程序您的表单上的文本框即 textbox_Enter



问候,

- Manfred
图1:记住最后一个重点的示例表单文本框

i am new to c# and i want to select one textbox ...by one button
clear only those textbox which i selected by cursors. thanks...

解决方案

All of that is dependant on how you "select a textbox" - a textbox doesn't have a "select" function, so either you will have to add that to your textboxes, or you will have to add an external control (such as a CheckBox) which the user can use to indicate if the textbox should be considered selected or not.

If you do that, you could use the TextBox.Tag property to indicate if the textbox is selected and it's simple. If we assume that a null value in a Tag indicates not selected, and anything else means "selected" then:

foreach (Control c in Controls)
   {
   TextBox tb = c as TextBox;
   if (tb != null && tb.Tag != null)
      {
      tb.Text = "";
      tb.Tag = null;
      }
   }

But how to select the boxes is up to you.


Since you said something about "selected by cursors" I thought maybe you wanted a solution where you clicked into a textbox or even tabbed to it and then by pressing the delete button it would clear the textbox that last got the focus.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DeleteSelectedTextbox
{
    public partial class Form1 : Form
    {
        private TextBox lastFocusedTextBox = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox_Enter(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            lastFocusedTextBox = tb;
            label1.Text = String.Format("Last focused textbox's name: {0}", tb.Name);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (lastFocusedTextBox != null)
            {
                lastFocusedTextBox.Clear();
            }
        }
    }
}



Use the same event handler for all of the textboxes on your form i.e. textbox_Enter.

Regards,

— Manfred
Figure 1: Example form remembering the last focused textbox


这篇关于如果我们有多个文本框,我想要清除一个文本框而不是其他文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆