Last active
November 10, 2022 12:59
-
-
Save TheLeftExit/d69d1ae4c8dcd21eb38aa09315cb7388 to your computer and use it in GitHub Desktop.
DevExpress.XtraEditors.HtmlContentControl as a text editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DevExpress.XtraEditors; | |
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 DXApplication39 { | |
public partial class Form1 : DevExpress.XtraEditors.XtraForm { | |
public Form1() { | |
InitializeComponent(); | |
var timer = new Timer(); | |
timer.Interval = 500; | |
timer.Tick += (sender, e) => { | |
var style = htmlContentControl1.HtmlTemplate.Styles; | |
if (style.Contains("Transparent")) { | |
htmlContentControl1.HtmlTemplate.Styles = style.Replace("Transparent", "Black"); | |
} else { | |
htmlContentControl1.HtmlTemplate.Styles = style.Replace("Black", "Transparent"); | |
} | |
}; | |
timer.Start(); | |
var text = new TextWithCaret() { Text = "SampleText", CaretPosition = 6 }; | |
htmlContentControl1.DataContext = text; | |
htmlContentControl1.KeyDown += (sender, e) => { | |
switch (e.KeyCode) { | |
case Keys.Back: | |
text.Backspace(); | |
return; | |
case Keys.Delete: | |
text.Delete(); | |
return; | |
case Keys.Left: | |
text.CaretPosition--; | |
return; | |
case Keys.Right: | |
text.CaretPosition++; | |
return; | |
case Keys.Enter: | |
text.Insert(Environment.NewLine); | |
return; | |
default: | |
var c = (char)e.KeyValue; | |
if (true || char.IsLetterOrDigit(c)) | |
text.Insert(c.ToString()); | |
return; | |
} | |
}; | |
} | |
} | |
public class MyHtmlContentControl : HtmlContentControl { | |
protected override bool IsInputKey(Keys keyData) { | |
if ((keyData & (Keys.Left | Keys.Right)) != 0) return true; | |
return base.IsInputKey(keyData); | |
} | |
} | |
public class TextWithCaret : INotifyPropertyChanged { | |
private string text; | |
private int caretPosition; | |
public string Text { | |
get => text; | |
set { | |
text = value; | |
CaretPosition = caretPosition; | |
Update(); | |
} | |
} | |
public int CaretPosition { | |
get => caretPosition; | |
set { | |
caretPosition = Math.Max(0, Math.Min(text.Length, value)); | |
Update(); | |
} | |
} | |
public string Left => text.Substring(0, caretPosition); | |
public string Right => text.Substring(caretPosition); | |
public void Backspace() { | |
if (Left.Length == 0) return; | |
text = text.Remove(caretPosition - 1, 1); | |
caretPosition--; | |
Update(); | |
} | |
public void Delete() { | |
if (Right.Length == 0) return; | |
text = text.Remove(caretPosition, 1); | |
Update(); | |
} | |
public void Insert(string s) { | |
text = text.Insert(caretPosition, s); | |
caretPosition++; | |
Update(); | |
} | |
private void Update() { | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Left")); | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Right")); | |
} | |
public event PropertyChangedEventHandler PropertyChanged; | |
} | |
} | |
/* | |
this.htmlContentControl1.HtmlTemplate.Styles = ".leftsegment{\r\n\tborder-right: 1px solid Black;\r\n\tmin-width: 1px;\r\n}"; | |
this.htmlContentControl1.HtmlTemplate.Template = "<span class=\"leftsegment\">${Left}</span><span class=\"rightsegment\">${Right}</span>"; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment