Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...
Posted
Comments
CHill60 6-Jan-14 14:28pm    
What have you tried so far and where are you stuck?

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:
C#
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.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 6-Jan-14 17:40pm    
I see an issue with c as TextBox. Won't that throw an exception when the control is not a TextBox?
OriginalGriff 6-Jan-14 17:56pm    
Nope, it checks the type and returns null or a text box.
It's like
TextBox tb = (c is TextBox) ? (TextBox) c : (TextBox) null;
Manfred Rudolf Bihy 6-Jan-14 17:59pm    
Thanks, that's good to know! :)
OriginalGriff 7-Jan-14 2:57am    
You're welcome!
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.

C#
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
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900