vb@rchiv
VB Classic
VB.NET
ADO.NET
VBA
C#
sevDataGrid - Gönnen Sie Ihrem SQL-Kommando diesen krönenden Abschluß!  
 vb@rchiv Quick-Search: Suche startenErweiterte Suche starten   Impressum  | Datenschutz  | vb@rchiv CD Vol.6  | Shop Copyright ©2000-2025
 
zurück

 Sie sind aktuell nicht angemeldet.Funktionen: Einloggen  |  Neu registrieren  |  Suchen

VB.NET - Fortgeschrittene
Re: "bessere" Formulierung 
Autor: ModeratorDaveS (Moderator)
Datum: 29.09.06 13:41

Schon wieder Sudoku? Warum zeichnest du nicht einen Grid mit GDI (wie unten)? Ist nicht sehr schwierig. Aber wie ich schon gesagt habe, label.Parent wird dein UserControl sein.

Hier ist ein Sudoku Control komplett, falls das interessant sein könnte. (Ich schreibe in Wirklichkeit C#). (Und wenn das doch nichts mit Sudoku zu tun hat dann kann es auch mal interessant sein

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
 
using System.Diagnostics;
 
namespace DSNTS.Sudoku
{
  /// <summary>
  /// Sudoku Control provides a visual display of a 9*9 Sudoku puzzle.
  /// </summary>
  [DefaultEvent("CellChanged")]
  [DefaultProperty("CellSize")]
  public class SudokuControl : Control, ISupportInitialize
  {
    #region Variable, constructors
 
    // CellChanged Delegate and Event
    public delegate void CellChangedHandler(object sender, _
      SudokuCellChangedEventArgs e);
    public event CellChangedHandler CellChanged;
 
    // GetValidCellValues Delegate and Event
    public delegate void GetValidCellValuesHandler(object sender, _
      SudokuGetCellValuesEventArgs e);
    public event GetValidCellValuesHandler GetValidCellValues;
 
    // UserValueChanged Delegate and Event
    public delegate void UserValueChangedHandler(object sender, _
      SudokuUserValueChangedEventArgs e);
    public event UserValueChangedHandler UserValueChanged;
 
    // GetUserCellValues Delegate and Event
    public delegate void GetUserCellValuesHandler(object sender, _
      SudokuGetCellValuesEventArgs e);
    public event GetUserCellValuesHandler GetUserCellValues;
 
    // Option to cause min cells to be drawn according to their number
    // rather than in next free space
    private bool _spaceMinicells = false;
 
    private bool[] _validValues = {false, false, false, false, false, false, _
      false, false, false};
    private bool _showValidValues = false;
 
    private bool[] _userValues = {false, false, false, false, false, false, _
      false, false, false};
    private bool _showUserValues = false;
 
    private Color[,] _cellColors = new Color[9,9];
 
    private int _cellSize = 30;
    private bool _inInit = false;
    private Point _focusCell = new Point(-1, -1);
 
    // Error cells, if cell in error != 0
    private int[,] _errors = null;
 
    // Sudoku current values : 0 = empty cell, 1 - 9 = set cell
    private int[,] _sudoku = null;
 
    // Pregiven values : 0 = empty cell, 1 - 9 = set cell
    private int[,] _pregiven = null;
 
    private Color _pregivenCellColor = Color.Green;
    private Color _errorCellColor = Color.Red;
 
    // Bitmap for background grid
    private Bitmap _grid = null;
 
    private bool _readOnly = false;
    private bool _showFocusRectangle = true;
 
    // Constructor
    public SudokuControl() : base()
    {
      SetStyle( _
        ControlStyles.UserPaint|ControlStyles.AllPaintingInWmPaint|ControlStyle_
        s.DoubleBuffer|ControlStyles.UserMouse, true);
      _errors = new int[9,9];
      _sudoku = new int[9,9];
      _pregiven = new int[9,9];
 
      // Create background grid bitmap
      createGrid();
 
      // Init cell background colors
      ClearColors();
    }
 
    #endregion
 
    #region Properties
 
    // Sudoku Array property (read only)
    [Browsable(false)]
    public int[,] Sudoku
    {
      get{ return (int[,])_sudoku.Clone(); } 
    }
 
    // Pregiven Array property (read only)
    [Browsable(false)]
    public int[,] Pregiven
    {
      get{ return (int[,])_pregiven.Clone(); } 
    }
 
    // Errors Array property (read only)
    [Browsable(false)]
    public int[,] Errors
    {
      get{ return (int[,])_errors.Clone(); } 
    }
 
    // CellSize Property
    [DefaultValue(30)]
    public int CellSize
    {
      get{ return _cellSize; } 
      set
      {
        if ((value<20)|(value>60))
        throw new System.ArgumentException("CellSize invalid", "CellSize");
        _cellSize = value;
        createGrid();
        doRefresh();
      }
    }
 
    // Preferred Size Property
    [Browsable(false)]
    public int PreferredSize
    {
      get
      {
        return _cellSize*9+4*2+6; 
      }
    }
 
    // Pregiven Cell Color Property
    [DefaultValue(typeof(Color), "Green")]
    public Color PregivenCellColor
    {
      get { return _pregivenCellColor; }
      set
      {
        _pregivenCellColor = value;
        doRefresh();
      }
    }
 
    // Error Cell Color Property
    [DefaultValue(typeof(Color), "Red")]
    public Color ErrorCellColor
    {
      get { return _errorCellColor; }
      set
      {
        _errorCellColor = value;
        doRefresh();
      }
    }
 
    // Show Valid Values Option Property
    [DefaultValue(false)]
    public bool ShowValidValues
    {
      get { return _showValidValues; }
      set
      {
        if (value)
          _showUserValues = false;
        _showValidValues = value;
         doRefresh();
      }
    }
 
    // Show Valid Values Option Property
    [DefaultValue(false)]
    public bool ShowUserValues
    {
      get { return _showUserValues; }
      set
      {
        if (value)
          _showValidValues = false;
        _showUserValues = value;
        doRefresh();
      }
    }
 
    // Space mincells Option Property
    [DefaultValue(false)]
    public bool SpaceMinicells
    {
      get { return _spaceMinicells; }
      set
      {
        _spaceMinicells = value;
        doRefresh();
      }
    }
 
    // ReadOnly property (suppress cell changes when true)
    [DefaultValue(false)]
    public bool ReadOnly
    {
      get { return _readOnly; }
      set
      {
        _readOnly = value;
      }
    }
 
    // ShowFocusRectangle property 
    // (only show focus rectangle when true)
    [DefaultValue(true)]
    public bool ShowFocusRectangle
    {
      get { return _showFocusRectangle; }
      set
      {
        _showFocusRectangle = value;
      }
    }
 
    #endregion
 
    #region Public methods
 
    // Set sudoku cell
    public void SetCell(int y, int x, int val)
    {
      if ((x>=0)&&(x<=9)&&(y>=0)&&(y<=9)&&(val>=0)&&(val<=9))
      {
        _sudoku[y,x] = val;
        _errors[y,x] = 0;
        this.Invalidate(getCellRectangle(y,x));
        doUpdate();
      }
    }
 
    // Set focus cell
    public void SetFocusCell(int y, int x)
    {
      if ((x>=0)&&(x<=9)&&(y>=0)&&(y<=9))
      {
        _focusCell = new Point(x, y);
        this.Invalidate(getCellRectangle(y,x));
        doUpdate();
      }
    }
 
    // Set sudoku cell color
    public void SetCellColor(int y, int x, Color val)
    {
      if ((x>=0)&&(x<=9)&&(y>=0)&&(y<=9))
      {
        _cellColors[y,x] = val;
        this.Invalidate(getCellRectangle(y,x));
        doUpdate();
      }
    }
 
    // Set pregiven cell
    public void SetPregivenCell(int y, int x, int val)
    {
      if ((x>=0)&&(x<=9)&&(y>=0)&&(y<=9)&&(val>=0)&&(val<=9))
      {
        _sudoku[y,x] = val;
        _pregiven[y,x] = val;
      }
    }
 
    // Set error cell
    public void SetErrorCell(int y, int x, int val)
    {
      if ((x>=0)&&(x<=9)&&(y>=0)&&(y<=9))
      {
        _errors[y,x] = val;
        this.Invalidate(getCellRectangle(y,x));
        doUpdate();
      }
    }
 
    // Reset sudoku to pregiven and clear error cells
    public void Reset()
    {
      for (int y = 0; y<9; y++)
        for (int x = 0; x<9; x++)
        {
          _sudoku[y,x] = _pregiven[y,x];
          _errors[y,x] = 0;
        }
      doRefresh();
    }
 
    // Clear all cells (sudoku, errors, pregiven) and reset all option
    public void Clear()
    {
      for (int y = 0; y<9; y++)
      {
        for (int x = 0; x<9; x++)
        {
          _sudoku[y,x] = 0;
          _pregiven[y,x] = 0;
          _errors[y,x] = 0;
        }
      }
      _showValidValues = false;
      _showUserValues = false;
      _spaceMinicells = false;
      _readOnly = false;
      _showFocusRectangle = false;
      ClearColors();
      _focusCell = new Point(-1, -1);
      doRefresh();
    }
 
    // Clear all cell colors
    public void ClearColors()
    {
      for (int y = 0; y<9; y++)
        for (int x = 0; x<9; x++)
          _cellColors[y,x] = this.BackColor;
      doRefresh();
    }
 
    // Clear error cells
    public void ClearErrors()
    {
      for (int y = 0; y<9; y++)
        for (int x = 0; x<9; x++)
        {
          _errors[y,x] = 0;
        }
      doRefresh();
    }
 
    // Get sudoku cell
    public int GetCell(int y, int x)
    {
      if ((x>=0)&&(x<=9)&&(y>=0)&&(y<=9))
        return _sudoku[y,x];
      return 0;
    }
 
    // Get pregiven cell
    public int GetPregivenCell(int y, int x)
    {
      if ((x>=0)&&(x<=9)&&(y>=0)&&(y<=9))
        return _pregiven[y,x];
      return 0;
    }
 
    // Get error cell
    public int GetErrorCell(int y, int x)
    {
      if ((x>=0)&&(x<=9)&&(y>=0)&&(y<=9))
        return _errors[y,x];
      return 0;
    }
 
    // Flash a particular cell
    public void FlashCell(int y, int x, Color flashColor, int count, int _
      msDelay)
    {
      // Sanity check
      if ((x>9)||(x<0)||(y>9)||(y<0))
        return;
 
      Rectangle r = getCellRectangle(y, x);
 
      // Paint background 2*count times in a loop with 
      // alternating colours.
      Graphics g = this.CreateGraphics();
      Brush bc = new SolidBrush(flashColor);
      Brush bb = new SolidBrush(this.BackColor);
 
      for (int i=0; i<count; i++)
      {
        g.FillRectangle(bc, r);
        System.Threading.Thread.Sleep(msDelay);
        g.FillRectangle(bb, r);
        System.Threading.Thread.Sleep(msDelay);
      }
      g.Dispose();
      bc.Dispose();
      bb.Dispose();
      this.Refresh();
    } //FlashCell()
 
    #endregion
 
    #region Base class overrides
 
    // Paint the controls window
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
      //Debug.WriteLine("Paint clipping: " +e.ClipRectangle.ToString());
      Brush bn = null;
      Brush bp = null;
      Brush be = null;
      Brush bc = null;
      Font mf = null;
      Pen lp = null;
 
      try
      {
 
        // Draw background grid from bitmap if whole control window invalidated
        if (e.ClipRectangle.Width!=_cellSize)
          e.Graphics.DrawImage(_grid, 0, 0);
 
        // Draw the characters in cells...
        // ...but don't bother if in designer
        if (!DesignMode)
        {
          int hloc = 0;
          int vloc = 0;
 
          // Create pens and brushes
          bn = new SolidBrush(this.ForeColor);
          bp = new SolidBrush(this._pregivenCellColor);
          be = new SolidBrush(this._errorCellColor);
 
          lp = new Pen(this.ForeColor);
          lp.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
 
          // Get minifont if required
          Debug.WriteLine("Font size "+this.Font.Size.ToString());
          if (_showValidValues||_showUserValues)
            mf = new Font(this.Font.FontFamily, this.Font.Size/3, _
              this.Font.Unit);
 
          // Create string format object to center digit in cell
          StringFormat sf = new StringFormat();
          sf.Alignment = StringAlignment.Center;
          sf.LineAlignment = StringAlignment.Center;
 
          for (int y = 0; y<9; y++)
          {
            hloc = 0;
            int adjy = (y%3)==0?2:1;
            for (int x=0; x<9; x++)
            {
              int adjx = (x%3)==0?2:1;
              int cv = _sudoku[y,x];          
 
              // Calculate cell rectangle (without borders)
              RectangleF r = new RectangleF(hloc+adjx, vloc+adjy, _cellSize, _
                _cellSize);
 
              // Paint background color if set for cell
              if (_cellColors[y,x]!=this.BackColor)
              {
                // Maybe cache brushes?
                bc = new SolidBrush(_cellColors[y,x]);
                e.Graphics.FillRectangle(bc, r);
              }
 
              // Draw focus rectangle if this is the focus cell
              if ( _
                _showFocusRectangle&&this.Focused&&x==_focusCell.X&&y==_focusCe_
                ll.Y)
              {
                //Debug.WriteLine("RF: "+r.ToString());
                Rectangle rl = new Rectangle((int)Math.Ceiling(r.X), ( _
                  int)Math.Ceiling(r.Y), (int)Math.Floor(r.Width)-1, ( _
                  int)Math.Floor(r.Height)-1);
                //Debug.WriteLine("RL: "+rl.ToString());
                rl.Inflate(-1, -1);
                //Debug.WriteLine("RL1: "+rl.ToString());
                e.Graphics.DrawRectangle(lp, rl);
              }
 
              if (cv!=0)
              {
                // Put number in cell
                string c = cv.ToString();
                if (_errors[y,x]!=0)
                  // Error value
                  e.Graphics.DrawString(c, this.Font, be, r, sf); 
                else
                {
                  if (_pregiven[y,x]!=0)
                    // Pregiven value
                    e.Graphics.DrawString(c, this.Font, bp, r, sf); 
                  else
                    // Set value
                    e.Graphics.DrawString(c, this.Font, bn, r, sf); 
                }
              } // if (cv!=0)
              else
              {
                // If option set show valid or user values in mini cells
                if (_showValidValues||_showUserValues)
                {
                  bool[] values = null;
 
                  // Get valid values for current cell
                  if (_showValidValues)
                  {
                    for (int mci = 0; mci<9; mci++)
                      _validValues[mci] = false;
                    OnGetValidCellValues(new SudokuGetCellValuesEventArgs(y, x, _
                      _validValues));
                    values = _validValues;
                  }
                  else
                  {
                    OnGetUserCellValues(new SudokuGetCellValuesEventArgs(y, x, _
                      _userValues));
                    values = _userValues;
                  }
 
                  // Draw the Minicells
                  if (_spaceMinicells)
                  {
                    // Position valid values in cells according to their value
                    float minicellSize = _cellSize/3;
                    int mci = 0; 
                    for (int ym = 0; ym<3; ym++)
                      for (int xm = 0; xm<3; xm++)
                      {
                        if (values[mci])
                        {
                          // Calculate minicell rectangle
                          RectangleF rm = new RectangleF(r.X+minicellSize*xm, _
                            r.Y+minicellSize*ym, minicellSize, minicellSize);
                          // Put number in cell
                          string c = (mci+1).ToString();
                          e.Graphics.DrawString(c, mf, bn, rm, sf);           
                        }
                        mci += 1;
                      }
 
                  } // if (_spaceMinicells)
                  else
                  {
                    // Position valid value in next free minicell position
                    float minicellSize = _cellSize/3;
                    int xm = 0;
                    int ym = 0;
                    for (int mci = 0; mci<9; mci++)
                    {
                      if (values[mci])
                      {
                        // Calculate minicell rectangle
                        RectangleF rm = new RectangleF(r.X+minicellSize*xm, _
                          r.Y+minicellSize*ym, minicellSize, minicellSize);
                        // Put number in cell
                        string c = (mci+1).ToString();
                        e.Graphics.DrawString(c, mf, bn, rm, sf);           
                        xm += 1;
                        if (xm==3)
                        {
                          ym += 1;
                          xm = 0;
                        }
                      }
                    } // for (int mci = 0; mci<9; mci++)
                  } // if (_spaceMinicells) else
                } // if (_showValidValues||_showUserValues)
              } // if (cv!=0) else 
              hloc += _cellSize+adjx;
            } //for (int x=0; x<9; x++)
            vloc += _cellSize+adjy;
          } //for (int y = 0; y<9; y++)
 
        } // if(!DesignMode)
      }
      catch(System.Exception ex)
      {
        Debug.WriteLine("OnPaint "+ex.ToString());
      }
      finally
      {
        if (mf!=null) mf.Dispose();
        if (lp!=null) lp.Dispose();
        if (bn!=null) bn.Dispose();
        if (bp!=null) bp.Dispose();
        if (be!=null) be.Dispose();
        if (bc!=null) bc.Dispose();
      }
    } // OnPaint()
 
    // Detect Mouse Up to change value in cell +1, -1
    protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
    {
      if (_readOnly)
        return;
 
      // Ext if mouse not over control window
      Point cmp = this.PointToClient(Control.MousePosition);
      if (!this.ClientRectangle.Contains(cmp))
        return;
 
      // Find x-cell, subcell
      int loc = 0; // Cell x
      int cx = 0; // Cell y
      int scx = 0; // Subcell x
      int scy = 0; // Subcell y
 
      int locx = 0;
      for (int i = 0; i<9; i++)
      {
        int adjx = (i%3)==0?2:1;
        if ((e.X>loc)&&(e.X<(loc+1+_cellSize)))
        {
          locx = loc+adjx;
          cx = i;
          scx = (e.X-loc)/(_cellSize/3);
          break;
        }
        loc += adjx+_cellSize;
      }
      if (locx==0) return;
 
      // Find y-cell, subcell
      loc = 0;
      int cy = 0;
      int locy = 0;
      for (int i = 0; i<9; i++)
      {
        int adjy = (i%3)==0?2:1;
        if ((e.Y>loc)&&(e.Y<(loc+1+_cellSize)))
        {
          locy = loc+adjy;
          cy = i;
          scy = (e.Y-loc)/(_cellSize/3);
          break;
        }
        loc += adjy+_cellSize;
      }
      if (locy==0) return;
 
      if (_pregiven[cy,cx]!=0)
        return;
 
      // Just set cell focus if first click in cell
      if (cx!=_focusCell.X||cy!=_focusCell.Y)
      {
        _focusCell = new Point(cx, cy);
        doRefresh();
        return;
      }
 
      if ((Control.ModifierKeys&Keys.Shift)==Keys.Shift)
      {
        // Subcell user value request
        Debug.WriteLine("e.X: "+e.X.ToString()+" e.Y: "+e.Y.ToString());
        Debug.WriteLine("cx: "+cx.ToString()+" cy: "+cy.ToString());
        Debug.WriteLine("scx: "+scx.ToString()+" scy: "+scy.ToString());
        OnUserCellChanged(new SudokuUserValueChangedEventArgs(cy, cx, _
          3*scy+scx));
      }
      else
      {
        // Cell value change
 
        // Get next value up or down
        int newValue = _sudoku[cy, cx];
        if (newValue<0) return;
 
        bool changeDone = false;
        SudokuCellChangedEventArgs sccea = null;
 
        // We keep tryng till we reach zero or 
        // event routine accepts the value
        while (!changeDone)
        {
          if (e.Button==MouseButtons.Left)
          {
            newValue += 1;
            if (newValue>9)
              newValue = 0;
          }
          else
          {
            newValue -= 1;
            if (newValue<0)
              newValue = 9;
          }
 
          sccea = new SudokuCellChangedEventArgs(cy, cx, _sudoku[cy,cx], _
            newValue);
 
          // Invoke CellChanged Event
          OnCellChanged(sccea);
 
          // Option to supress change, newValue = -1
          changeDone = sccea.NewValue>-1; 
        }
 
        // Save returned value
        newValue = sccea.NewValue;
 
        // Otherwise just sanity check
        if ((newValue>9)||(newValue<0))
          newValue = 0;
 
        //Debug.WriteLine("Set newValue: " +newValue.ToString()+" at " + _
          cx.ToString()+"/"+cy.ToString());
 
        _sudoku[cy,cx] = newValue;
        _errors[cy,cx] = 0;
 
      } // if (Control.ModifierKeys&Keys.Shift)==Keys.Shift)
 
      if (_showValidValues)
      {
        // Refresh all cells if cell value changed
        doRefresh();
      }
      else
      {
        // Invalidate just cell that was changed
        Rectangle clip = new Rectangle(locx, locy, _cellSize, _cellSize);
        //Debug.WriteLine("Clip rect: "+clip.ToString());
        this.Invalidate(clip, false);
        doUpdate();
      } // if (_showValidValues)
    } // OnMOuseUp()
 
    // Handle keyboard navigation with Arrow keys
    protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
    {
      //Debug.WriteLine("KeyDown:"+((int)e.KeyData).ToString()+" in cell:" & _
        ""+_focusCell.ToString());
      if (!_showFocusRectangle)
        return;
 
      bool changed = false;
      bool gotNext = false;
      int pos = _focusCell.Y*9+_focusCell.X;
      Point newPos = new Point(0, 0);
 
      // We check for a pregiven cell and skip over it
      // when moving focus rectangle.
      while (!gotNext)
      {
        switch(e.KeyData)
        {
          case Keys.Right:
            pos += 1;
            if (pos==81)
              pos = 0;
            changed = true;
            break;
          case Keys.Left:
            pos -= 1;
            if (pos==-1)
              pos = 80;
            changed = true;
            break;
          case Keys.Up:
            pos -= 9;
            if (pos<0)
              pos = (pos)%9+80;
            changed = true;
            break;
          case Keys.Down:
            pos  += 9;
            if (pos>80)
              pos = (pos-80)%9;
            changed = true;
            break;
        } //while (!gotNext)
        if (changed)
        {
          newPos = new Point(pos%9, pos/9);
          gotNext = _pregiven[newPos.Y, newPos.X]==0;
        }
        else
          gotNext = true;
      }
      if (changed)
      {
        _focusCell = newPos;
        //Debug.WriteLine("focusCell: "+_focusCell.ToString());
        doRefresh();
      }
      else
        base.OnKeyDown(e);
    } // OnKeyDown()
 
    // Handle keyboard input (1-9, 0, space)
    protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
    {
      if (_readOnly)
        return;
 
      if ((e.KeyChar>='0'&&e.KeyChar<='9')||e.KeyChar==' ')
      {
        int newValue = 0;
        if (e.KeyChar!=' ')
          newValue = (int)e.KeyChar-(int)'0';
 
        SudokuCellChangedEventArgs sccea = 
          new SudokuCellChangedEventArgs(_focusCell.Y, _focusCell.X, _
            _sudoku[_focusCell.X,_focusCell.Y], newValue);
 
        // Invoke CellChanged Event
        OnCellChanged(sccea);
 
        // Option to supress change, newValue = -1
        if (sccea.NewValue>-1)
        {
          _sudoku[_focusCell.Y, _focusCell.X] = newValue;  
          if (_showValidValues)
          {
            // Refresh all cells if cell value changed
            doRefresh();
          }
          else
          {
            // Invalidate just cell that was changed
            Rectangle clip = getCellRectangle(_focusCell.Y, _focusCell.X);
            //Debug.WriteLine("Clip rect: "+clip.ToString());
            this.Invalidate(clip, false);
            doUpdate();
          }
        }
        e.Handled = true;
      } // if ((e.KeyChar>='0'&&e.KeyChar<='9')||e.KeyChar==' ')
      else
        base.OnKeyPress(e);
 
      //Debug.WriteLine("KeyPress:"+((int)e.KeyChar).ToString()+" in cell:" & _
        ""+_focusCell.ToString());    
    } // OnKeyPress()
 
    // Allow us to deal with Arrow navigation keys
    protected override bool IsInputKey(Keys keyData)
    {
      //Debug.WriteLine("IsInputKey: "+keyData.ToString());
      if (keyData==Keys.Right||( _
        keyData==Keys.Left||keyData==Keys.Up||keyData==Keys.Down))
        return true;
      return base.IsInputKey (keyData);
    }
 
    // Refresh on Got Focus for focus rectangle
    protected override void OnGotFocus(EventArgs e)
    {
      if (!_showFocusRectangle)
        return;
 
      if (_focusCell.X>-1)
        doRefresh();
      base.OnGotFocus(e);
    } // OnGotFocus()
 
    // Refresh on Lost Focus for focus rectangle
    protected override void OnLostFocus(EventArgs e)
    {
      if (!_showFocusRectangle)
        return;
 
      if (_focusCell.X>-1)
        doRefresh();
      base.OnLostFocus(e);
    } // OnLostFocus()
 
    #endregion
 
    #region Our Event routines
    // Override for CellChanged event
    protected virtual void OnCellChanged(SudokuCellChangedEventArgs e)
    {
      if (CellChanged!=null) 
        CellChanged(this, e);
    }
 
    // Override for CellChanged event
    protected virtual void OnGetValidCellValues(SudokuGetCellValuesEventArgs e)
    {
      if (GetValidCellValues!=null) 
        GetValidCellValues(this, e);
    }
 
    // Override for CellChanged event
    protected virtual void OnUserCellChanged(SudokuUserValueChangedEventArgs e)
    {
      if (UserValueChanged!=null) 
        UserValueChanged(this, e);
    }
 
    // Override for CellChanged event
    protected virtual void OnGetUserCellValues(SudokuGetCellValuesEventArgs e)
    {
      if (GetUserCellValues!=null) 
        GetUserCellValues(this, e);
    }
 
    #endregion
 
    #region local functions
 
    // Create background grid bitmap
 
    private int _side = 0; 
 
    private void createGrid()
    {
      // Note: sidex and sidey should always be equal
      int side = 14+9*_cellSize;
 
      // If no size change, don't recreate bitmap
      if (_grid!=null&&side==_side)
        return;
      _side = side;
 
      if (_grid!=null)
        _grid.Dispose();
 
      _grid = new Bitmap(side, side);
      Graphics g = Graphics.FromImage(_grid);
      drawGrid(g, side);
    }
 
    private void drawGrid(Graphics g, int side)
    {
      // Narrow and wide pens for borders
      Pen np = new Pen(this.ForeColor, 1);
      Pen wp = new Pen(this.ForeColor, 2);
 
      // Draw vertical grid lines
      int hloc = 0;
      for (int i = 0; i<10; i++)
      {
        if ((i%3)==0)
        {
          // Thick line
          g.DrawLine(wp, hloc+1, 0, hloc+1, side);
          hloc += 2+_cellSize;
        }
        else
        {
          // Thin line
          g.DrawLine(np, hloc, 0, hloc, side);
          hloc += 1+_cellSize;
        }
      }
 
      // Draw horizontal grid lines
      int vloc = 0;
      for (int i = 0; i<10; i++)
      {
        if ((i%3)==0)
        {
          // Thick line
          g.DrawLine(wp, 0, vloc+1, side, vloc+1);
          vloc += 2+_cellSize;
        }
        else
        {
          // Thin line
          g.DrawLine(np, 0, vloc, side, vloc);
          vloc += 1+_cellSize;
        }
      }
 
      np.Dispose();
      wp.Dispose();
    }
 
    // Calculate display rectangle for cell at y, x
    private Rectangle getCellRectangle(int y, int x)
    {
      Rectangle r = new Rectangle(
        x+2+(int)Math.Floor(x/3)+x*_cellSize,
        y+2+(int)Math.Floor(y/3)+y*_cellSize,
        _cellSize,
        _cellSize);
      return r;
    }
 
    // Common Refresh
    protected void doRefresh()
    {
      if (!_inInit)
        this.Refresh();
    }
 
    // Common Update
    protected void doUpdate()
    {
      if (!_inInit)
        this.Update();
    }
 
    #endregion
 
    #region ISupportInitialize Interface methods
 
    // ISupportInitialize.BeginInit()
    public void BeginInit()
    {
      _inInit = true;
    }
 
    // ISupportInitialize.EndInit()
    public void EndInit()
    {
      _inInit = false;
      this.Refresh();
    }
 
    #endregion
  }
}

________
Alle Angaben ohne Gewähr. Keine Haftung für Vorschläge, Tipps oder sonstige Hilfe, falls es schiefgeht, nur Zeit verschwendet oder man sonst nicht zufrieden ist

alle Nachrichten anzeigenGesamtübersicht  |  Zum Thema  |  Suchen

 ThemaViews  AutorDatum
problem mit selbst programmiertem steuerelement1.462stefanjp29.09.06 11:58
Re: problem mit selbst programmiertem steuerelement1.089ModeratorDaveS29.09.06 13:11
Re: problem mit selbst programmiertem steuerelement1.070stefanjp29.09.06 13:26
"bessere" Formulierung1.148stefanjp29.09.06 13:18
Re: "bessere" Formulierung2.401ModeratorDaveS29.09.06 13:41
Re: "bessere" Formulierung1.103stefanjp29.09.06 14:26
Re: "bessere" Formulierung1.093ModeratorDaveS29.09.06 14:38
Re: "bessere" Formulierung1.162stefanjp29.09.06 15:59
Re: "bessere" Formulierung1.130ModeratorDaveS29.09.06 16:15
Re:1.120stefanjp01.10.06 19:45
Re: "bessere" Formulierung1.008ModeratorDaveS17.07.09 14:42

Sie sind nicht angemeldet!
Um auf diesen Beitrag zu antworten oder neue Beiträge schreiben zu können, müssen Sie sich zunächst anmelden.

Einloggen  |  Neu registrieren

Funktionen:  Zum Thema  |  GesamtübersichtSuchen 

nach obenzurück
 
   

Copyright ©2000-2025 vb@rchiv Dieter Otter
Alle Rechte vorbehalten.
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein.

Diese Seiten wurden optimiert für eine Bildschirmauflösung von mind. 1280x1024 Pixel