Clearing A Single Texbox when clicked In C#? - c#

How do i make it so that when a text box is clicked the text that was originally in the text box ("Enter Text Here") is cleared and only the first time it is clicked?
Edit: Sorry, I'm using C#.

The most common way to achieve that is to handle the the textbox's focus event (depending on what framework you're using this will vary) and then test for the expected "tip string". If it's there, then you clear the textbox. If not, you leave it alone.
If you only want to show the "tip" once, then you can unsubscribe from the event after you've handled it.
Note that if you give us some more information about what technology you're using (WinForms/WPF/ASP.NET/MVC/jQuery/HTML5/etc.) then a more specific and possibly more robust approach may be possible.

Assuming its WinForms App, simply bind a handler for the GotFocus or Click event.

I wouldn't follow the suggestion of changing text in GotFocus event - it will cause problems during binding and is not elegant.
WinForms:
There is special technique to set this kind of tooltip for any standard Windows textbox. Declare this:
private const uint ECM_FIRST = 0x1500;
private const uint EM_SETCUEBANNER = ECM_FIRST + 1;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
and then use:
private void SetWatermark(string watermarkText)
{
SendMessage(this.Handle, EM_SETCUEBANNER, 0, watermarkText);
}
HTML:
<input name="email" placeholder="Enter text here">

What you are trying to do is called "Watermarking" a Text-Box. There are a number of methods do to it:
1) use the MouseClick event on the textbox to remove the Default text.
2) use a ready available class to implement it like the one found here:
http://vidmar.net/weblog/archive/2008/11/05/watermarked-textbox-in-windows-forms-on-.net.aspx
You can find some more info in similar questions asked on Stackoverflow here are a few:
Watermark / hint text / placeholder TextBox in WPF
Watermark in System.Windows.Forms.TextBox
How to use watermark System.Windows.Forms.TextBox using C#?
Hope this was helpful.
Good Luck.

You can do the following:
handle the focus event (Focus) and clear the text if it's the one set
as "tip"
handle the focus lost event (LostFocus) and if the textbox is empty
add the "tip" back to the textbox

Related

Copy selected text from the CrystalDecisions Report Viewer in Visual Studio

I am writing a Crystal Report viewer in C# Visual Studio. One thing that I have noticed is users want to use Ctrl+C to copy the text they selected (not using the copy button on the viewer). They said it was a feature in a third party application that we switched over from.
How would I achieve this?
I have set the form KeyPreview to true. Using Ctrl+C still does not copy the selected text.
Looping through all of the CrystalReportViewer objects Controls, I have been able to find the control that was highlighted using the Control.Focused getter but there is no indication on which section of that Control contains the highlighted text. That means I cannot even manually copy the text to the clipboard.
I have also tried using the SendMessage PInvoke with WM_COPY, no success.
Here is the code I am using to step through the viewer looking for the correct data and the PInvoke message I am sending.
[DllImport("USER32.DLL", EntryPoint = "SendMessage")]
public static extern IntPtr SendMessage(int hWnd, int Msg, int wParam, IntPtr lParam);
public const int WM_COPY = 0x0301;
CrystalReportViewer viewer;
public void CopySelection()
{
Control.ControlCollection c = viewer.Controls;
GetControl(viewer.Controls);
IntPtr p = SendMessage((int)viewer.Handle, WM_COPY, (int)IntPtr.Zero, IntPtr.Zero);
Console.WriteLine("Copied! " + p);
}
private void GetControl(Control.ControlCollection cItem)
{
foreach (Control c in cItem)
{
if (c.Focused)
Console.WriteLine(c.Focused);
if (c.Controls.Count > 0)
GetControl(c.Controls);
string txt = c.Text;
}
}
Does anybody have any ideas?
Edit:
It seems that after using the 'Find Text' button from the viewer, you are able to copy any text throughout the report with Ctrl+C. Kind of strange. On another note, is there a way to programmatically run the 'Find Text' option?

How to set focus to date portion of DateTimePicker that has a checkbox?

I am using a regular vanilla DateTimePicker (DTP) in Windows Forms. The user has requested that when the window opens, the DTP is selected so they can immediately edit the date with the keyboard.
It is a DTP with a checkbox enabled, and so when I use the standard
DTP.Select()
it selects the checkbox, not the date-part of the control, which achieves nothing useful for the user.
I am sensing there is no way around this beyond hacks like SendKeys, which I will resort to if nobody has a good solution.
Thought I'd check with the boffins!
You must select the portion after it has the focus
For example :
SwitchToThisWindow(DTP.Handle, true);
SendKeys.Send("{RIGHT}");
with declaration :
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool SwitchToThisWindow(IntPtr hWnd, Boolean fAltTab);

Change text in another application NumericUpDown

I'm a little stuck. I have successfully changed the text in normal TextBoxes in said application but am stuck on changing this one. Im not sure what this is called so I have called it a scroll TextBox, also it will only accept numbers not letters.
The code im using
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);
const int WM_SETTEXT = 0X000C;
public void Main(IntPtr handle, string text)
{
SendMessage(handle, WM_SETTEXT, 0, 555);
}
I have also tried changing lParam into a StringBuilder which has worked in other TextBoxes but hasnt for this one.
This is the type of TextBox.
Using windows defender there isn't a Edit child like the usual TextBoxes. The only handle I have is for the whole element.
What are my choices here to be able to get that text changed?
The control is known as a NumericUpDown. The text is a representation of the numerical value that it holds, unlike a traditional textbox where you may parse the text to get a number. Depending on your intentions, you may need to find a way to change the underlying numerical value to effectively modify this type of control from another process.

How to always show underline character? (C# Windows Form)

I'm making a dialog that look like Notepad's Find Dialog. I notice that the underline character of Notepad's Find dialog always show all the time (I have to press ALT key to see this with my dialog). How to always show underline character like that?
I try to use SendKeys.Send("%") on Form_Load event but nothing happens.
There is another problem, when I press ALT key on child Form, it show underline charater of parent Form too. How to avoid that?
This is sreenshot of Notepad's find dialog:
I pretty sure this is not about Ease of Acess Center, because the main Form of Notepad doesn't always show this.
Seeing the n in "Find" underlined in the Notepad dialog is an intentional bug. The dialog isn't actually part of Notepad, it built into Windows. Underlying winapi call is FindText(). The feature is in general a pile 'o bugs, one core problem is that creating a new window after the UI is put in the "show underlines" state doesn't work correctly, that new window isn't also in that state. Presumably the intentional bug was based on the assumption that the user would be somewhat likely to use the Alt key to get the dialog displayed. Yuck if he pressed Ctrl+F.
The Windows dialog probably does it by simply drawing the "Find" string with DrawText() with the DT_NOPREFIX option omitted. You could do the same with TextRenderer.DrawText(), omit the TextFormatFlags.HidePrefix option.
Not exactly WinFormsy, you'd favor a Label control instead of code. It is hackable, you'd have to intentionally send the message that puts the UI in the "show underlines" state for your own dialog. Do so in an override for the OnHandleCreated() method:
protected override void OnHandleCreated(EventArgs e) {
const int WM_UPDATEUISTATE = 0x0128;
base.OnHandleCreated(e);
SendMessage(this.label1.Handle, WM_UPDATEUISTATE, new IntPtr(0x30002), IntPtr.Zero);
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
Where "label1" is the control you want to show underlines. Repeat for other controls, if any. It is supposed to work by sending the message to the form, that this doesn't work is part of the pile 'o bugs. Yuck.
Fwiw: do not fix this by changing the system option as recommended in the duplicate. That's very unreasonable.
You can use RichTextBox control and extension method for that:
public static class FontHelper
{
public static void Underline(this RichTextBox txtBox, int underlineStart, int length)
{
if (underlineStart > 0)
{
txtBox.SelectionStart = underlineStart;
txtBox.SelectionLength = length;
txtBox.SelectionFont = new Font(txtBox.SelectionFont, FontStyle.Underline);
txtBox.SelectionLength = 0;
}
}
}
richTextBox1.Text = "Search for";
richTextBox1.Underline(7, 1); // index and length of underlying text

How to show the DropDown list of a ComboBox in WinForms (Telerik)

I'm trying to initiate the drop down list click for a combobox of type MultiColumnComboBox (RadMultiColumnComboBox).
The behavior I'm trying to emulate is when the user clicks the [v] button of the drop down, which shows the actual list.
My control is a Telerik.WinControls.UI.RadMultiColumnComboBox.
I saw a post on the Telerik forums suggesting to do something like this:
Dim item As RadTextBoxItem = TryCast(Me.radMultiColumnComboBox1.MultiColumnComboBoxElement.Children(2).Children(0).Children(0), RadTextBoxItem)
If item IsNot Nothing Then
AddHandler item.Click, AddressOf OnTextBoxItem_Click
End If
Seems like a viable solution, but I'm not sure how this would work on my C# control.
There is also a Win32 hack I found, but this would not pass code review:
// Declare the following in your class
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
public const int CB_SHOWDROPDOWN = 0x14F;
// In the leave event of combobox, use the following code:
SendMessage(comboBox1.Handle.ToInt32(), CB_SHOWDROPDOWN, 1, IntPtr.Zero);
If anyone is familiar with a WinForms ComboBox and can help me figure out how to kick off the Show Items/Elements/List event (or whatever its called), I'd really appreciate it!
The equivalent c# is:
RadTextBoxItem item = this.radMultiColumnComboBox1.MultiColumnComboBoxElement.Children(2).Children(0).Children(0) as RadTextBoxItem;
if (item != null) {
item.Click += OnTextBoxItem_Click;
}
Check if it works for you.
If I understand correctly, you want to open the drop down programatically. If this is the case, here is how you can do that:
radMultiColumnComboBox1.MultiColumnComboBoxElement.ShowPopup();

Categories