Click here to Skip to main content
15,891,670 members
Home / Discussions / C#
   

C#

 
QuestionDataGridView prevent edit Pin
~~~Johnny~~~6-Jun-07 2:17
~~~Johnny~~~6-Jun-07 2:17 
AnswerRe: DataGridView prevent edit Pin
I Believe In GOD6-Jun-07 2:53
I Believe In GOD6-Jun-07 2:53 
AnswerRe: DataGridView prevent edit Pin
~~~Johnny~~~6-Jun-07 9:18
~~~Johnny~~~6-Jun-07 9:18 
AnswerRe: DataGridView prevent edit Pin
ganti.r7-Jun-07 0:24
ganti.r7-Jun-07 0:24 
QuestionDomain Pin
Michael O.6-Jun-07 2:09
Michael O.6-Jun-07 2:09 
NewsRe: Domain Pin
I Believe In GOD6-Jun-07 2:24
I Believe In GOD6-Jun-07 2:24 
QuestionCreating blinking I-Beam Pin
Vertyg06-Jun-07 2:02
Vertyg06-Jun-07 2:02 
AnswerRe: Creating blinking I-Beam Pin
I Believe In GOD6-Jun-07 2:16
I Believe In GOD6-Jun-07 2:16 
Hi ....

I hope this Example will help you ...

I used 2 Files
- mycaret.cs (Class)
- caretsample.cs ( Example How to use it ) Optional

File : mycaret.cs

<br />
using System;<br />
using System.Windows.Forms;<br />
using System.Runtime.InteropServices;<br />
using System.Drawing;<br />
<br />
public class MyCaret {<br />
  [DllImport("user32.dll")]<br />
  public static extern int CreateCaret(IntPtr hwnd, IntPtr hbm, int cx, int cy);<br />
  [DllImport("user32.dll")]<br />
  public static extern int DestroyCaret();<br />
  [DllImport("user32.dll")]<br />
  public static extern int SetCaretPos(int x, int y);<br />
  [DllImport("user32.dll")]<br />
  public static extern int ShowCaret(IntPtr hwnd);<br />
  [DllImport("user32.dll")]<br />
  public static extern int HideCaret(IntPtr hwnd);<br />
<br />
  Control ctrl;<br />
  Size  size;<br />
  Point  pos;<br />
  bool  bVisible;<br />
<br />
  public MyCaret(Control ctrl) {<br />
    this.ctrl = ctrl;<br />
    Position = Point.Empty;<br />
    Size = new Size(1, ctrl.Font.Height);<br />
    Control.GotFocus += new EventHandler(OnGotFocus);<br />
    Control.LostFocus += new EventHandler(OnLostFocus);<br />
<br />
    if (ctrl.Focused)<br />
      OnGotFocus(ctrl, new EventArgs());<br />
  }<br />
<br />
  public Control Control {<br />
    get { return ctrl; }<br />
  }<br />
<br />
  public Size Size {<br />
    get { return size; }<br />
    set { size = value; }<br />
  }<br />
<br />
  public Point Position {<br />
    get { <br />
      return pos; <br />
    }<br />
    set { <br />
      pos = value; <br />
      SetCaretPos(pos.X, pos.Y);<br />
    }<br />
  }<br />
<br />
  public bool Visible {<br />
    get {<br />
      return bVisible;<br />
    }<br />
    set {<br />
      bVisible = value;<br />
      if (bVisible)<br />
        ShowCaret(Control.Handle);<br />
      else<br />
        HideCaret(Control.Handle);<br />
    }<br />
  }<br />
<br />
  public void Dispose() {<br />
    if (ctrl.Focused)<br />
      OnLostFocus(ctrl, new EventArgs());<br />
    Control.GotFocus -= new EventHandler(OnGotFocus);<br />
    Control.LostFocus -= new EventHandler(OnLostFocus);<br />
  }<br />
<br />
  private void OnGotFocus(object sender, EventArgs e) {<br />
    CreateCaret(Control.Handle, IntPtr.Zero, Size.Width, Size.Height);<br />
    SetCaretPos(Position.X, Position.Y);<br />
    Visible = true;<br />
  }<br />
<br />
  private void OnLostFocus(object sender, EventArgs e) {<br />
    Visible = false;<br />
    DestroyCaret();<br />
  }<br />
}<br />


caretsample.cs


<br />
using System;<br />
using System.Drawing;<br />
using System.ComponentModel;<br />
using System.Windows.Forms;<br />
using System.Runtime.InteropServices;<br />
<br />
public class MyForm : Form {<br />
  private Container components = null;<br />
  MyCaret caret;<br />
  string[] lines;<br />
  Point cur;<br />
  Point org;<br />
  bool bSel;<br />
<br />
  public MyForm() {<br />
    InitializeComponent();<br />
<br />
    caret = new MyCaret(this);<br />
    lines = new string[] {<br />
                 "public class MyApp {",<br />
                 "    System.Console.WriteLine(\"Hello,World\");",<br />
                 "}"};<br />
    cur = Point.Empty;<br />
    org = Point.Empty;<br />
    bSel = false;<br />
  }<br />
<br />
  protected override void Dispose( bool disposing ) {<br />
    if( disposing ) {<br />
      if (components != null) {<br />
        components.Dispose();<br />
      }<br />
    }<br />
    base.Dispose( disposing );<br />
  }<br />
<br />
  protected override void OnPaint(PaintEventArgs e) {<br />
    base.OnPaint (e);<br />
    StringFormat fmt = StringFormat.GenericTypographic;<br />
    fmt.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;<br />
    for (int i = 0; i < lines.Length; ++i)<br />
      e.Graphics.DrawString(lines[i], Font, new SolidBrush(this.ForeColor), <br />
        0, Font.Height * i, StringFormat.GenericTypographic);<br />
    if (bSel) {<br />
      DrawSelected(e.Graphics);<br />
    }<br />
  }<br />
<br />
  protected override void OnKeyDown(KeyEventArgs e) {<br />
    if (e.Shift) {<br />
      if (!bSel) {<br />
        org = cur;<br />
        bSel = true;<br />
      }<br />
    }<br />
    else {<br />
      org = cur;<br />
      bSel = false;<br />
    }<br />
<br />
    switch (e.KeyCode) {<br />
      case Keys.Left:<br />
        if (cur.X > 0) <br />
          --cur.X;<br />
        else if (cur.Y > 0) {<br />
          --cur.Y;<br />
          cur.X = lines[cur.Y].Length;<br />
        }<br />
        break;<br />
      case Keys.Right:<br />
        if (cur.X < lines[cur.Y].Length)<br />
          ++cur.X;<br />
        else if (cur.Y < lines.Length - 1) {<br />
          ++cur.Y;<br />
          cur.X = 0;<br />
        }<br />
        break;<br />
      case Keys.Up:<br />
        if (cur.Y > 0) {<br />
          --cur.Y;<br />
          if (cur.X > lines[cur.Y].Length)<br />
            cur.X = lines[cur.Y].Length;<br />
        }<br />
        break;<br />
      case Keys.Down:<br />
        if (cur.Y < lines.Length - 1) {<br />
          ++cur.Y;<br />
          if (cur.X > lines[cur.Y].Length)<br />
            cur.X = lines[cur.Y].Length;<br />
        }<br />
        break;<br />
    }<br />
    MoveCaret();<br />
    Invalidate();<br />
  }<br />
<br />
  void DrawSelected(Graphics g) {<br />
    Point bgn;<br />
    Point end;<br />
<br />
    if (org.Y < cur.Y || org.Y == cur.Y && org.X < cur.X) {<br />
      bgn = org;<br />
      end = cur;<br />
    }<br />
    else {<br />
      bgn = cur;<br />
      end = org;<br />
    }<br />
<br />
    int p;<br />
    int q;<br />
    PointF lt = PointF.Empty;<br />
    PointF rb = PointF.Empty;<br />
    for (int i = bgn.Y; i <= end.Y; ++i) {<br />
      lt.Y = i * Font.Height;<br />
      rb.Y = (i + 1) * Font.Height;<br />
<br />
      if (i == bgn.Y) { <br />
        lt.X = CalcWidth(g, lines[i].Substring(0, bgn.X));<br />
        p = bgn.X;<br />
      }<br />
      else { <br />
        lt.X = 0; <br />
        p = 0;<br />
      }<br />
      if (i == end.Y) {<br />
        rb.X = CalcWidth(g, lines[i].Substring(0, end.X));<br />
        q = end.X;<br />
      }<br />
      else {<br />
        rb.X = CalcWidth(g, lines[i]);<br />
        q = lines[i].Length;<br />
      }<br />
      g.FillRectangle(Brushes.DarkBlue, lt.X, lt.Y, rb.X - lt.X, rb.Y - lt.Y);<br />
      g.DrawString(lines[i].Substring(p, q - p), Font, new SolidBrush(Color.White), <br />
        lt.X, lt.Y, StringFormat.GenericTypographic);<br />
    }<br />
  }<br />
<br />
  float CalcWidth(Graphics g, string s) {<br />
    StringFormat fmt = StringFormat.GenericTypographic;<br />
    fmt.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;<br />
    SizeF size = g.MeasureString(s, Font, Point.Empty, fmt);<br />
    return size.Width;<br />
  }<br />
<br />
  void MoveCaret() {<br />
    using (Graphics g = Graphics.FromHwnd(Handle)) {<br />
      float w = CalcWidth(g, lines[cur.Y].Substring(0, cur.X));<br />
      caret.Position = new Point((int)w, Font.Height * cur.Y);<br />
    }<br />
  }<br />
<br />
  private void InitializeComponent() {<br />
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 12);<br />
    this.ClientSize = new System.Drawing.Size(292, 266);<br />
    this.Font = new System.Drawing.Font("MS ゴシック", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));<br />
    this.Name = "Form1";<br />
    this.Text = "Form1";<br />
<br />
  }<br />
<br />
  [STAThread]<br />
  static void Main() {<br />
    Application.Run(new MyForm());<br />
  }<br />
}<br />
<br />
<br />


have a good day ...

I know nothing , I know nothing

GeneralRe: Creating blinking I-Beam Pin
Vertyg06-Jun-07 2:20
Vertyg06-Jun-07 2:20 
GeneralRe: Creating blinking I-Beam Pin
leppie6-Jun-07 3:30
leppie6-Jun-07 3:30 
QuestionAre theses the same: object.Equal(obj,null) and obj == null? Pin
Christopher Stratmann6-Jun-07 1:31
Christopher Stratmann6-Jun-07 1:31 
AnswerRe: Are theses the same: object.Equal(obj,null) and obj == null? Pin
Guffa6-Jun-07 1:54
Guffa6-Jun-07 1:54 
GeneralRe: Are theses the same: object.Equal(obj,null) and obj == null? Pin
S. Senthil Kumar6-Jun-07 4:48
S. Senthil Kumar6-Jun-07 4:48 
GeneralRe: Are theses the same: object.Equal(obj,null) and obj == null? Pin
Guffa6-Jun-07 21:56
Guffa6-Jun-07 21:56 
QuestionBinding DataReader to DataGrid Pin
S Palip6-Jun-07 1:16
S Palip6-Jun-07 1:16 
AnswerRe: Binding DataReader to DataGrid Pin
Harini N K6-Jun-07 2:14
Harini N K6-Jun-07 2:14 
GeneralRe: Binding DataReader to DataGrid Pin
S Palip6-Jun-07 16:07
S Palip6-Jun-07 16:07 
QuestionPrint PDF via AcrobatReader and close it afterwards Pin
blackjack21506-Jun-07 1:11
blackjack21506-Jun-07 1:11 
AnswerRe: Print PDF via AcrobatReader and close it afterwards Pin
Seishin#6-Jun-07 1:50
Seishin#6-Jun-07 1:50 
GeneralRe: Print PDF via AcrobatReader and close it afterwards Pin
ScottM16-Jun-07 2:01
ScottM16-Jun-07 2:01 
GeneralRe: Print PDF via AcrobatReader and close it afterwards Pin
Seishin#6-Jun-07 2:12
Seishin#6-Jun-07 2:12 
GeneralRe: Print PDF via AcrobatReader and close it afterwards Pin
blackjack21506-Jun-07 2:01
blackjack21506-Jun-07 2:01 
GeneralRe: Print PDF via AcrobatReader and close it afterwards Pin
ScottM16-Jun-07 2:19
ScottM16-Jun-07 2:19 
GeneralRe: Print PDF via AcrobatReader and close it afterwards Pin
jethomas414-Oct-09 6:07
jethomas414-Oct-09 6:07 
GeneralRe: Print PDF via AcrobatReader and close it afterwards Pin
blackjack215015-Oct-09 1:08
blackjack215015-Oct-09 1:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.