منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب
تصحيح هذا الكود - نسخة قابلة للطباعة

+- منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب (http://vb4arb.com/vb)
+-- قسم : قسم لغة السي شارب C#.NET (http://vb4arb.com/vb/forumdisplay.php?fid=175)
+--- قسم : قسم اسئلة C#.NET (http://vb4arb.com/vb/forumdisplay.php?fid=176)
+--- الموضوع : تصحيح هذا الكود (/showthread.php?tid=16359)



تصحيح هذا الكود - أسامة - 05-07-16

قبل التحويل
كود :
   Private Sub InvalidateScroll()
       Debug.Print(CStr(Height))
       If CInt(Math.Round(((_Items.Count) * ItemHeight) / _SelectedHeight)) < CDbl((((_Items.Count) * ItemHeight) / _SelectedHeight)) Then
           VerticalScrollbar._Maximum = CInt(Math.Ceiling(((_Items.Count) * ItemHeight) / _SelectedHeight))
       ElseIf CInt(Math.Round(((_Items.Count) * ItemHeight) / _SelectedHeight)) = 0 Then
           VerticalScrollbar._Maximum = 1
       Else
           VerticalScrollbar._Maximum = CInt(Math.Round(((_Items.Count) * ItemHeight) / _SelectedHeight))
       End If
       Invalidate()
   End Sub
بعد التحويل 

كود :
private void InvalidateScroll()
{
    Debug.Print((Height).ToString());
    if (System.Convert.ToInt32(Math.Round(((_Items.Count) * ItemHeight) / _SelectedHeight)) < System.Convert.ToDouble(((_Items.Count) * ItemHeight) / _SelectedHeight))
    {
        VerticalScrollbar._Maximum = System.Convert.ToInt32(Math.Ceiling(((_Items.Count) * ItemHeight) / _SelectedHeight));
    }
    else if (System.Convert.ToInt32(Math.Round(((_Items.Count) * ItemHeight) / _SelectedHeight)) == 0)
    {
        VerticalScrollbar._Maximum = 1;
    }
    else
    {
        VerticalScrollbar._Maximum = System.Convert.ToInt32(Math.Round(((_Items.Count) * ItemHeight) / _SelectedHeight));
    }
    Invalidate();
}
الخطاء فى Math.Ceiling و Math.Round

الكلاس كامل
http://pastebin.com/DFDVBsYC
الكود كامل بعد التحويل

كود :
public class LogInListBoxWBuiltInScrollBar : Control
{

   #region "Declarations"

   private List<LogInListBoxItem> _Items = new List<LogInListBoxItem>();
   private readonly List<LogInListBoxItem> _SelectedItems = new List<LogInListBoxItem>();
   private bool _MultiSelect = true;
   private int ItemHeight = 24;
   private readonly LogInVerticalScrollBar VerticalScrollbar;
   private Color _BaseColour = Color.FromArgb(55, 55, 55);
   private Color _SelectedItemColour = Color.FromArgb(50, 50, 50);
   private Color _NonSelectedItemColour = Color.FromArgb(47, 47, 47);
   private Color _BorderColour = Color.FromArgb(35, 35, 35);
   private Color _TextColour = Color.FromArgb(255, 255, 255);

   private int _SelectedHeight = 1;
   #endregion

   #region "Properties"

   [Category("Colours")]
   public Color TextColour
   {
       get { return _TextColour; }
       set { _TextColour = value; }
   }

   [Category("Control")]
   public int SelectedHeight
   {
       get { return _SelectedHeight; }
       set
       {
           if (value < 1)
           {
               _SelectedHeight = Height;
           }
           else
           {
               _SelectedHeight = value;
           }
           InvalidateScroll();
       }
   }

   [Category("Colours")]
   public Color BaseColour
   {
       get { return _BaseColour; }
       set { _BaseColour = value; }
   }

   [Category("Colours")]
   public Color SelectedItemColour
   {
       get { return _SelectedItemColour; }
       set { _SelectedItemColour = value; }
   }

   [Category("Colours")]
   public Color NonSelectedItemColour
   {
       get { return _NonSelectedItemColour; }
       set { _NonSelectedItemColour = value; }
   }

   [Category("Colours")]
   public Color BorderColour
   {
       get { return _BorderColour; }
       set { _BorderColour = value; }
   }


   private void HandleScroll(object sender)
   {
       Invalidate();
   }

   private void InvalidateScroll()
   {
       Debug.Print(Convert.ToString(Height));
       if (Convert.ToInt32(Math.Round(((_Items.Count) * ItemHeight) / _SelectedHeight)) < Convert.ToDouble((((_Items.Count) * ItemHeight) / _SelectedHeight)))
       {
           VerticalScrollbar._Maximum = Convert.ToInt32(Math.Ceiling(((_Items.Count) * ItemHeight) / _SelectedHeight));
       }
       else if (Convert.ToInt32(Math.Round(((_Items.Count) * ItemHeight) / _SelectedHeight)) == 0)
       {
           VerticalScrollbar._Maximum = 1;
       }
       else
       {
           VerticalScrollbar._Maximum = Convert.ToInt32(Math.Round(((_Items.Count) * ItemHeight) / _SelectedHeight));
       }
       Invalidate();
   }

   private void InvalidateLayout()
   {
       VerticalScrollbar.Location = new Point(Width - VerticalScrollbar.Width - 2, 2);
       VerticalScrollbar.Size = new Size(18, Height - 4);
       Invalidate();
   }

   public class LogInListBoxItem
   {
       public string Text { get; set; }
       public override string ToString()
       {
           return Text;
       }
   }

   [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content)]
   public LogInListBoxItem[] Items
   {
       get { return _Items.ToArray(); }
       set
       {
           _Items = new List<LogInListBoxItem>(value);
           Invalidate();
           InvalidateScroll();
       }
   }

   public LogInListBoxItem[] SelectedItems
   {
       get { return _SelectedItems.ToArray(); }
   }

   public bool MultiSelect
   {
       get { return _MultiSelect; }
       set
       {
           _MultiSelect = value;

           if (_SelectedItems.Count > 1)
           {
               _SelectedItems.RemoveRange(1, _SelectedItems.Count - 1);
           }

           Invalidate();
       }
   }

   public override Font Font
   {
       get { return base.Font; }
       set
       {
           ItemHeight = Convert.ToInt32(Graphics.FromHwnd(Handle).MeasureString("@", Font).Height);
           if (VerticalScrollbar != null)
           {
               VerticalScrollbar._SmallChange = 1;
               VerticalScrollbar._LargeChange = 1;

           }
           base.Font = value;
           InvalidateLayout();
       }
   }

   public void AddItem(string Items)
   {
       LogInListBoxItem Item = new LogInListBoxItem();
       Item.Text = Items;
       _Items.Add(Item);
       Invalidate();
       InvalidateScroll();
   }

   public void AddItems(string[] Items)
   {
       foreach (var I in Items)
       {
           LogInListBoxItem Item = new LogInListBoxItem();
           Item.Text = System.Convert.ToString(I);
           _Items.Add(Item);
       }
       Invalidate();
       InvalidateScroll();
   }


   public void RemoveItemAt(int index)
   {
       _Items.RemoveAt(index);
       Invalidate();
       InvalidateScroll();
   }

   public void RemoveItem(LogInListBoxItem item)
   {
       _Items.Remove(item);
       Invalidate();
       InvalidateScroll();
   }

   public void RemoveItems(LogInListBoxItem[] items)
   {
       foreach (LogInListBoxItem I in items)
       {
           _Items.Remove(I);
       }
       Invalidate();
       InvalidateScroll();
   }

   protected override void OnSizeChanged(EventArgs e)
   {
       _SelectedHeight = Height;
       InvalidateScroll();
       InvalidateLayout();
       base.OnSizeChanged(e);
   }

   private void Vertical_MouseDown(object sender, MouseEventArgs e)
   {
       Focus();
   }

   protected override void OnMouseDown(MouseEventArgs e)
   {
       Focus();
       if (e.Button == MouseButtons.Left)
       {
           int Offset = System.Convert.ToInt32(VerticalScrollbar.Value * (VerticalScrollbar.Maximum + (Height - (ItemHeight))));

           int Index = System.Convert.ToInt32((e.Y + Offset) / ItemHeight);

           if (Index > _Items.Count - 1)
           {
               Index = -1;
           }

           if (!(Index == -1))
           {

               if (ModifierKeys == Keys.Control && _MultiSelect)
               {
                   if (_SelectedItems.Contains(_Items[Index]))
                   {
                       _SelectedItems.Remove(_Items[Index]);
                   }
                   else
                   {
                       _SelectedItems.Add(_Items[Index]);
                   }
               }
               else
               {
                   _SelectedItems.Clear();
                   _SelectedItems.Add(_Items[Index]);
               }
               Debug.Print((_SelectedItems[0].Text).ToString());
           }

           Invalidate();
       }
       base.OnMouseDown(e);
   }


   protected override void OnMouseWheel(MouseEventArgs e)
   {
       int Move = -((e.Delta * SystemInformation.MouseWheelScrollLines / 120) * (2 / 2));
       int Value = Math.Max(Math.Min(VerticalScrollbar.Value + Move, VerticalScrollbar.Maximum), VerticalScrollbar.Minimum);
       VerticalScrollbar.Value = Value;
       base.OnMouseWheel(e);
   }

   #endregion

   #region "Draw Control"

   public LogInListBoxWBuiltInScrollBar()
   {
       SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.Selectable | ControlStyles.SupportsTransparentBackColor, true);
       DoubleBuffered = true;
       VerticalScrollbar = new LogInVerticalScrollBar();
       VerticalScrollbar._SmallChange = 1;
       VerticalScrollbar._LargeChange = 1;
       VerticalScrollbar.Scroll += HandleScroll;
       VerticalScrollbar.MouseDown += Vertical_MouseDown;
       Controls.Add(VerticalScrollbar);
       InvalidateLayout();
   }

   protected override void OnPaint(PaintEventArgs e)
   {
       object g = e.Graphics;
       dynamic with_1 = g;
       with_1.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
       with_1.SmoothingMode = SmoothingMode.HighQuality;
       with_1.PixelOffsetMode = PixelOffsetMode.HighQuality;
       with_1.Clear(_BaseColour);
       LogInListBoxItem AllItems = default(LogInListBoxItem);
       int Offset = System.Convert.ToInt32(VerticalScrollbar.Value * (VerticalScrollbar.Maximum + (Height - (ItemHeight))));
       int StartIndex = 0;
       if (Offset == 0)
       {
           StartIndex = 0;
       }
       else
       {
           StartIndex = Offset / ItemHeight / VerticalScrollbar.Maximum;
       }
       int EndIndex = System.Convert.ToInt32(Math.Min(StartIndex + System.Convert.ToInt32(Height / ItemHeight), _Items.Count - 1));
       with_1.DrawLine(new Pen(_BorderColour, 2), VerticalScrollbar.Location.X - 1, 0, VerticalScrollbar.Location.X - 1, Height);

       for (int I = StartIndex; I <= _Items.Count - 1; I++)
       {
           AllItems = Items[I];
           int Y = ((I * ItemHeight) + 1 - Offset) + (int)(((double)ItemHeight / 2) - 8);
           if (_SelectedItems.Contains(AllItems))
           {
               with_1.FillRectangle(new SolidBrush(_SelectedItemColour), new Rectangle(0, (I * ItemHeight) + 1 - Offset, Width - 19, ItemHeight - 1));
           }
           else
           {
               with_1.FillRectangle(new SolidBrush(_NonSelectedItemColour), new Rectangle(0, (I * ItemHeight) + 1 - Offset, Width - 19, ItemHeight - 1));
           }
           with_1.DrawLine(new Pen(_BorderColour), 0, ((I * ItemHeight) + 1 - Offset) + ItemHeight - 1, Width - 18, ((I * ItemHeight) + 1 - Offset) + ItemHeight - 1);
           with_1.DrawString(AllItems.Text, new Font("Segoe UI", 8), new SolidBrush(_TextColour), 9, Y);
           with_1.ResetClip();
       }
       with_1.DrawRectangle(new Pen(Color.FromArgb(35, 35, 35), 2), 1, 1, Width - 2, Height - 2);
       //   .DrawLine(New Pen(_BorderColour), 0, ItemHeight, Width, ItemHeight)
       with_1.DrawLine(new Pen(_BorderColour, 2), VerticalScrollbar.Location.X - 1, 0, VerticalScrollbar.Location.X - 1, Height);
       with_1.InterpolationMode = (InterpolationMode)(7);

   }

   #endregion

}



RE: تصحيح هذا الكود - الوادي - 05-07-16

يا أسامة،

جرب هذا التعديل
PHP كود :
private void InvalidateScroll()
{
    
Debug.Print(Convert.ToString(Height));
    if (
Convert.ToInt32(Math.Round((decimal)((_Items.Count) * ItemHeight) / _SelectedHeight)) < Convert.ToDouble((((_Items.Count) * ItemHeight) / _SelectedHeight))) {
        
VerticalScrollbar._Maximum Convert.ToInt32(Math.Ceiling((decimal)((_Items.Count) * ItemHeight) / _SelectedHeight));
    } else if (
Convert.ToInt32(Math.Round((decimal)((_Items.Count) * ItemHeight) / _SelectedHeight)) == 0) {
        
VerticalScrollbar._Maximum 1;
    } else {
        
VerticalScrollbar._Maximum Convert.ToInt32(Math.Round((decimal)((_Items.Count) * ItemHeight) / _SelectedHeight));
    }
    
Invalidate();




RE: تصحيح هذا الكود - أسامة - 05-07-16

(05-07-16, 04:54 PM)الوادي كتب : يا أسامة،

جرب هذا التعديل
PHP كود :
private void InvalidateScroll()
{
    
Debug.Print(Convert.ToString(Height));
    if (
Convert.ToInt32(Math.Round((decimal)((_Items.Count) * ItemHeight) / _SelectedHeight)) < Convert.ToDouble((((_Items.Count) * ItemHeight) / _SelectedHeight))) {
        
VerticalScrollbar._Maximum Convert.ToInt32(Math.Ceiling((decimal)((_Items.Count) * ItemHeight) / _SelectedHeight));
    } else if (
Convert.ToInt32(Math.Round((decimal)((_Items.Count) * ItemHeight) / _SelectedHeight)) == 0) {
        
VerticalScrollbar._Maximum 1;
    } else {
        
VerticalScrollbar._Maximum Convert.ToInt32(Math.Round((decimal)((_Items.Count) * ItemHeight) / _SelectedHeight));
    }
    
Invalidate();

جزاك الله خير