Password char for RichTextBox - c#

I am using transparent custom control based on RichTextBox. Is there are any way to have password char support like in regular TextBox control.

It is simple to do, RTB actually implements support for password entry. It just isn't exposed in the .NET wrapper. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.
using System;
using System.Windows.Forms;
class RichPassword : RichTextBox {
protected override CreateParams CreateParams {
get {
// Turn on ES_PASSWORD
var cp = base.CreateParams;
cp.Style |= 0x20;
return cp;
}
}
}

Yes there is a way. You would have to intercept key presses before they are drawn (look for PreviewKeyDown or similar events) and change them to asterisks(*). But you also need to implement logic to manage the real chars string.
Note I am in agreement with the Cody Gray comment.

string Value= "";
int richTextlent = 1;
private void richTextBox2_TextChanged(object sender, EventArgs e)
{
if(richTextBox2.Text.Length == richTextlent)
{
Value += richTextBox2.Text[0].ToString();
richTextBox2.Text = richTextBox2.Text.Remove(0, 1);
richTextBox2.Text += "*";
}
else
{
Value = "";
richTextBox2.Text = "";
}
richTextlent = richTextBox2.Text.Length + 1;
}

This has worked for me and solved the purpose.
Public Class RichPassword
Inherits RichTextBox
Private Const ES_PASSWORD = 32
Protected Overrides ReadOnly Property CreateParams As CreateParams
Get
Dim CP = MyBase.CreateParams
CP.Style = CP.Style Or 32
Return CP
End Get
End Property
End Class

Related

Selecting item in PropertyGrid causes UI flicker

I have designed a WinForms app with a several (nested) TableLayoutPanel containing background images and colors indicating status of hardware components. I added a PropertyGrid to display content of a specific object using the ComponentModel.
Example:
public class MyObject
{
public float Voltage;
[Category("Operation Voltage")]
[Description("Voltage applied")]
[DisplayName("Voltage")]
public float Vop { get => Voltage; }
}
I add the object to a PropertyGrid (the example is nonsense - only for illustration)
MyObject m = new MyObject();
m.Voltage = 1.234;
propertyGrid1.SelectedObject = m;
All works fine so far until I actively select the value in the PropertyGrid the actual value, selecting the category header has no side effect). If I do so, most TableLayoutPanel in my app start to flicker.
The problem is caused by (which I need for other purposes) this method described here:
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
Following this article here which describes the issue in my opinion I am supposed to release the "device context". Can anyone point out how to do that in case of a PropertyGrid?

WebBrowser Control disable mouse clicks

I want to show a YouTube video inside a WebBrowser control, but I want to disable all user interactions (no mouse clicks no keyboard events...).
I am catching all control's preview, mouse and keyboard events and furthermore I put some handlers to the loaded HTML document, but without any success:
void webBrowser1_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e )
{
if(webBrowser1.Document != null) {
var doc = webBrowser1.Document;
doc.Body.Style = "overflow:hidden";
doc.Click += htmlDoc_Click;
doc.MouseDown += htmlDoc_MouseDown;
doc.MouseMove += htmlDoc_MouseMove;
webBrowser1.Document.Body.Click += new HtmlElementEventHandler(htmlDoc_Click);
webBrowser1.Document.Body.MouseDown += new HtmlElementEventHandler(Document_MouseDown);
webBrowser1.Document.Body.MouseUp += new HtmlElementEventHandler(Document_MouseMove);
webBrowser1.Document.Body.MouseUp += new HtmlElementEventHandler(Document_MouseUp);
HtmlElement head = doc.GetElementsByTagName("head")[0];
HtmlElement mscript = doc.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)mscript.DomElement;
element.text = "function handleMouseEvent(e) { "
+ "var evt = (e==null ? event:e); "
+ "return true; } "
+ "document.onmousedown = handleMouseEvent; "
+ "document.onmouseup = handleMouseEvent; "
+ "document.onclick = handleMouseEvent; ";
head.AppendChild(mscript);
}
}
Also it would be fine to overlay a transparent Control "in front of" the WebBrowser control.
This is a custom control derived from a standard WinForms Panel, modified to be completely transparent but "solid" (receives the Mouse events).
The transparency is achieved using CreateParams adding an ExStyle = WS_EX_TRANSPARENT;
Also, Control.SetStyle() method is used to modify the control behaviour, adding these ControlStyles:
ControlStyles.Opaque prevents the painting of the control BackGround, so it's not managed by the System.
ControlStyles.SupportsTransparentBackColor allows the control to accept Alpha values for it's background color.
ControlStyles.ResizeRedraw causes the redrawing of the control when it's resized.
The Custom Control is initialized passing a reference of the control it has to overlay. It then resizes itself to the size of this referenced control, excluding the ScrollBars from this measure, so they can be used.
To set it to work, create a reference to the OverlayPanel class and call the helper method CreateOverlay(Control control):
private OverlayPanel overlayPanel;
private void CreateOverlay(Control control)
{
overlayPanel = new OverlayPanel(this.webBrowser1);
Controls.Add(overlayPanel);
overlayPanel.BringToFront();
}
The OverlayPanel class code can be inserted in a Form or in a class file of its own. It should be created when all controls in a Form already have their dimensions set: in the Form.Shown() event or any other time when the parent form is visible. The Form.Load() event might also well work most of the time, anyway.
As a note, this OverlayPanel doesn't have a Resize method at this moment, which is required if the overlayed control is resized at some point. But it's quite a simple implementation, should it be needed.
private class OverlayPanel : Panel
{
internal int WS_EX_TRANSPARENT = 0x00000020;
public OverlayPanel(Control RefControl)
{
InitializeComponent();
this.Size = new Size(RefControl.Size.Width - SystemInformation.VerticalScrollBarWidth,
RefControl.Size.Height - SystemInformation.HorizontalScrollBarHeight);
this.Location = RefControl.Location;
}
private void InitializeComponent()
{
this.SetStyle(ControlStyles.Opaque |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
this.BorderStyle = BorderStyle.None;
}
protected override CreateParams CreateParams
{
get
{
CreateParams parameters = base.CreateParams;
parameters.ExStyle |= WS_EX_TRANSPARENT;
return parameters;
}
}
}

Flickering with transparent panel

I've created a simple program and now i'm in the stages of doing my designing. I've got a multiple Panels which i make visible / invisible to switch between "tabs" (EG. 1 panel for the login screen and 1 panel for the create account screen). Now i've made these panels invisible because i want them just as containers to be able to quickly move around controls and create buttons in.
My problem is that i've set my forms background image to a image i made in photoshop and whenever i switch between panels it flickers, whenever i just use a system color (white,black) this doesn't happen.
Is there any way for me to remove the flickering?
i've tried :
Setting double buffer to true
protected overrideing OnPaint, CreateBackground, and Createparam
my code is extremely basic :
private void btnNewAcc_Click(object sender, EventArgs e)
{
PanelNewAccount.Visible = true;
PanelLogin.Visible = false;
}
Try to setting the form property DoubleBuffered to true, in winforms the flickering usually happens because the GDI+ is trying to draw the control(s) a lot of times so DoubleBuffering you graphics should help in such cases
form.DoubleBuffered = true;
Thanks to Patrick i've solved my problem,
instead of using panels i'm using a TabControl and i assigned the same background to each tab.
Just as easy to add dynamic buttons as well.
Same features as panels but without the flickering.
#region .. Double Buffered function ..
public static void SetDoubleBuffered(System.Windows.Forms.Control c)
{
if (System.Windows.Forms.SystemInformation.TerminalServerSession)
return;
System.Reflection.PropertyInfo aProp = typeof(System.Windows.Forms.Control).GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
aProp.SetValue(c, true, null);
}
#endregion
#region .. code for Flucuring ..
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
#endregion
Even though i am late but if somebody else is also suffering from the same problem then this code fixed the flickering for me even though i dont know how it works.
I found it here.
Add the above code snippet in your program and in the contructor of your app add this line:
SetDoubleBuffered(YourPanelName);
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
code copy from codeproject solved my problem.

CreateParams Confusion

I am creating a class to simplify designing controls. It's called ControlDrawer and has three private fields:
a Point named _location
a Bitmap named _innerImage
a NativeWindow named _window
I have most of the class done, but here's my snagging point (note, ControlDrawer implements IWin32Window):
IntPtr Handle
{
get
{
CreateParams cp = new CreateParams();
cp.X = _location.X;
cp.Y = _location.Y;
// Code to make it so CreateParams says to display _innerImage
_window.CreateHandle(cp);
return _window.Handle;
}
}
The problem is, I have no idea how to fill in the commented part. Can someone help?

How to make a window have taskbar text but no title bar

How can I make my window not have a title bar but appear in the task bar with some descriptive text?
If you set the Form's .Text property then .net gives it a title bar, which I don't want.
this.ControlBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ShowInTaskbar = true;
this.Text = "My title for task bar";
I've found a partial solution, to override CreateParams:
protected override System.Windows.Forms.CreateParams CreateParams
{
get
{
System.Windows.Forms.CreateParams cp = base.CreateParams;
cp.Style &= ~0x00C00000; // WS_CAPTION
return cp;
}
}
However this causes my window to be resized as if they have a title bar, ie it's taller than it should be. Is there any good solution to this?
In my case I have a Form with FormBorderStyle = FormBorderStyle.SizableToolWindow and the following CreateParams override did the trick (i.e. I now have a form without caption and without additional margin for the title, but it keeps the title in the task bar):
protected override System.Windows.Forms.CreateParams CreateParams
{
get
{
var parms = base.CreateParams;
parms.Style &= ~0x00C00000; // remove WS_CAPTION
parms.Style |= 0x00040000; // include WS_SIZEBOX
return parms;
}
}
One approach to look into might be to set the FormBorderStyle property of your Form to None (instead of FixedDialog).
The drawback to this approach is that you lose the borders of your window as well as the Titlebar. A result of this is that you lose the form repositioning/resizing logic that you normally get "for free" with Windows Forms; you would need to deal with this by implementing your own form move/resize logic in the form's MouseDown and MouseMove event handlers.
I would also be interested to hear about better solutions.
Just set the border style to None.
this.FormBorderStyle = FormBorderStyle.None;
Once you have removed the borders with the FormBorderStyle, as mentioned already, you can make it draggable fairly easily. I describe this at http://www.blackwasp.co.uk/DraggableBorderless.aspx.

Categories