一个问题,关于控件编写。200分

阿牛138588 2008-06-09 05:37:41
我想写一个替代tooltip或者helpprovider的类,但有一事不明白。
像此类控件(组件)当你添加到窗体时,窗体上的每个控件就多了一个属性,类似于:在tooltip1上的tooltip
这个属性是怎么弄的?
...全文
245 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
starts_2000 2008-06-20
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 bejon 的回复:]
用c明显浪费资源,保存一个类当然比保存一个名称费资源。一个控件的名字是具有唯一性的。

问题是,在运行时怎么获得某个控件的对应字符串。我的获得方法有错吗?
[/Quote]

只是一个引用,保来浪费源?
yagebu1983 2008-06-20
  • 打赏
  • 举报
回复
学习了!!
帮你顶!!
lalac 2008-06-19
  • 打赏
  • 举报
回复
[SRCategory("CatBehavior"), SRDescription("ToolTipDrawEventDescr")]
是internal的,ms自己写的从资源里取分类和描述的attribute,都反code了,自己跟过去看看就知道了。
lalac 2008-06-19
  • 打赏
  • 举报
回复
1、c.Name在运行时可能为空串,因为Extender生成代码在最前面,如果想节省空间可以考虑Hash,但是性能差好多。
2、lz没有处理任何事件或者消息,何时弹出提示来?照lz的意思貌似是想Mouse hover TextBox时提示一下,这里你需要自己挂事件处理才行的。


这里从你的代码改了一下示例,举例可用相差甚远,lz自己改改用吧:

[ProvideProperty("HelpTip", typeof(Control))]
public partial class HelpTip : Component, IExtenderProvider
{
private Hashtable hstData = new Hashtable(); //存放各控件的提示值字符

public HelpTip()
{
InitializeComponent();
}

public HelpTip(IContainer container)
{
container.Add(this);

InitializeComponent();
}

public bool CanExtend(object o)
{
return o is TextBox; //只有这里返回为真时才会被扩展
}

public void SetHelpTip(Control c, string sValue)
{
int hash = c.GetHashCode();
if (!hstData.Contains(hash))
{
hstData.Add(hash, sValue);

// Means here should handle mouse hover event.
c.MouseHover += new EventHandler(c_MouseHover);
c.Disposed += new EventHandler(c_Disposed);
}
else
hstData[hash] = sValue;
}

void c_Disposed(object sender, EventArgs e)
{
Control c = sender as Control;
if (c != null)
{
c.MouseHover -= new EventHandler(c_MouseHover);
c.Disposed -= new EventHandler(c_Disposed);
}
}

void c_MouseHover(object sender, EventArgs e)
{
TextBox c = sender as TextBox;
if (c != null)
{
Form f = c.FindForm();
if (f != null)
showTip(c, f);
}
}

[DefaultValue("")]
[Description("件鼠标悬停时控件的提示")]
public string GetHelpTip(Control c)
{
int hash = c.GetHashCode();
if (hstData.Contains(hash))
return hstData[hash].ToString();
else
return "";
}

//前景色
private Color fc = Color.Black;
public Color foreColor
{
get { return fc; }
set { fc = value; }
}
//窗体背景色
private Color bbc = Color.White;
public Color baseBackColor
{
get { return bbc; }
set { bbc = value; }
}
//label背景色
private Color lblBc = Color.Yellow;
public Color lblBackColor
{
get { return lblBc; }
set { lblBc = value; }
}
//窗体背景图
private Image BackImageT = null;
public Image BackImage
{
get { return BackImageT; }
set { BackImageT = value; }
}
//窗体透明度
private byte op = 100;
public byte Opatity
{
get { return op; }
set
{
byte oT = op;
try
{
op = byte.Parse(value.ToString());
}
catch
{
MessageBox.Show("无效的值!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
op = oT;
}
}
}
//lable显示的字体
private Font ft = new Font("宋体", 9);
public Font sFont
{
get { return ft; }
set { ft = value; }
}
//多长时间没有动静时显示
private int delay = 500;
public int AutoMaticDelay
{
get { return delay; }
set { delay = value; }
}
//每次自动显示多长时间(有键盘及鼠标操作时会停止显示)
private int popdelay = 3000;
public int AutoPopDelay
{
get { return popdelay; }
set { popdelay = value; }
}


//主要显示用的窗体
private class TipForm : System.Windows.Forms.Form
{
public TipForm(Color bckColor, Color lblBckColor)
{
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = bckColor;
this.TopMost = true;
this.MinimumSize = new Size(1, 1);
this.ShowInTaskbar = false;
this.BackColor = bckColor;
}
}

public void showTip(TextBox c, Form f)
{
showTip(c.Location, f.Location, f.Size, c);
}

public void showTip(Point ControlLocation, Point FormLocation, Size FormSize, Control c)
{
TipForm tf = new TipForm(baseBackColor, lblBackColor);
tf.Location = new Point(10, 10);
tf.Width = 200;
tf.Height = 200;
tf.Show();
//获得大小

//添加提示内容(一图片及label)
Label lb = new Label();

if (hstData.Contains(c.Name)) //这里获得不到值
{
lb.ForeColor = foreColor;
lb.BackColor = lblBackColor;
lb.Font = sFont;
lb.AutoSize = true;
lb.Text = hstData[c.Name].ToString();
tf.Controls.Add(lb);
}
}
}
阿牛138588 2008-06-19
  • 打赏
  • 举报
回复
我反编译了vs自带的tooltip控件
把他的所有源码贴到ide时提示以下错误,这个是什么东西
错误 31 找不到类型或命名空间名称“SRDescription”(是否缺少 using 指令或程序集引用?) G:\我的文档\Visual Studio 2005\Projects\help\help\ht.cs 26 98 help

下面这东西需要引用什么吗??需要using什么吗?(system.windwos.forms下没这东西?)
[SRCategory("CatBehavior"), SRDescription("ToolTipDrawEventDescr")]
public event DrawToolTipEventHandler Draw;
xiaoqiuwyh 2008-06-19
  • 打赏
  • 举报
回复
虽然不会,但是替楼主顶下啦.
Indian520 2008-06-19
  • 打赏
  • 举报
回复
关注中。。。。。。。
阿牛138588 2008-06-19
  • 打赏
  • 举报
回复
用c明显浪费资源,保存一个类当然比保存一个名称费资源。一个控件的名字是具有唯一性的。

问题是,在运行时怎么获得某个控件的对应字符串。我的获得方法有错吗?
starts_2000 2008-06-19
  • 打赏
  • 举报
回复
建议不要用c.Name,用c,因为代码生成器的代码生成是按字母排序的
阿牛138588 2008-06-19
  • 打赏
  • 举报
回复
怎 么没人呢????
阿牛138588 2008-06-18
  • 打赏
  • 举报
回复
意思是,设置控件的提示值,并显示出来。
helptip a = new helptip();
a.showTip(textBox1, this); //textbox1在helptip1上的值为abc
在Form1.Designer.cs里有这样一句this.helptip1.SetHelpTip(this.textBox1, "abc");,此句为系统自动生成
阿牛138588 2008-06-18
  • 打赏
  • 举报
回复
以下源码不能正常工作,麻烦哪个有经验的人帮忙看一下

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace help
{
[ProvideProperty("HelpTip", typeof(Control))]
public partial class helptip : Component, IExtenderProvider
{
private Hashtable hstData = new Hashtable(); //存放各控件的提示值字符

public helptip()
{
InitializeComponent();
}

public helptip(IContainer container)
{
container.Add(this);
InitializeComponent();
}

public bool CanExtend(object o)
{
return o is TextBox; //只有这里返回为真时才会被扩展
}


public void SetHelpTip(Control c, string sValue)
{
if (!hstData.Contains(c.Name))
hstData.Add(c.Name, sValue); //这里的c.name永远是空字符串""
else
hstData[c.Name] = sValue;
}
[DefaultValue("")]
[Description("件鼠标悬停时控件的提示")]
public string GetHelpTip(Control c)
{
if (hstData.Contains(c.Name ))
return hstData[c.Name].ToString();
else
return "";
}

//前景色
private Color fc = Color.Black;
public Color foreColor
{
get { return fc; }
set { fc = value; }
}
//窗体背景色
private Color bbc = Color.White;
public Color baseBackColor
{
get { return bbc;}
set { bbc = value; }
}
//label背景色
private Color lblBc = Color.Yellow;
public Color lblBackColor
{
get { return lblBc; }
set { lblBc = value; }
}
//窗体背景图
private Image BackImageT = null;
public Image BackImage
{
get { return BackImageT; }
set { BackImageT = value; }
}
//窗体透明度
private byte op = 100;
public byte Opatity
{
get { return op; }
set
{
byte oT = op;
try
{
op = byte.Parse(value.ToString());
}
catch
{
MessageBox.Show("无效的值!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
op = oT;
}
}
}
//lable显示的字体
private Font ft=new Font("宋体",9);
public Font sFont
{
get { return ft; }
set { ft = value; }
}
//多长时间没有动静时显示
private int delay = 500;
public int AutoMaticDelay
{
get { return delay; }
set { delay = value; }
}
//每次自动显示多长时间(有键盘及鼠标操作时会停止显示)
private int popdelay = 3000;
public int AutoPopDelay
{
get { return popdelay; }
set { popdelay = value; }
}


//主要显示用的窗体
private class TipForm : System.Windows.Forms.Form
{
public TipForm(Color bckColor,Color lblBckColor)
{
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = bckColor;
this.TopMost = true;
this.MinimumSize = new Size(1, 1);
this.ShowInTaskbar = false;
this.BackColor = bckColor;
}
}

public void showTip(TextBox c, Form f)
{
showTip(c.Location, f.Location, f.Size,c);
}
public void showTip(Point ControlLocation, Point FormLocation, Size FormSize,Control c)
{
TipForm tf = new TipForm(baseBackColor, lblBackColor);
tf.Location = new Point(10, 10);
tf.Width = 200;
tf.Height = 200;
tf.Show();
//获得大小

//添加提示内容(一图片及label)
Label lb = new Label();

if (hstData.Contains(c.Name)) //这里获得不到值
{
lb.ForeColor = foreColor;
lb.BackColor = lblBackColor;
lb.Font = sFont;
lb.AutoSize = true;
lb.Text = hstData[c.Name].ToString();
tf.Controls.Add(lb);
}
}
}
}
fuadam 2008-06-10
  • 打赏
  • 举报
回复
顶顶顶
阿牛138588 2008-06-10
  • 打赏
  • 举报
回复
同志们欢迎入群:28702746

顶贴散分!
kbryant 2008-06-10
  • 打赏
  • 举报
回复
学习一下
烈火焚身 2008-06-10
  • 打赏
  • 举报
回复
学习一下,呵呵
zlkingdom 2008-06-10
  • 打赏
  • 举报
回复
窗体上的每个控件就多了一个属性?
这个功能有点难度啊,理论上应该和程序集有关
ypacyhero 2008-06-10
  • 打赏
  • 举报
回复
狂顶
ypacyhero 2008-06-10
  • 打赏
  • 举报
回复
只要是public出来的东西,都会在属性栏上有。

virusswb 2008-06-10
  • 打赏
  • 举报
回复
www.cnblogs.com/clingingboy
加载更多回复(14)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧