求把C#代码翻译成DELPHI,只有一点点
publicForm1(){InitializeComponent();this.comboBox1.Items.AddRange(newstring[]{"hello"...
public Form1()
{
InitializeComponent();
this.comboBox1.Items.AddRange(new string[] { "hello", "world", "nihao" });
this.comboBox1.DrawItem += new DrawItemEventHandler(ComboBox1_DrawItem);
this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
}
private void ComboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
ComboBox combo = sender as ComboBox;
string text = combo.Items[e.Index].ToString();
Brush brush = System.Drawing.Brushes.Black;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
this.Text = text;
brush = System.Drawing.Brushes.White;
}
e.DrawBackground();
e.Graphics.DrawString(text, combo.Font, brush, e.Bounds.X, e.Bounds.Y);
e.DrawFocusRectangle();
} 展开
{
InitializeComponent();
this.comboBox1.Items.AddRange(new string[] { "hello", "world", "nihao" });
this.comboBox1.DrawItem += new DrawItemEventHandler(ComboBox1_DrawItem);
this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
}
private void ComboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
ComboBox combo = sender as ComboBox;
string text = combo.Items[e.Index].ToString();
Brush brush = System.Drawing.Brushes.Black;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
this.Text = text;
brush = System.Drawing.Brushes.White;
}
e.DrawBackground();
e.Graphics.DrawString(text, combo.Font, brush, e.Bounds.X, e.Bounds.Y);
e.DrawFocusRectangle();
} 展开
1个回答
展开全部
这是一段代码,对于Comboboxe根据是否选中采用自绘的方式(OwnerDraw)来显示。是练手的代码。
建议:开始学习时,不要采用ownerDraw,在界面上没必要花太多时间,等以后熟悉Delphi了再来学。
Delphi的Combobox初始化的代码:
procedure TForm1.FormShow(Sender: TObject);
//在Form显示时初始化为好,而不是在create时。C#的例子是在Create时初始化的
begin
combobox1.items.clear; //不管以前items里有没有内容,都先清空
combobox1.items.commatext:='hello,world,nihao';
...
end;
建议:开始学习时,不要采用ownerDraw,在界面上没必要花太多时间,等以后熟悉Delphi了再来学。
Delphi的Combobox初始化的代码:
procedure TForm1.FormShow(Sender: TObject);
//在Form显示时初始化为好,而不是在create时。C#的例子是在Create时初始化的
begin
combobox1.items.clear; //不管以前items里有没有内容,都先清空
combobox1.items.commatext:='hello,world,nihao';
...
end;
追问
你没答到点子上,上面的代码是COMBOBOX悬停处理
DELPHI的COMBOBOX没有悬停事件,所以我想翻译一下,我不是初学好吧
他只是在DrawItem事件中处理而已
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询