Clear text in Text box on clicking it - c#

I want my TextBox's text which has already product name on it to automatically vanishes when I click on it, and I can enter the text box I want in it.
The product name must be there always while there is no text in the TextBox
I also want that if I click on the TextBox for the second time I don't lose what I've already entered.
Please let me know how can I do it without loosing the data I've entered manually in it and I can get the default text whenever there is nothing entered by myself in the text box.

why use some extra software and not to use your own mind to code? here is the simple code to achieve this task
first use this:
public bool txSearch = false;
then on your text click event code:
private void txtSearch_Click(object sender, EventArgs e)
{
txSearch = true;
if (txtSearch.Text == "Product Name")
{
if (txSearch == true)
{
txtSearch.Text = "";
}
}
}
this will clear your field text box when you click on the text, now to write back the product name when there is nothing in your textbox and you are leaving it do this code on textbox leaving event:
private void txtSearch_Leave(object sender, EventArgs e)
{
if (txtSearch.Text == "") // here you can also use txtSearch.Text != "Poduct Name", but it could affect your search code possibly
{
txtSearch.Text = "Product Name";
}
}

That behavior is known as watermark. You can either :
Use textbox control with watermark from a library such as WPF extended toolkit
Implement it your self using style and attached behavior as demonstrated in this blog post
Do some trick to achieve the same behavior with simpler code, for example as shown in this codeproject post

You should consider using a third party control, there are plenty WatermarkTextbox Controls available. I prefer the one from xceed: http://wpftoolkit.codeplex.com/wikipage?title=WatermarkTextBox
I wrote this behavior by myself some time ago, used an AdornerDecorator to lay over the TextBox, bound the IsFocused Property to my ViewModel and made a flag ShouldShowWatermark in which I bound the Visibility of the AdornerDecorator.

You actually need a watermark for your textbox.
Please look at this answer of Watermark / hint text TextBox in WPF to implement an attached property to a textbox.

This will work:
Go to the "properties" of your textbox. You will see a yellow lightning bolt in the first line tab. There you will find all possible events that can be triggered. Search for "Enter" or "Click" entry, double-click it. There you can put whatever you want (such as textBox1.Clear();)

Related

Allow text to be dropped into textbox but not written by user

I want to make a text box where text can be dragged/dropped into but where the user cannot write text into themselves.
The pseudo code would be as follows:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if( text is not dropped )
{
txtInstructionReg.Text = "";
}
}
I don't believe that you can use events in exactly the way that you're looking to. Instead, I'd suggest handling each event separately to allow/disallow the actions that you wish to control.
For instance, with your example I would have handlers for both of the events that could happen for the textbox. The event that handles typed text would delete the new input and perhaps display a message to the user that effectively says "You can't do that". Then have the drag/drop handler accept text and put it in the textbox.

How to keep text box's focus when a window pops up

i am building a virtual keyboard to suit the needs of the touch screen machine i'm going to be deploying on. i am using a popup window for the keyboard and have been able to wire all number buttons as follow, here's my virtual keyboard class
public partial class NumKeypad : Window
{
withoutB withoutbvn;
enterBvn ebvnn;
public NumKeypad()
{
InitializeComponent();
}
public NumKeypad(withoutB wobvn)
{
InitializeComponent();
withoutbvn = wobvn;
}
private void one_Click(object sender, RoutedEventArgs e)
{
var focusedElt = FocusManager.GetFocusedElement(withoutbvn);
var tbox = focusedElt as TextBox;
try
{
withoutbvn.ph.Text += (((sender as Button).Content as Border).Child as TextBlock).Text;//this works, but this is assigning directly to only one control. i want to assign to whatever control that has focus
}
catch (Exception ex)
{
}
}
}
on the first line of the one_click function(which handles all input button click) i'm trying to get a reference to the element currently focused in the page whose instance is "withoutbvn".
on the second line, i am tryin to convert the element to a text box so i can write to its text property. but that keeps returning null. meaning when this pop up windows come up(the keyboard pop up window comes up when a textbox or any other input element receives focus), i cannot get a reference to the focused textbox so i cannot write to it. Please how do i ensure a focused textbox remains focused so that i can assign its text property from a pop up window? Or if there's a better way to do this, pls point me in the right direction. Thanks
I've used this keyboard for WPF :
keyboard control wpf
It is a popup control which can be customized as you wish. You have the entire code and it's free. In my case I had to adjust the popup (layout and to add the German letters) and was pretty straightforward.
I also had to show a numeric keyboard, and I've used the same keyboard but with a simpler layout. Behind the scenes, all it is very simple: you have to define a key in a grid, place it where you want and make sure you generate on click the corresponding virtual key code.
i used the popup control to create the keyboard, used buttons to create all keys and wired a single event handler to all input buttons, then different event handlers for the backspace and enter buttons. once any letter, number or symbol button is clicked, the following function gets called.
try
{
IInputElement focusedControl = Keyboard.FocusedElement;
var foc = focusedControl as TextBox;
foc.Text += (((sender as Button).Content as Border).Child as TextBlock).Text;
}
catch (Exception)
{
}
that inserts the button's text to the control in focus. This is pretty basic. i'll appreciate more suggestion on how i can expand on this. Thanks

ComboBox open while another control has focus

I have a case where the user is given a ComboBox with potentially a lot of choices in it. Paired with this is a TextBox that filters the items. What I would like to do is open the drop down list when the TextBox has focus--let the user see what the current filter accomplishes as they type it. (This isn't just autocomplete, I'm currently matching the filter text anywhere in the item, I may replace this with a RegEx search down the road.)
It sounds simple enough--drop the box when the TextBox gets focus, close it when it loses focus. It opens--and promptly closes back up. Any good answers?
My Google-Fu must be weak tonight, I can't believe nobody has wanted to do this before yet I find nothing out there. (I have seen a related thing of typing in an open ComboBox to provide suggested options like Google does but my list is required, not merely suggestions.)
You can add on the Focus event of the TextBox code for the ComboBox setting the property
ComboBox.DroppedDown = true;
Than add on the TextChanged event of the TextBox code
ComboBox.SuspentLayout();
//ComboBox.Items add/remove
ComboBox.ResumeLayout();
Don't forget to reset the items when Text is empty.
EDIT:
This seems to work (but you don't get to see the mouse)
string[] items = { "abcd", "abc", "bcd", "cd" };
private void textBox1_TextChanged(object sender, EventArgs e)
{
comboBox1.SuspendLayout();
comboBox1.Items.Clear();
comboBox1.Items.AddRange(items.Where(item => item.ToLower().Contains(textBox1.Text.ToLower())).ToArray());
comboBox1.ResumeLayout();
comboBox1.DroppedDown = true;
}

How do I specify the default button of a TextBox that is referred to by AcceptsReturn?

I have a windows form with a tool strip on it containing a text box and some buttons (think browser url bar with a go button, back, forward)
I want pressing enter to activate the goButton just as clicking it would, which I believe is what TextBox.AcceptsReturn = false is for.
I don't see anything that seems to fit the bill for "tell me what button on the form is the one that we will activate".
What am I missing?
A Form has a default button, but a specific control does not (out of the box anyway).
In your scenario, I would probably handle invoking the goButton.Click event by monitoring the keys pressed waiting for the Enter key to be pressed.
The easiest way is to set the forms "Accept Button" to the button control you want. This can be done in the designer.
I know this is an Old Question, but for someone who might to to lazy or just a beginner ,
handler might look like too much work ( though it isn't really )
But there is an easier work around for this,
you can make a panel for each of them ( 1 Textbox and 1 Button for Example ) , and set the Defaultbutton for Each panel as you need.
I used this for my site, where I had several Ajax panel , and I Wanted Each to have their own search box on different subjects and work with Enter Button.
Looks like an old question, but I will provide my solution.
private void ChangeDefaultButton()
{
if (this.TextBox.Focused)
{
this.AcceptButton = button;
}
else
{
this.AcceptButton = button1;
}
}
And then add this method to the Focus Events of the text boxes. Like...
private void TextBox_Enter(object sender, EventArgs e)
{
ChangeDefaultButton();
}
And
private void TextBox_Leave(object sender, EventArgs e)
{
ChangeDefaultButton();
}

WinForms: two way TextBox problem

I have a TextBox, which I use for both data input and display. A user can select items from a list and for each item there is a corresponding text which is displayed in the TextBox when the item is selected.
The user can edit this textbox, thus changing the list item's correponding text.
My problem is this: I use the TextChanged event to detect when the user enters some text and I update the internal variables in the event handler, however this event handler is called when I programmatically change the values of the textbox too. I want this handler only to be called when the User changes the textbox. How can I achieve this?
edit: I have the same problem a combo box as well.
The handler will always be called - no way disable it that I'm aware of. However you can simply set a flag in your class to indicate the you've programatically changed the state and should ignore the next event.
_updating = true;
_textBox.Text = "New Text";
...
_textBox_TextChanged( object sender, EventArgs e )
{
if( _updating ) { _updating = false; return; }
// Do something special with the new text.
}
You might also try creating your textbox control and overriding the Text property to provide your own custom logic.
class MyTextBox : TextBox
{
public overrides string Text{
get{ return base.Text; }
set{
if( value == Text ) return;
_updating = true;
base.Text = value;
_updating = false;
}
}
}
The TextChanged event is invoked whenever the TextBox changes its Text property, which can be achieved via modifying the Text property, or when the user directly changes the text, there are other events which are more likely usable for the scenario you are trying to achieve.
KeyPress: Raised whenever the user pulses a key
KeyDown: Raised whenever a Key is pulled down
KeyUp: Raised whenever a Key is pulled up (released)
Hope it helps
You could set a flag in the programatic updates, and in the event handler exit early if the flag is set.
But if you use databinding, then the variables and the gui are synchronized automatically, which avoids this problem all together.
Do you mean you want the second textbox to display something which depends on what's in the first textbox, but ONLY if the user did set it ?
Could you be more specific about what you are trying to achieve? It seems to me that your UI design may be confusing for the user, since the text in B is sometimes related to A, and sometimes not.
If you still want to do it, the typical way to go is to use an updating flag, as proposed in other answers.
Check for focus on the textbox. If it does not have focus, it means the user isn't editing it.

Categories

Resources