2008-09-24 15 views
7

मैं लिख रहा हूं कि एक आवेदन के लिए एक मुफ्त Winforms घटक के लिए देख रहा हूँ। मुझे मूल रूप से एक टेक्स्टबॉक्स की आवश्यकता है जिसमें एक साइड कॉलम में रेखा संख्याएं हों। इसके भीतर डेटा सारणी करने में सक्षम होने के नाते भी एक बड़ा प्लस होगा।विंडोज फॉर्म टेक्स्टबॉक्स जिसमें लाइन नंबर हैं?

क्या किसी को प्रीपेड घटक के बारे में पता है जो यह कर सकता है?

+0

विंडोज, नहीं खिड़कियां। कृपया ठीक करें। मुझे bugging है: पी – Svish

उत्तर

6

Wayne's post संदर्भित, यहाँ प्रासंगिक कोड है। यह टेक्स्ट बॉक्स के बगल में लाइन नंबर खींचने के लिए जीडीआई का उपयोग कर रहा है।

Public Sub New() 
    MyBase.New() 

    'This call is required by the Windows Form Designer. 
    InitializeComponent() 

    'Add any initialization after the InitializeComponent() call 
    SetStyle(ControlStyles.UserPaint, True) 
    SetStyle(ControlStyles.AllPaintingInWmPaint, True) 
    SetStyle(ControlStyles.DoubleBuffer, True) 
    SetStyle(ControlStyles.ResizeRedraw, True) 
End Sub 

Private Sub RichTextBox1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.SelectionChanged 
    FindLine() 
    Invalidate() 
End Sub 

Private Sub FindLine() 
    Dim intChar As Integer 

    intChar = RichTextBox1.GetCharIndexFromPosition(New Point(0, 0)) 
    intLine = RichTextBox1.GetLineFromCharIndex(intChar) 
End Sub 

Private Sub DrawLines(ByVal g As Graphics, ByVal intLine As Integer) 
    Dim intCounter As Integer, intY As Integer 

    g.Clear(Color.Black) 

    intCounter = intLine + 1 
    intY = 2 
    Do 
     g.DrawString(intCounter.ToString(), Font, Brushes.White, 3, intY) 
     intCounter += 1 

     intY += Font.Height + 1 
     If intY > ClientRectangle.Height - 15 Then Exit Do 
    Loop 
End Sub 

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) 
    DrawLines(e.Graphics, intLine) 
End Sub 

Private Sub RichTextBox1_VScroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.VScroll 
    FindLine() 
    Invalidate() 
End Sub 

Private Sub RichTextBox1_UserScroll() Handles RichTextBox1.UserScroll 
    FindLine() 
    Invalidate() 
End Sub 

RichTextBox इस तरह ओवरराइड की गई है:

Public Class UserControl1 
Inherits System.Windows.Forms.RichTextBox 

Public Event UserScroll() 

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) 
    If m.Msg = &H115 Then 
     RaiseEvent UserScroll() 
    End If 

    MyBase.WndProc(m) 
End Sub 
End Class 

(। कोड xtremedotnettalk.com मंच पर divil द्वारा)

+0

यह सी # में भी करने योग्य है? – Svish

+0

हां, http://www.developerfusion.com/tools/convert/vb-to-csharp/ आपको शुरू करना चाहिए। यदि आप इसे काम करते हैं तो आपको इसे दूसरों के लिए संसाधन के रूप में उत्तर के रूप में पोस्ट करना चाहिए। – ine

+0

क्या यह 'शुद्ध' .NET है? –

4

SharpDevelop सी # कंपाइलर/आईडीई स्रोत कोड पर एक नज़र डालें। उनके पास लाइन नंबरों के साथ एक परिष्कृत टेक्स्ट बॉक्स है। आप स्रोत को देख सकते हैं, पता लगा सकते हैं कि वे क्या कर रहे हैं, और फिर इसे स्वयं लागू करें। alt text http://community.sharpdevelop.net/photos/mattward/images/original/FeatureTourQuickClassBrowser.aspx

2

http://www.xtremedotnettalk.com/showthread.php?s=&threadid=49661&highlight=RichTextBox पर उपलब्ध कोड के साथ एक परियोजना है:

यहाँ मैं क्या कर रहा हूँ संदर्भित का एक नमूना है।

आप साइट पर लॉग इन कर सकते हैं उपयोगकर्ता के साथ ज़िप फ़ाइल डाउनलोड करने के लिए/पारित: BugMeNot/BugMeNot

+0

बहिष्कृत उपयोगकर्ता/पास ... 'असुरक्षित' भंडारण में जाने पर विचार करें? : -/ – DigitalJedi805

+0

क्षमा करें ..मेरी परियोजना नहीं थी और जवाब 5 साल पहले था। हालांकि, ऐसा लगता है कि शीर्ष उत्तर ने प्रासंगिक कोड को पकड़ लिया और इसे ऊपर पोस्ट किया। – Wayne

2

ऐसा करने के लिए यहां कुछ सी # कोड है। यह वेन द्वारा संदर्भित xtremedotnettalk.com के कोड पर आधारित है। मैंने वास्तव में संपादक टेक्स्ट को प्रदर्शित करने के लिए कुछ बदलाव किए हैं, जो मूल ने नहीं किया था। लेकिन, निष्पक्ष होने के लिए, मूल कोड लेखक ने जरूरी काम का जिक्र किया।

यहाँ कोड (NumberedTextBox.cs)

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Text; 
using System.Windows.Forms; 

namespace NumberedTextBoxLib { 
    public partial class NumberedTextBox : UserControl { 
    private int lineIndex = 0; 

    new public String Text { 
     get { 
     return editBox.Text; 
     } 
     set { 
     editBox.Text = value; 
     } 
    } 

    public NumberedTextBox() { 
     InitializeComponent(); 
     SetStyle(ControlStyles.UserPaint, true); 
     SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
     SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
     SetStyle(ControlStyles.ResizeRedraw, true); 
     editBox.SelectionChanged += new EventHandler(selectionChanged); 
     editBox.VScroll += new EventHandler(OnVScroll); 
    } 

    private void selectionChanged(object sender, EventArgs args) { 
     FindLine(); 
     Invalidate(); 
    } 

    private void FindLine() { 
     int charIndex = editBox.GetCharIndexFromPosition(new Point(0, 0)); 
     lineIndex = editBox.GetLineFromCharIndex(charIndex); 
    } 

    private void DrawLines(Graphics g) { 
     int counter, y; 
     g.Clear(BackColor); 
     counter = lineIndex + 1; 
     y = 2; 
     int max = 0; 
     while (y < ClientRectangle.Height - 15) { 
     SizeF size = g.MeasureString(counter.ToString(), Font); 
     g.DrawString(counter.ToString(), Font, new SolidBrush(ForeColor), new Point(3, y)); 
     counter++; 
     y += (int)size.Height; 
     if (max < size.Width) { 
      max = (int) size.Width; 
     } 
     } 
     max += 6; 
     editBox.Location = new Point(max, 0); 
     editBox.Size = new Size(ClientRectangle.Width - max, ClientRectangle.Height); 
    } 

    protected override void OnPaint(PaintEventArgs e) { 
     DrawLines(e.Graphics); 
     e.Graphics.TranslateTransform(50, 0); 
     editBox.Invalidate(); 
     base.OnPaint(e); 
    } 

    ///Redraw the numbers when the editor is scrolled vertically 
    private void OnVScroll(object sender, EventArgs e) { 
     FindLine(); 
     Invalidate(); 
    } 

    } 
} 

और यहाँ दृश्य स्टूडियो डिजाइनर कोड (NumberedTextBox.Designer.cs)


namespace NumberedTextBoxLib { 
    partial class NumberedTextBox { 
    /// Required designer variable. 
    private System.ComponentModel.IContainer components = null; 

    /// Clean up any resources being used. 
    protected override void Dispose(bool disposing) { 
     if (disposing && (components != null)) { 
     components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    private void InitializeComponent() { 
     this.editBox = new System.Windows.Forms.RichTextBox(); 
     this.SuspendLayout(); 
     // 
     // editBox 
     // 
     this.editBox.AcceptsTab = true; 
     this.editBox.Anchor = ((System.Windows.Forms.AnchorStyles) ((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right))); 
     this.editBox.Location = new System.Drawing.Point(27, 3); 
     this.editBox.Name = "editBox"; 
     this.editBox.Size = new System.Drawing.Size(122, 117); 
     this.editBox.TabIndex = 0; 
     this.editBox.Text = ""; 
     this.editBox.WordWrap = false; 
     // 
     // NumberedTextBox 
     // 
     this.Controls.Add(this.editBox); 
     this.Name = "NumberedTextBox"; 
     this.Size = new System.Drawing.Size(152, 123); 
     this.ResumeLayout(false); 

    } 

    private System.Windows.Forms.RichTextBox editBox; 
    } 
} 
+0

पुराना है, लेकिन इसे हर जगह 'फ़ॉन्ट' के बजाय 'editBox.Font' का उपयोग करना चाहिए, और ऐसा करने से संख्या लेबल की स्थिति प्राप्त करना बेहतर होगा: ' y = editBox.GetPositionFromCharIndex (संपादित करेंबॉक्स.गेटफर्स्ट कैरइंडेक्सफ्रेमलाइन (काउंटर - 1)); ' –

1

मैं इसे फ़ॉन्ट आकार पर निर्भर करता है लगता है, मैं "कूरियर न्यू, 9pt, शैली = बोल्ड"

    आसान स्क्रॉलिंग, (लाइन द्वारा नहीं लाइन) के लिए
  • ठीक जोड़ा
  • इस्तेमाल किया
  • बड़ी फ़ाइलों के लिए जोड़ा गया फिक्स, जब स्क्रॉल मान 16 बिट से अधिक है।

NumberedTextBox.cs

public partial class NumberedTextBox : UserControl 
{ 
    private int _lines = 0; 

    [Browsable(true), 
     EditorAttribute("System.ComponentModel.Design.MultilineStringEditor, System.Design","System.Drawing.Design.UITypeEditor")] 
    new public String Text 
    { 
     get 
     { 
      return editBox.Text; 
     } 
     set 
     { 
      editBox.Text = value; 
      Invalidate(); 
     } 
    } 

    private Color _lineNumberColor = Color.LightSeaGreen; 

    [Browsable(true), DefaultValue(typeof(Color), "LightSeaGreen")] 
    public Color LineNumberColor { 
     get{ 
      return _lineNumberColor; 
     } 
     set 
     { 
      _lineNumberColor = value; 
      Invalidate(); 
     } 
    } 

    public NumberedTextBox() 
    { 
     InitializeComponent(); 

     SetStyle(ControlStyles.UserPaint, true); 
     SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
     SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
     SetStyle(ControlStyles.ResizeRedraw, true); 
     editBox.SelectionChanged += new EventHandler(selectionChanged); 
     editBox.VScroll += new EventHandler(OnVScroll); 
    } 

    private void selectionChanged(object sender, EventArgs args) 
    { 
     Invalidate(); 
    } 

    private void DrawLines(Graphics g) 
    { 
     g.Clear(BackColor); 
     int y = - editBox.ScrollPos.Y; 
     for (var i = 1; i < _lines + 1; i++) 
     { 
      var size = g.MeasureString(i.ToString(), Font); 
      g.DrawString(i.ToString(), Font, new SolidBrush(LineNumberColor), new Point(3, y)); 
      y += Font.Height + 2; 
     } 
     var max = (int)g.MeasureString((_lines + 1).ToString(), Font).Width + 6; 
     editBox.Location = new Point(max, 0); 
     editBox.Size = new Size(ClientRectangle.Width - max, ClientRectangle.Height); 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     _lines = editBox.Lines.Count(); 
     DrawLines(e.Graphics); 
     e.Graphics.TranslateTransform(50, 0); 
     editBox.Invalidate(); 
     base.OnPaint(e); 
    } 

    private void OnVScroll(object sender, EventArgs e) 
    { 
     Invalidate(); 
    } 

    public void Select(int start, int length) 
    { 
     editBox.Select(start, length); 
    } 

    public void ScrollToCaret() 
    { 
     editBox.ScrollToCaret(); 
    } 

    private void editBox_TextChanged(object sender, EventArgs e) 
    { 
     Invalidate(); 
    } 
} 

public class RichTextBoxEx : System.Windows.Forms.RichTextBox 
{ 
    private double _Yfactor = 1.0d; 

    [DllImport("user32.dll")] 
    static extern IntPtr SendMessage(IntPtr hWnd, Int32 wMsg, Int32 wParam, ref Point lParam); 

    private enum WindowsMessages 
    { 
     WM_USER = 0x400, 
     EM_GETSCROLLPOS = WM_USER + 221, 
     EM_SETSCROLLPOS = WM_USER + 222 
    } 

    public Point ScrollPos 
    { 
     get 
     { 
      var scrollPoint = new Point(); 
      SendMessage(this.Handle, (int)WindowsMessages.EM_GETSCROLLPOS, 0, ref scrollPoint); 
      return scrollPoint; 
     } 
     set 
     { 
      var original = value; 
      if (original.Y < 0) 
       original.Y = 0; 
      if (original.X < 0) 
       original.X = 0; 

      var factored = value; 
      factored.Y = (int)((double)original.Y * _Yfactor); 

      var result = value; 

      SendMessage(this.Handle, (int)WindowsMessages.EM_SETSCROLLPOS, 0, ref factored); 
      SendMessage(this.Handle, (int)WindowsMessages.EM_GETSCROLLPOS, 0, ref result); 

      var loopcount = 0; 
      var maxloop = 100; 
      while (result.Y != original.Y) 
      { 
       // Adjust the input. 
       if (result.Y > original.Y) 
        factored.Y -= (result.Y - original.Y)/2 - 1; 
       else if (result.Y < original.Y) 
        factored.Y += (original.Y - result.Y)/2 + 1; 

       // test the new input. 
       SendMessage(this.Handle, (int)WindowsMessages.EM_SETSCROLLPOS, 0, ref factored); 
       SendMessage(this.Handle, (int)WindowsMessages.EM_GETSCROLLPOS, 0, ref result); 

       // save new factor, test for exit. 
       loopcount++; 
       if (loopcount >= maxloop || result.Y == original.Y) 
       { 
        _Yfactor = (double)factored.Y/(double)original.Y; 
        break; 
       } 
      } 
     } 
    } 
} 

NumberedTextBox.Designer.cs

partial class NumberedTextBox 
{ 
    /// <summary> 
    /// Required designer variable. 
    /// </summary> 
    private System.ComponentModel.IContainer components = null; 

    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    #region Component Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.editBox = new WebTools.Controls.RichTextBoxEx(); 
     this.SuspendLayout(); 
     // 
     // editBox 
     // 
     this.editBox.AcceptsTab = true; 
     this.editBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
     | System.Windows.Forms.AnchorStyles.Left) 
     | System.Windows.Forms.AnchorStyles.Right))); 
     this.editBox.BorderStyle = System.Windows.Forms.BorderStyle.None; 
     this.editBox.Location = new System.Drawing.Point(27, 3); 
     this.editBox.Name = "editBox"; 
     this.editBox.ScrollPos = new System.Drawing.Point(0, 0); 
     this.editBox.Size = new System.Drawing.Size(120, 115); 
     this.editBox.TabIndex = 0; 
     this.editBox.Text = ""; 
     this.editBox.WordWrap = false; 
     this.editBox.TextChanged += new System.EventHandler(this.editBox_TextChanged); 
     // 
     // NumberedTextBox 
     // 
     this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 
     this.Controls.Add(this.editBox); 
     this.Name = "NumberedTextBox"; 
     this.Size = new System.Drawing.Size(150, 121); 
     this.ResumeLayout(false); 

    } 

    private RichTextBoxEx editBox; 

    #endregion 
} 
संबंधित मुद्दे