Vatansever
Asistan
- Katılım
- 23 Ağustos 2007
- Mesajlar
- 405
- Reaksiyon puanı
- 1
- Puanları
- 18
Arkadaşlar programımda bir combobox kullanarak renk seçmek istiyorum ama yapamadım bu iş için tam istediğim gibi bir örnek proje buldum ama C# dilinde yazılmış bunu VB.NET diline dönüştürünce hatalar veriyor bu hatalarıda gideremedim kodları http://www.developerfusion.com/tools/convert/csharp-to-vb/
Adresinde dönüştürdüm acaba yardımcı olabilirmisiniz bu kodları VB.NETe dönüştürebilirmisiniz kodlar çok uzun değil ilgilenirseniz sevinirim.
Örnek projenin lingi aşağıdadır
http://turbobit.net/tj6bbmkczsau.html
ColorComboBox.cs
Program.cs
TestForm.cs
Adresinde dönüştürdüm acaba yardımcı olabilirmisiniz bu kodları VB.NETe dönüştürebilirmisiniz kodlar çok uzun değil ilgilenirseniz sevinirim.
Örnek projenin lingi aşağıdadır
http://turbobit.net/tj6bbmkczsau.html
ColorComboBox.cs
Kod:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
namespace ColorComboBoxApp
{
/// <summary>
/// Inherits from existing ComboBox
/// </summary>
public class ColorComboBox : ComboBox
{
public ColorComboBox()
{
FillColors();
// Change DrawMode for custom drawing
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DropDownStyle = ComboBoxStyle.DropDownList;
}
private void FillColors()
{
this.Items.Clear();
// Fill Colors using Reflection
foreach (Color color in typeof(Color).GetProperties(BindingFlags.Static | BindingFlags.Public).Where(c => c.PropertyType == typeof(Color)).Select(c => (Color)c.GetValue(c, null)))
{
this.Items.Add(color);
}
}
/// <summary>
/// Override Draw Method
/// </summary>
/// <param name="e"></param>
protected override void OnDrawItem(DrawItemEventArgs e)
{
if (e.Index >= 0)
{
Color color = (Color)this.Items[e.Index];
int nextX = 0;
e.Graphics.FillRectangle(new SolidBrush(e.BackColor), e.Bounds);
DrawColor(e, color, ref nextX);
DrawText(e, color, nextX);
}
else
base.OnDrawItem(e);
}
/// <summary>
/// Draw the Color rectangle filled with item color
/// </summary>
/// <param name="e"></param>
/// <param name="color"></param>
/// <param name="nextX"></param>
private void DrawColor(DrawItemEventArgs e, Color color, ref int nextX)
{
int width = e.Bounds.Height * 2 - 8;
Rectangle rectangle = new Rectangle(e.Bounds.X + 3, e.Bounds.Y + 3, width, e.Bounds.Height - 6);
e.Graphics.FillRectangle(new SolidBrush(color), rectangle);
nextX = width + 8;
}
/// <summary>
/// Draw the color name next to the color rectangle
/// </summary>
/// <param name="e"></param>
/// <param name="color"></param>
/// <param name="nextX"></param>
private void DrawText(DrawItemEventArgs e, Color color, int nextX)
{
e.Graphics.DrawString(color.Name, e.Font, new SolidBrush(e.ForeColor), new PointF(nextX, e.Bounds.Y + (e.Bounds.Height - e.Font.Height) / 2));
}
/// <summary>
/// Gets/sets the selected color of ComboBox
/// (Default color is Black)
/// </summary>
public Color Color
{
get
{
if (this.SelectedItem != null)
return (Color)this.SelectedItem;
return Color.Black;
}
set
{
int ix = this.Items.IndexOf(value);
if (ix >= 0)
this.SelectedIndex = ix;
}
}
}
}
Kod:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace ColorComboBoxApp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new TestForm());
}
}
}
Kod:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ColorComboBoxApp
{
public partial class TestForm : Form
{
public TestForm()
{
InitializeComponent();
// Set some random colors
this.colorComboBox1.Color = Color.Blue;
this.colorComboBox2.Color = Color.Red;
this.colorComboBox3.Color = Color.Yellow;
this.colorComboBox4.Color = Color.Violet;
this.colorComboBox5.Color = Color.Orange;
}
private void button1_Click(object sender, EventArgs e)
{
ResetAllTo(Color.Blue);
}
private void button2_Click(object sender, EventArgs e)
{
ResetAllTo(Color.Red);
}
private void button3_Click(object sender, EventArgs e)
{
ResetAllTo(Color.Green);
}
private void button4_Click(object sender, EventArgs e)
{
ResetAllTo(Color.Orange);
}
private void ResetAllTo(Color color)
{
this.Controls.OfType<ColorComboBox>().ToList().ForEach(c => c.Color = color);
}
}
}