snaplines linha de base no controles WinForms personalizados
-
01-07-2019 - |
Pergunta
Eu tenho um controle de usuário personalizada com uma caixa de texto sobre ele e eu gostaria de expor a linha de base (do texto na caixa de texto) fora snapline do controle personalizado. Eu sei que você crie um designer (Herdado de ControlDesigner) e snaplines substituição para ter acesso aos snaplines, mas eu estou querendo saber como obter a linha de base do texto de um controle que tenho exposto pelo meu controle de usuário personalizada.
Solução
Eu só tinha uma necessidade semelhante, e eu resolvido assim:
public override IList SnapLines
{
get
{
IList snapLines = base.SnapLines;
MyControl control = Control as MyControl;
if (control == null) { return snapLines; }
IDesigner designer = TypeDescriptor.CreateDesigner(
control.textBoxValue, typeof(IDesigner));
if (designer == null) { return snapLines; }
designer.Initialize(control.textBoxValue);
using (designer)
{
ControlDesigner boxDesigner = designer as ControlDesigner;
if (boxDesigner == null) { return snapLines; }
foreach (SnapLine line in boxDesigner.SnapLines)
{
if (line.SnapLineType == SnapLineType.Baseline)
{
snapLines.Add(new SnapLine(SnapLineType.Baseline,
line.Offset + control.textBoxValue.Top,
line.Filter, line.Priority));
break;
}
}
}
return snapLines;
}
}
Desta forma, ele está realmente criando uma sub-desenhador temporária para o subcontrole, a fim de descobrir onde o "real" baseline snapline é.
Este parecia razoavelmente alto desempenho em testes, mas se perf torna-se uma preocupação (e se a caixa de texto interna não se move), então a maior parte deste código pode ser extraído para o método de inicialização.
Isso também pressupõe que a caixa de texto é um filho direto do UserControl. Se houver outros controles que afetam a disposição no caminho, em seguida, o cálculo deslocamento se torna um pouco mais complicado.
Outras dicas
Como uma atualização para a resposta do Miral .. aqui estão alguns dos "passos perdidos", para alguém novo que está olhando como fazer isso. :) O código C # acima é quase 'drop-in' pronto, com a excepção de mudar alguns dos valores de referência para o UserControl que irá ser modificado.
referências possíveis Necessário:
System.Design (@robyaw)
Usings necessário:
using System.Windows.Forms.Design;
using System.Windows.Forms.Design.Behavior;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
Em seu UserControl é necessário o seguinte atributo:
[Designer(typeof(MyCustomDesigner))]
Em seguida, você precisa de uma classe "designer" que terá o override snaplines:
private class MyCustomerDesigner : ControlDesigner {
public override IList SnapLines {
get {
/* Code from above */
IList snapLines = base.SnapLines;
// *** This will need to be modified to match your user control
MyControl control = Control as MyControl;
if (control == null) { return snapLines; }
// *** This will need to be modified to match the item in your user control
// This is the control in your UC that you want SnapLines for the entire UC
IDesigner designer = TypeDescriptor.CreateDesigner(
control.textBoxValue, typeof(IDesigner));
if (designer == null) { return snapLines; }
// *** This will need to be modified to match the item in your user control
designer.Initialize(control.textBoxValue);
using (designer)
{
ControlDesigner boxDesigner = designer as ControlDesigner;
if (boxDesigner == null) { return snapLines; }
foreach (SnapLine line in boxDesigner.SnapLines)
{
if (line.SnapLineType == SnapLineType.Baseline)
{
// *** This will need to be modified to match the item in your user control
snapLines.Add(new SnapLine(SnapLineType.Baseline,
line.Offset + control.textBoxValue.Top,
line.Filter, line.Priority));
break;
}
}
}
return snapLines;
}
}
}
}
Obrigado a todos aqueles para a ajuda. Esta foi uma pergunta difícil de engolir. O pensamento de ter uma sub-classe privada em cada UserControl não era muito palatável.
Eu vim com essa classe base para ajudar ..
[Designer(typeof(UserControlSnapLineDesigner))]
public class UserControlBase : UserControl
{
protected virtual Control SnapLineControl { get { return null; } }
private class UserControlSnapLineDesigner : ControlDesigner
{
public override IList SnapLines
{
get
{
IList snapLines = base.SnapLines;
Control targetControl = (this.Control as UserControlBase).SnapLineControl;
if (targetControl == null)
return snapLines;
using (ControlDesigner controlDesigner = TypeDescriptor.CreateDesigner(targetControl,
typeof(IDesigner)) as ControlDesigner)
{
if (controlDesigner == null)
return snapLines;
controlDesigner.Initialize(targetControl);
foreach (SnapLine line in controlDesigner.SnapLines)
{
if (line.SnapLineType == SnapLineType.Baseline)
{
snapLines.Add(new SnapLine(SnapLineType.Baseline, line.Offset + targetControl.Top,
line.Filter, line.Priority));
break;
}
}
}
return snapLines;
}
}
}
}
Em seguida, derivar sua UserControl a partir desta base:
public partial class MyControl : UserControlBase
{
protected override Control SnapLineControl
{
get
{
return txtTextBox;
}
}
...
}
Mais uma vez obrigado por este destacamento.
VB.Net Versão:
Nota: você tem que mudar a txtDescription
à caixa de texto ou outro nome controle interno que você usa. e ctlUserControl
ao seu nome usercontrol
<Designer(GetType(ctlUserControl.MyCustomDesigner))> _
Partial Public Class ctlUserControl
'...
'Your Usercontrol class specific code
'...
Class MyCustomDesigner
Inherits ControlDesigner
Public Overloads Overrides ReadOnly Property SnapLines() As IList
Get
' Code from above
Dim lines As IList = MyBase.SnapLines
' *** This will need to be modified to match your user control
Dim control__1 As ctlUserControl = TryCast(Me.Control, ctlUserControl)
If control__1 Is Nothing Then Return lines
' *** This will need to be modified to match the item in your user control
' This is the control in your UC that you want SnapLines for the entire UC
Dim designer As IDesigner = TypeDescriptor.CreateDesigner(control__1.txtDescription, GetType(IDesigner))
If designer Is Nothing Then
Return lines
End If
' *** This will need to be modified to match the item in your user control
designer.Initialize(control__1.txtDescription)
Using designer
Dim boxDesigner As ControlDesigner = TryCast(designer, ControlDesigner)
If boxDesigner Is Nothing Then
Return lines
End If
For Each line As SnapLine In boxDesigner.SnapLines
If line.SnapLineType = SnapLineType.Baseline Then
' *** This will need to be modified to match the item in your user control
lines.Add(New SnapLine(SnapLineType.Baseline, line.Offset + control__1.txtDescription.Top, line.Filter, line.Priority))
Exit For
End If
Next
End Using
Return lines
End Get
End Property
End Class
End Class
Você está no caminho certo. Você vai precisar para substituir a propriedade snaplines em sua DesignR e fazer algo como isto:
Public Overrides ReadOnly Property SnapLines() As System.Collections.IList
Get
Dim snapLinesList As ArrayList = TryCast(MyBase.SnapLines, ArrayList)
Dim offset As Integer
Dim ctrl As MyControl = TryCast(Me.Control, MyControl)
If ctrl IsNot Nothing AndAlso ctrl.TextBox1 IsNot Nothing Then
offset = ctrl.TextBox1.Bottom - 5
End If
snapLinesList.Add(New SnapLine(SnapLineType.Baseline, offset, SnapLinePriority.Medium))
Return snapLinesList
End Get
End Property
Neste exemplo, o usercontrol contém uma caixa de texto. O código adiciona um novo snapline que representa a linha de base para a caixa de texto. O importante é calcular o deslocamento corretamente.