欢迎光临
我们一直在努力

一个使用用户控件(包括组件)的演示-.NET教程,组件控件开发

建站超值云服务器,限时71元/月

1.

imports system.componentmodel
public class pic
    inherits system.windows.forms.usercontrol

#region ” windows 窗体设计器生成的代码 “

    usercontrol1 重写 dispose 以清理组件列表。
    protected overloads overrides sub dispose(byval disposing as boolean)
        if disposing then
            if not (components is nothing) then
                components.dispose()
            end if
        end if
        mybase.dispose(disposing)
    end sub

    windows 窗体设计器所必需的
    private components as system.componentmodel.icontainer

    注意:以下过程是 windows 窗体设计器所必需的
    可以使用 windows 窗体设计器修改此过程。
    不要使用代码编辑器修改它。
    <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
       
        pic
       
        me.name = “pic”
        me.size = new system.drawing.size(48, 48)

    end sub

#end region

    public const m_maxlen as integer = 48 固定的宽和高
    public const m_maxheight as integer = 48
    public sub new(byval m as image) 主要是用于在piccontrols组件中创建实例时使用
        mybase.new()

        该调用是 windows 窗体设计器所必需的。
        initializecomponent()

        在 initializecomponent() 调用之后添加任何初始化
        m_image = m
    end sub
    public sub new()
        mybase.new()

        该调用是 windows 窗体设计器所必需的。
        initializecomponent()

        在 initializecomponent() 调用之后添加任何初始化

    end sub

    private m_image as image = image.fromfile(“g:\练习\重要的例程\使用问题(在格子中显示图片)\gounda takeshi.ico”)
    <category(“grid”), description(“设置卡片的图片。”)> _
    public property image() as image
        get
            return m_image
        end get
        set(byval value as image)
            m_image = value
            me.refresh()
        end set
    end property
    绘制边框和图象
    protected overrides sub onpaint(byval e as system.windows.forms.painteventargs)
        dim g as graphics = me.creategraphics
        me.backcolor = color.white
        g.drawrectangle(system.drawing.pens.black, 0, 0, me.width – 1, me.height – 1)
        dim ic as image = ctype(m_image, image)
        g.drawimage(ic, 0, 0)
    end sub
    不允许调整大小
    protected overrides sub onsizechanged(byval e as system.eventargs)
        me.size = new size(m_maxlen, m_maxheight)
    end sub
    匹配否标志
    private m_double as boolean = false
    <category(“grid”), description(“是否匹配的标志。”)> _
    public property doubles() as boolean
        get
            return m_double
        end get
        set(byval value as boolean)
            m_double = value
        end set
    end property
    private m_id as integer
    <category(“grid”), description(“区分是否来自同一图片的标志。”)> _
    public property id() as integer
        get
            return m_id
        end get
        set(byval value as integer)
            m_id = value
        end set
    end property
 
end class

2.

imports my_namespace
imports system.componentmodel
public class piccontrols
    inherits system.componentmodel.component

#region ” 组件设计器生成的代码 “

    组件重写 dispose 以清理组件列表。
    protected overloads overrides sub dispose(byval disposing as boolean)
        if disposing then
            if not (components is nothing) then
                components.dispose()
            end if
        end if
        mybase.dispose(disposing)
    end sub

    组件设计器所必需的
    private components as system.componentmodel.icontainer

    注意:以下过程是组件设计器所必需的
    可以使用组件设计器修改此过程。
    不要使用代码编辑器修改它。
    <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
        components = new system.componentmodel.container()
    end sub

#end region
    public sub new(byval container as system.componentmodel.icontainer)
        myclass.new()

        windows.forms 类撰写设计器支持所必需的
        container.add(me)

        changepic() 如果选择的图片发生了变化,那么创建卡片集合也要相应的变化。

    end sub

    public sub new()
        mybase.new()

        该调用是组件设计器所必需的。
        initializecomponent()

        在 initializecomponent() 调用之后添加任何初始化

        changepic() 如果选择的图片发生了变化,那么创建卡片集合也要相应的变化。

    end sub
    ////////////////////////////////////////////////////////////////

    dim m_piccontrols as new system.collections.arraylist()
    总数量
    <category(“grid”), description(“集合内卡片的总数。”)> _
        public readonly property count() as integer
        get
            return m_piccontrols.count
        end get
    end property
    指定位置的卡片(这个对于vb.net是比较特殊的,具有带参数的属性)
    default public readonly property items(byval index as integer) as pic
        get
            if index >= 0 and index < m_piccontrols.count then
                return ctype(m_piccontrols(index), pic)
            end if
        end get
    end property

    public sub shuffle() 混卡片,也就是生成一组随机的卡片集合。(这里的算法不错!)
        dim r as new system.random()
        dim d as new system.collections.arraylist()
        dim p as pic
        while (m_piccontrols.count > 0)
            dim removeindex as integer = r.next(0, m_piccontrols.count – 1)
            p = ctype(m_piccontrols(removeindex), my_namespace.pic)
            m_piccontrols.removeat(removeindex)
            d.add(p)
        end while
        m_piccontrols = d
    end sub

    private m_image as imagelist
    <category(“grid”), description(“选择相应的imagelist控件。”)> _
    public property imagelist() as imagelist
        get
            return m_image
        end get
        set(byval value as imagelist)
            m_image = value
            changepic()
        end set
    end property
    /////////
    这个事件比较重要,主要是根据图片的变动来生成不同的卡片集合。
    private sub changepic()
        if m_image is nothing then exit sub
        dim i as integer
        for i = 0 to m_piccontrols.count – 1
            ctype(m_piccontrols(i), pic).dispose() 注意这里。
        next
        m_piccontrols.clear()
        dim j as integer
        for i = 0 to m_image.images.count – 1
            for j = 0 to 3
                dim p as new pic(m_image.images(i))
                p.id = i
                m_piccontrols.add(p)
            next
        next
    end sub
    由于在排列好后,每个在集合中的卡片的doubles属性都会被设置成true,
    所以要在开始一次新的排序时设置所有的卡片该属性为false
    public sub setfalse()
        dim i as integer
        for i = 0 to m_piccontrols.count – 1
            dim apic as pic = ctype(m_piccontrols(i), pic)
            apic.doubles = false
        next
    end sub

end class

3.

imports system.componentmodel
public class picshow
    inherits system.windows.forms.usercontrol

#region ” windows 窗体设计器生成的代码 “

    public sub new()
        mybase.new()

        该调用是 windows 窗体设计器所必需的。
        initializecomponent()

        在 initializecomponent() 调用之后添加任何初始化

    end sub

    usercontrol 重写 dispose 以清理组件列表。
    protected overloads overrides sub dispose(byval disposing as boolean)
        if disposing then
            if not (components is nothing) then
                components.dispose()
            end if
        end if
        mybase.dispose(disposing)
    end sub

    windows 窗体设计器所必需的
    private components as system.componentmodel.icontainer

    注意:以下过程是 windows 窗体设计器所必需的
    可以使用 windows 窗体设计器修改此过程。
    不要使用代码编辑器修改它。
    <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
        components = new system.componentmodel.container()
    end sub

#end region
    //                                 这个程序的原理                                                                        //
    //先做一个pic控件,可以为其设置相应的图片,不允许改变大小,要重写sizechange,onpait事件  //
    //做一个集合组件piccontrols来容纳一定数量的pic卡片,但并不显示它,因为是组件。只是容器  //
    //最后做一个picshow控件,用于显示piccontrols.count数量的卡片集合。                      //
    //比较重要的地方就是如何对卡片进行随机混排(piccontrols的shuffle方法)和picshow控件的   //
    //contrains,start方法。尤其注意这里进行排序的方法:是将卡片在集合里就弄混(随机),这样//
    //我们取得的每个卡片都是随机的了,然后在picshow控件里根据每个卡片的doubles,id属性来进行 //
    //排序,把随机和排序分开了。当然也可以把他们合并写到picshow控件里。不过这里不建议这样。 //
    //因为对于piccontrols组件来说,它的集合就是一个随机产生的卡片集合。这样比较好理解。     //

    private const m_spacing as integer = 10 间隔设置的常量

    private m_rows as integer = 2 对于一个阵列来讲,2行应该更有意义。
    <category(“grid”), description(“矩阵的行。”), defaultvalue(2)> _
        public property row() as integer
        get
            return m_rows
        end get
        set(byval value as integer)
            if value > 0 then
                m_rows = value
                me.refresh()
            end if
        end set
    end property
    private m_columns as integer = 2
    <category(“grid”), description(“矩阵的列。”), defaultvalue(2)> _
    public property columns() as integer
        get
            return m_columns
        end get
        set(byval value as integer)
            if (value > 0) and (value mod 2 = 0) then
                m_columns = value
                me.refresh()
            else
                throw new exception(“不是有效的列值!请输入2的倍数的列值。”)
            end if
        end set
    end property
    private m_collection as piccontrols
    <category(“grid”), description(“指定相应的piccontrols组件。”)> _
    public property getcontrols() as piccontrols
        get
            return m_collection
        end get
        set(byval value as piccontrols)
            m_collection = value
        end set
    end property
    绘制边框,由于还没有将卡片加入到me.controls集合,所以只有边框。
    protected overrides sub onpaint(byval e as system.windows.forms.painteventargs)
        dim height as integer = my_namespace.pic.m_maxheight
        dim width as integer = my_namespace.pic.m_maxlen
        me.width = (width + m_spacing) * m_columns + m_spacing
        me.height = (height + m_spacing) * m_rows + m_spacing
        dim g as graphics = me.creategraphics
        dim row, column as integer
        for row = 0 to m_rows – 1
            for column = 0 to m_columns – 1
                g.drawrectangle(system.drawing.pens.gray, column * (width + m_spacing) + m_spacing, _
                row * (height + m_spacing) + m_spacing, width, height)
            next
        next
    end sub

    private m_double as pic 记录相同的那个卡片

    private m_last as integer  记录格子中的最后一个卡片

    开始排列
    public sub start()

        me.controls.clear() 先清空容器

        if not isnothing(m_collection) then 判断行列之积和卡片数量是否相等
            if (m_collection.count <> m_rows * m_columns) then
                throw new exception(“卡片数量为:” & cstr(m_collection.count) & “格子数量为:” & cstr(m_rows * m_columns))
            end if

            ///////////////////////////////////

            m_last = -2 初始化,因为从0开始是第一个格子,所以初始值为-2

            m_collection.setfalse() 因为开始一次排序就会把所有的卡片pic的double属性全都设置为true。所以,这里要全都设置回false

            m_collection.shuffle() 将卡片弄混

            dim cardcount as integer = 0 卡片指针
            dim row, column as integer

            for row = 0 to m_rows – 1
                for column = 0 to m_columns – 1
                    dim apic as pic = ctype(m_collection(cardcount), pic)
                    加入到me的控件集合
                    me.controls.add(apic)
                    控件集合中的原有卡片进行遍历,看是否有单个的与新加入的同一个图片的卡片
                    dim rint as integer = contrains(apic)

                    select case rint
                        case 0 匹配排列
                            apic.left = m_double.left + m_spacing + my_namespace.pic.m_maxlen
                            apic.top = m_double.top
                        case 1 没有匹配项,间隔排列

                            行,注意一定要使用int进行转化,否则会四舍五入。
                            dim r as integer = int(m_last / (m_rows))
                            列
                            dim c as integer = m_last mod (m_rows)
                            取得行列后就可以直接设置位置了。
                            apic.left = c * (pic.m_maxlen + m_spacing) + m_spacing
                            apic.top = r * (pic.m_maxheight + m_spacing) + m_spacing

                    end select
                    cardcount += 1 下一个卡片
                next
            next
            messagebox.show(“排序完成!”)

        end if
    end sub
    排序的函数
    public function contrains(byval p as pic) as integer
        m_double = nothing 初始值为空,每次排序前要设置为空。
        dim apic as pic
        dim i as integer
        dim count as integer = me.controls.count – 1
        for i = 0 to count – 1 从0到末尾-1,把自己排除掉,自己和自己不必要去比较
            apic = ctype(me.controls(i), pic)
            if (apic.id = p.id) and apic.doubles = false then  and i <> count
                apic.doubles = true 匹配
                p.doubles = true 匹配
                m_double = apic
            end if
        next
        找到匹配的了
        if not (m_double is nothing) then
            return 0
        else
            m_last += 2 分隔开一个格子( 这里的格子开始为0,最后为m_collection的count-1)
            return 1
        end if

    end function
end class

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个使用用户控件(包括组件)的演示-.NET教程,组件控件开发
分享到: 更多 (0)