Highlighting labels Windows Forms - c#

Is there any way to make a label on a .NET Windows form to be highlightable to allow for the text to be copied. I have attempted to do this with a text box that was made to look like a label, but this results in a flashing cursor.

I think this is pretty darn close:
textBox.BackColor = System.Drawing.SystemColors.Control;
textBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
textBox.ReadOnly = true;
textBox.Text = "This is selectable text";
textBox.MouseUp += new MouseEventHandler(
delegate(object sender, MouseEventArgs e)
{ HideCaret((sender as Control).Handle); });
[DllImport("User32.dll")]
static extern Boolean HideCaret(IntPtr hWnd);
And if you need it to span more than one line:
textBox.Multiline = true;

If you want it to be a predictable, well behaved and standard control with all the keyboard and shortcut support you simply need a textbox. And then the flashing cursor is a normal helpful feature, why fight it?

It's not unusual for selectable static text to show a flashing cursor. If you get the properties of any file in Windows Explorer and select any data in that window, you'll also see a flashing cursor.

I have done this previously, a couple of years back, I think I used this Win API call (but with a regular text box): http://www.dreamincode.net/forums/showtopic35107.htm

You have the HideCaret function in User32.dll. Use it like this:
[DllImport("User32.dll")]
static extern bool HideCaret(IntPtr hWnd);
private void textBox_Enter(object sender, EventArgs e)
{
HideCaret(textBox.Handle);
}
This will prevent the caret from showing when textbox has focus.

One thing to consider is to go ahead and use a label, but then programmatically copy content (the Label's text) into the clipboard using:
Clipboard.SetText(yourLabel.Text);

Related

Timepicker does not show the arrow beside it

I would like to know i have removed Application.EnableVisualStyles () as the program i develop for progress bar it will be easier if i remove that statement of "Application.EnableVisualStyles". The problem i face was, when i put datetimepicker i set the showupdown to true, the arrow in show up down does not display as formerly with the statement "Application.EnabledStyles". Is there a way i can solve it without addding "Application.EnableVisualStyles". Thanks
It is like this for the datetimepicker
enter image description here
i want like this for the datetimepicker with the enabled visual style removed
enter image description here
If you just want to use the old ProgressBar, this Hans Passant answer How do I disable visual styles for just one control, and not its children? shows how to turn visual styles off for a single control. I've adapted that code for your progress bar:
public class OldProgressBar : ProgressBar {
[DllImportAttribute("uxtheme.dll")]
private static extern int SetWindowTheme(IntPtr hWnd, string appName, string idlist);
protected override void OnHandleCreated(EventArgs e) {
SetWindowTheme(this.Handle, "", "");
base.OnHandleCreated(e);
}
}
Now you can use Application.EnableVisualStyles(); and still use the old progress bar control.

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

Set active window

I'm trying to make an app that gives a quake style drop-down HUD console. I can get it to show and hide the window, but I can't figure out how to set it as the active window after showing it. Im using Win API calls to show and hide the window. I've tried SetForegroundWindow(IntPtr hWnd) and SetFocus(IntPtr hWnd) to no avail. Anyone have any ideas?
http://pastebin.com/DgtJJGiv
public void ShowApp()
{
IntPtr h = FindWindow(null, "C:\\Windows\\system32\\cmd.exe");
ShowWindow(h, SW_SHOW);
//EnableWindow(h, true);
isHidden = false;
// set focus to console window
SetForegroundWindow(h);
System.Diagnostics.Debug.WriteLine(h);
}
I found an answer here:
How to show form in front in C#
The winAPI approaches were not working correctly for me but this did:
form.TopMost = true;
form.TopMost = false;
I originally was only setting TopMost to true but I ran into problems with dialog boxes displaying behind the form. It appears that setting TopMost to true pulls the form to the front and holds it there. Setting it to false doesn't push it back but does allow other forms to be shown in front. I was still having problems with focus so I ended up going with the following:
form.Activate();
You may use SetActiveWindow winAPI method. Hope this helps...
Try this (works for me):
public static void ShowApp()
{
IntPtr h = FindWindow(null, "C:\\Windows\\system32\\cmd.exe");
ShowWindow(h, ShowWindowCommands.Show);
SetForegroundWindow(h);
SetFocus(h);
System.Diagnostics.Debug.WriteLine(h);
}
Is there any reason why you can't implement your own console window? What I mean is a simple Form with a Textbox set to the correct style. You would probably have more control over how it works than trying to use the 'cmd' process.
Just a thought.

How to make a windows form to come front in windows application done in c#?

I have done a global mouse event in my windows application. When i click the center button of my mouse, i want to make a particular form topmost...
There are some applications which runs in the full screen mode, so i need to do this, in order to make my form visible to the users, because this is the only way to view it. Since Alt + Tab is disabled. This is a Kiosk application.
I tried using Topmost = true for that particular form and
I tried using below code...But no use. I am not getting my form in front.
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
private void BringToFront(string className,string CaptionName)
{
SetForegroundWindow(FindWindow(className,CaptionName));
}
The global hotkey which has to trigger this form to bring front is working perfectly.
How to make my form come front ??? Thanks.
I solved it, doing this:
this.TopMost = true;
this.TopMost = false;
Not my thing but have you tried messing with:
this.BringToFront();
this.Activate();
get the handle of the window and do this
SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE| SWP_SHOWWINDOW);
You need set the fullscreen form's topmost to false, then continue on.
You may not be calling your BringToFront method correctly. For the FindWindow API function, lpClassName would be your application's name (e.g. "MyApplication.exe"), while lpWindowName refers to the caption in the particular form's title bar (e.g. "Form1"). Usually with FindWindow you pass in one or the other, eg:
FindWindow("MyApplication.exe", null);
// or
FindWindow(null, "Form1");
I'm not sure what happens when you pass both.
You may also just need to do something simple to achieve this, like calling the particular form's Activate() method.

MessageBox buttons - set language?

When you use MessageBox.Show() you have a selection of MessageBoxButtons to choose from. The buttons available are an enum, and give you options like "Yes No", "OK Cancel", etc.
When I am using, for instance, Norwegian message text the user still gets the English "Yes No".
Is there a way to change the text of the buttons (in C#) so that the language is correct? Can I override the text, or set the current locale in some way so that I can have "Ja Nei" instead of "Yes No"?
I do not want to rely on installing a .NET language pack at my client.
There is no native support for this in .NET (as far as I know, anyway; please correct me if I'm wrong, anyone). I did come across this CodeProject article, that seem to do the trick with some message hooking and P/Invoke:
http://www.codeproject.com/KB/miscctrl/Localizing_MessageBox.aspx
Usually messagebox buttons (as all of Windows) honor the currently set UI language for Windows. So if you've got an English installation and can't change languages (MUI versions or Ultimate for Vista/7) you're out of luck.
You could implement a messagebox yourself but I'd beg you not to. Simple things like common hotkeys for the buttons, having the ability to use Ctrl+Ins to copy the contents, etc. are the ones I miss the most when people start reinventing square wheels.
I don't think it is possible, but refer to the MSDN article MessageBox.Show Method. You may get some ideas. You can change the text in the message box. What about creating your own message box (new form) and displaying them?
You can create a panel (pnlExitMode), with property Visible=false, and put info text as well as buttons (btnYes, btnNo) labeled YES and NO (with button captions in "your" language) onto this panel, then place required Yes/No- actions into the button event handling routines.
At decision time (in my case: warning if ini file not yet written when closing application) set panel to Visible. Panel with buttons will pop up.
Example code:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (bIniModified)
{
pnlExitMode.Visible = true; pnlExitMode.BringToFront();
e.Cancel = true;
}
}
private void btnYes_Click(object sender, EventArgs e)
{
SaveToIni();
pnlExitMode.Visible = false; bIniModified = false;
Application.Exit();
}
private void btnNo_Click(object sender, EventArgs e)
{
pnlExitMode.Visible = false; bIniModified = false;
Application.Exit();
}

Categories