未能正确加载“RoslynPackage”包

在安装完DexExpress后,启动VS2015每次创建新项目都弹出“未能正确加载“RoslynPackage”包”的对话框,解决方法如下:

1、删除以下文件夹%appdata%\Local\Microsoft\VisualStudio\14.0\ComponentModelCache (以防万一,请做好备份)

2、删除以下文件夹%appdata%\Local\Microsoft\VisualStudio\14.0\Extensions (以防万一,请做好备份)。P.S.无法删除的话就删除文件夹内的文件。

3、重新打开VS2015,程序自动扫描已安装的扩展并重新生成新的文件夹

【VB.NET】带关闭按钮的选项卡

'来源:http://www.vbforums.com/showthread.php?622242-TabControl-with-Close-button-on-TabPages-(with-Design-Time-support)

Option Strict On
Imports System.ComponentModel



<ToolboxBitmap(GetType(System.Windows.Forms.TabControl))>
Public Class TabControlEx
    Inherits System.Windows.Forms.TabControl

    Private Declare Auto Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    Protected CloseButtonCollection As New Dictionary(Of Button, TabPage)
    Private _ShowCloseButtonOnCurrentTab As Boolean = True
    Private _ShowCloseButtonOnTabs As Boolean = False


    <Browsable(True), DefaultValue(True), Category("Behavior"), Description("Indicates whether a close button should be shown on each TabPage")>
    Public Property ShowCloseButtonOnCurrentTab() As Boolean
        Get
            Return _ShowCloseButtonOnCurrentTab
        End Get
        Set(ByVal value As Boolean)
            _ShowCloseButtonOnCurrentTab = value
            For Each btn In CloseButtonCollection.Keys
                btn.Visible = _ShowCloseButtonOnCurrentTab
            Next
            RePositionCloseButtons()
        End Set
    End Property


    <Browsable(True), DefaultValue(True), Category("Behavior"), Description("Indicates whether a close button should be shown on each TabPage")>
    Public Property ShowCloseButtonOnTabs() As Boolean
        Get
            Return _ShowCloseButtonOnTabs
        End Get
        Set(ByVal value As Boolean)
            _ShowCloseButtonOnTabs = value

            RePositionCloseButtons()
        End Set
    End Property

    Protected Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()


        For Each item In CloseButtonCollection
            item.Value.Text = item.Value.Text.Trim & Space(4)
        Next

        RePositionCloseButtons()
    End Sub

    Protected Overrides Sub OnControlAdded(ByVal e As System.Windows.Forms.ControlEventArgs)
        MyBase.OnControlAdded(e)
        Dim tp As TabPage = DirectCast(e.Control, TabPage)
        Dim rect As Rectangle = Me.GetTabRect(Me.TabPages.IndexOf(tp))
        Dim btn As Button = AddCloseButton(tp)
        btn.Size = New Size(rect.Height - 1, rect.Height - 1)
        btn.Location = New Point(rect.X + rect.Width - rect.Height - 1, rect.Y + 1)
        SetParent(btn.Handle, Me.Handle)
        AddHandler btn.Click, AddressOf OnCloseButtonClick
        CloseButtonCollection.Add(btn, tp)
    End Sub

    Protected Overrides Sub OnControlRemoved(ByVal e As System.Windows.Forms.ControlEventArgs)
        Dim btn As Button = CloseButtonOfTabPage(DirectCast(e.Control, TabPage))
        RemoveHandler btn.Click, AddressOf OnCloseButtonClick
        CloseButtonCollection.Remove(btn)
        SetParent(btn.Handle, Nothing)
        btn.Dispose()
        MyBase.OnControlRemoved(e)
    End Sub

    Protected Overrides Sub OnLayout(ByVal levent As System.Windows.Forms.LayoutEventArgs)
        MyBase.OnLayout(levent)
        RePositionCloseButtons()
    End Sub

    Public Event CloseButtonClick As CancelEventHandler
    Protected Overridable Sub OnCloseButtonClick(ByVal sender As Object, ByVal e As EventArgs)
        If Not DesignMode Then
            Dim btn As Button = DirectCast(sender, Button)
            Dim tp As TabPage = CloseButtonCollection(btn)
            Dim ee As New CancelEventArgs
            RaiseEvent CloseButtonClick(sender, ee)

            If Not ee.Cancel Then
                If MsgBox("是否关闭 " + tp.Text.Trim + "标签?") = MsgBoxResult.Ok Then
                    Me.TabPages.Remove(tp)
                    RePositionCloseButtons()
                End If
            End If
            End If
    End Sub

    Protected Overridable Function AddCloseButton(ByVal tp As TabPage) As Button
        Dim closeButton As New Button
        With closeButton
            '' TODO: Give a good visual appearance to the Close button, maybe by assigning images etc.
            ''       Here I have not used images to keep things simple.
            .Text = "X"
            .FlatStyle = FlatStyle.Flat
            .BackColor = Color.FromKnownColor(KnownColor.ButtonFace)
            .ForeColor = Color.White
            .Font = New Font("Microsoft Sans Serif", 6, FontStyle.Bold)
        End With
        Return closeButton
    End Function

    Public Sub RePositionCloseButtons()
        For Each item In CloseButtonCollection
            RePositionCloseButtons(item.Value)
        Next
    End Sub

    Public Sub RePositionCloseButtons(ByVal tp As TabPage)
        Dim btn As Button = CloseButtonOfTabPage(tp)
        If btn IsNot Nothing Then
            Dim tpIndex As Integer = Me.TabPages.IndexOf(tp)
            If tpIndex >= 0 Then
                Dim rect As Rectangle = Me.GetTabRect(tpIndex)
                If Me.SelectedTab Is tp Then
                    btn.BackColor = Color.Red
                    btn.Size = New Size(rect.Height - 1, rect.Height - 1)
                    btn.Location = New Point(rect.X + rect.Width - rect.Height, rect.Y + 1)
                    btn.Visible = ShowCloseButtonOnCurrentTab
                Else
                    btn.BackColor = Color.FromKnownColor(KnownColor.ButtonFace)
                    btn.Size = New Size(rect.Height - 3, rect.Height - 3)
                    btn.Location = New Point(rect.X + rect.Width - rect.Height - 1, rect.Y + 1)
                    btn.Visible = Me.ShowCloseButtonOnTabs
                End If

                btn.BringToFront()
            End If
        End If
    End Sub

    Protected Function CloseButtonOfTabPage(ByVal tp As TabPage) As Button
        Return (From item In CloseButtonCollection Where item.Value Is tp Select item.Key).FirstOrDefault
    End Function
End Class

 

【VB.NET】TabControl有关事件

事件: Deselecting               e.TabPage.Text=TabPage1
标签: SelectedTab               TabPage1
——————–
事件: Deselected               e.TabPage.Text=TabPage1
标签: SelectedTab               TabPage1
——————–
事件: Selecting               e.TabPage.Text=TabPage2
标签: SelectedTab               TabPage2
——————–
事件: Selected               e.TabPage.Text=TabPage2
标签: SelectedTab               TabPage2
——————–
事件: SelectedIndexChanged               TabPage2
标签: SelectedTab               TabPage2

【VB.NET】自定义TextBox类,添加显示下划线属性

实现功能:
a.ShowLine 参数:设置下划线是否可见
b.LineColor参数:设置下划线颜色
c.LineWidth参数:设置下划线粗细
d.LineOffset参数:设置下划线偏移量

Imports System.ComponentModel

<ToolboxBitmap(GetType(System.Windows.Forms.TextBox))>
Public Class TextBoxEx
    Inherits System.Windows.Forms.TextBox

    Protected _lineColor As Color = Color.Black
    Protected _lineWidth As Integer = 1
    Protected _lineOffset As Integer = 3
    Protected _showLine As Boolean = False
    Protected WithEvents Line As New Control()

    <DefaultValue(GetType(Color), "Black")>
    <Description("下划线颜色")>
    Public Property LineColor As Color
        Get
            Return _lineColor
        End Get
        Set(value As Color)
            If _lineColor <> value Then
                _lineColor = value
                RaiseEvent LineColorChanged(Me, Nothing)
            End If
        End Set
    End Property

    <DefaultValue(1)>
    <Description("下划线宽度")>
    Public Property LineWidth As Integer
        Get
            Return _lineWidth
        End Get
        Set(value As Integer)
            If _lineWidth <> value Then
                _lineWidth = value
                RaiseEvent LineWidthChanged(Me, Nothing)
            End If
        End Set
    End Property

    <DefaultValue(3)>
    <Description("下划线偏移量")>
    Public Property LineOffset As Integer
        Get
            Return _lineOffset
        End Get
        Set(value As Integer)
            If _lineOffset <> value Then
                _lineOffset = value
                RaiseEvent LineOffsetChanged(Me, Nothing)
            End If
        End Set
    End Property

    <DefaultValue(False)>
    <Description("指示控件是否显示下划线")>
    Public Property ShowLine As Boolean
        Get
            Return _showLine
        End Get
        Set(value As Boolean)
            _showLine = value
            RaiseEvent ShowLineChanged(Me, Nothing)
        End Set
    End Property

    ''' <summary>
    ''' 在 TextBoxEx.LineColor 属性值发生更改时发生
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event LineColorChanged(ByVal sender As Object, e As EventArgs)

    ''' <summary>
    ''' 在 TextBoxEx.LineWidth 属性值发生更改时发生
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event LineWidthChanged(ByVal sender As Object, e As EventArgs)

    ''' <summary>
    ''' 在 TextBoxEx.ShowLine 属性值发生更改时发生
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event ShowLineChanged(ByVal sender As Object, e As EventArgs)
    ''' <summary>
    ''' 在 TextBoxEx.LineOffset 属性值发生更改时发生
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event LineOffsetChanged(ByVal sender As Object, e As EventArgs)

    Public Sub onLineWidthChanged(ByVal sender As Object, e As EventArgs) Handles Me.LineWidthChanged, Me.LineOffsetChanged, Me.LineColorChanged
        If Me.ShowLine Then
            rePosLine()
        End If
    End Sub

    Public Sub onShowLineChanged() Handles Me.ShowLineChanged
        If Me.ShowLine Then
            BorderStyle = BorderStyle.None
            BackColor = Color.FromKnownColor(KnownColor.Control)
            rePosLine()
        Else
            BorderStyle = BorderStyle.Fixed3D
            BackColor = Color.FromKnownColor(KnownColor.Window)
            If Not IsNothing(Line) Then
                Line.Visible = False
            End If
        End If
    End Sub

    Protected Overrides Sub OnParentChanged(e As EventArgs)
        MyBase.OnParentChanged(e)
        If Me.ShowLine Then
            rePosLine()
        End If
    End Sub

    Protected Overrides Sub OnLocationChanged(e As EventArgs)
        MyBase.OnLocationChanged(e)
        If Me.ShowLine Then
            rePosLine()
        End If
    End Sub

    Protected Sub rePosLine()
        If Not IsNothing(Me.Parent) Then
            If Not Me.Parent.Controls.Contains(Line) Then
                Me.Parent.Controls.Add(Line)
            End If
            Line.Width = Me.Width
            Line.Height = Me.LineWidth
            Line.BackColor = Me.LineColor
            Line.Location = New Point(Me.Location.X, Me.Location.Y + Me.Height + Me.LineOffset)
            Line.Visible = True
            Line.BringToFront()
        End If
    End Sub

    Protected Overrides Sub Dispose(disposing As Boolean)
        MyBase.Dispose(disposing)
        If disposing Then
            If Not IsNothing(Line) Then
                Line.Dispose()
            End If
        End If
    End Sub

    Protected Overrides Sub OnLayout(levent As LayoutEventArgs)
        MyBase.OnLayout(levent)
        If Me.ShowLine Then
            rePosLine()
        End If
    End Sub
End Class

 

VB.NET 学习笔记

Visual Studio LOGO
Visual Studio LOGO

‘数组相关
dim 数组长度 as integer=12
dim 数组(数组长度-1) as byte() ‘数组长度-1:为数组下标最大值
msgbox(数组.length) ’12

‘如何让文本框自动滚动
textbox1.appendtext(“test”)
textbox1.scrolltocaret()

‘让listviewItem可见
Listview.FindItemWithText(“test”).EnsureVisible()

‘鼠标坐标
System.Windows.Forms.Cursor.Position
Control.MousePosition

‘权限
Imports System.Security.Principal

Module 权限
Public Function 是否为管理员()
Dim identity As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim principal As WindowsPrincipal = New WindowsPrincipal(identity)
Return principal.IsInRole(WindowsBuiltInRole.Administrator)
End Function
End Module

限制 DataGridView 某列只能输入数字(参考这里

 Private Sub TextBoxDec_KeyPress(sender As Object, e As KeyPressEventArgs)
        If e.KeyChar <> "8" And Not Char.IsDigit(e.KeyChar) And e.KeyChar <> "." Then
            e.Handled = True
        End If
    End Sub
    Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        Dim columnIndex As Integer = 4
        If DataGridView1.CurrentCell.ColumnIndex = columnIndex Then
            AddHandler e.Control.KeyPress, AddressOf TextBoxDec_KeyPress
        Else
            RemoveHandler e.Control.KeyPress, AddressOf TextBoxDec_KeyPress
        End If

        Me.Text = DataGridView1.CurrentCell.ColumnIndex.ToString
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
    End Sub

获取当前程序路径及程序名
Application.ExecutablePath

获取程序启动参数(注:参数前后有双引号)
dim para as String=Microsoft.VisualBasic.Command

vb.net 释放项目内文件

百度搜索了一大堆,发现大部分说得都不够详细,于是只好自己记录下了。

  1. 添加文件
    菜单:【项目】>【添加现有项】,在弹出的对话框内选择相应文件(比如:Loading.swf)。
  2. 设置文件属性
    在【解决方案资源管理器】内,找到刚添加的文件Loading.swf,右击选择【属性】
    将其【生成操作】一栏参数设为【嵌入的资源】
  3. 添加代码
    Public Sub 释放文件(ByVal 文件名 As String, ByVal 路径 As String)
          Try
                Dim 文件流 As IO.Stream = Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(My.Application.Info.AssemblyName + "." + 文件名)   '获取资源文件的流
                Dim 文件内容(文件流.Length - 1) As Byte  '定义数组
                Dim 新文件 As New IO.FileStream(路径 + "\" + 文件名, IO.FileMode.OpenOrCreate)   '创建文件
                文件流.Read(文件内容, 0, 文件内容.Length)
                新文件.Write(文件内容, 0, 文件内容.Length)
                文件流.Close()
                新文件.Close()
          Catch ex As System.IO.D
    irectoryNotFoundException
                MessageBox.Show("未找到以下路径:" + 路径, "错误", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error)
          Catch ex As System.NullReferenceException
                MessageBox.Show("未发现资源文件:" + 文件名, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
          Catch ex As exception
                Msgbox(ex.message)
          End Try
    End Sub
  4. 在需要的地方调用上述函数即可。如释放文件(“Loading.swf”, 存储位置)注:区分大小写,且不管该文件是否放在某一特定文件夹内都应只写文件名(C#需包含具体文件夹名)。

案例下载:链接: http://pan.baidu.com/s/1sjoFgiL 密码: 3dn6

另请参阅:http://www.cnblogs.com/chiname/articles/133034.html

《高效 XML》第5部分 Something went really wrong – OutOfMemoryException and StackOverflowException thrown when using XslCompiledTransform

来源:http://blogs.msdn.com/b/xmlteam/archive/2011/09/26/effective-xml-part-5-something-went-really-wrong-outofmemoryexception-and-stackoverflowexception-thrown-when-using-xslcompiledtransform.aspx

So, your application is crashing and it is crashing in?the?bad way. After spending hours of debugging and trying different things you figured out that this is this Xslt stylesheet that causes all the problems. How come? XslCompiledTransform is a compiler. It’s a bit different from C# or VB.NET compilers because it runs at runtime but the result of the compilation is basically the same – IL (Intermediate Language) code. One of disadvantages of running compilation at runtime is that compilation issues may have impact on the running application. In the “offline” scenario compiler would just return an error (the one I hate the most is “Internal Compiler Error” – still it will not affect my app at runtime ?? at worst there is no app). So, compilation may be one source of problems. The other source of problems can be the IL code the stylesheet was compiled to. Here there is not much difference between code generated from Xslt, C# or VB. In any case it is easy to write a program that compiles correctly but misbehaves at runtime (bugs anyone?). Let’s take a look at two most common unexpected exceptions you may hit when working with XslCompiledTransform and ways to resolve them.

继续阅读《高效 XML》第5部分 Something went really wrong – OutOfMemoryException and StackOverflowException thrown when using XslCompiledTransform

《高效 XML》第4部分 Let me project this (Xml file) for you

来源:http://blogs.msdn.com/b/xmlteam/archive/2011/09/26/effective-xml-part-4-let-me-project-this-xml-file-for-you.aspx

Xml is ubiquitous. No doubt about it. It is being used almost everywhere and almost by everyone. This includes places where huge amounts of data are being processed. This means xml files (or streams) used there are also huge. And the bigger the Xml file the harder it is to process. The two biggest problems are:

  • You need to query the document with a couple of XPath expressions or transform it with an Xslt file but the document is too big to be even loaded (the rule of thumb is that an Xml document needs up to 5 times memory of its size on the disk). When you try to load the document you get OutOfMemoryException and that’s about where your Xml processing ends.
  • You are able to load the document but all the queries or transformations are sloooow (and I assume it’s not because the queries or Xslt stylesheets are poorly written – if you are not sure see Effective Xml Part 3)

继续阅读《高效 XML》第4部分 Let me project this (Xml file) for you

《高效 XML》第3部分 Didn’t you say XslCompiledTransform was fast?

来源:http://blogs.msdn.com/b/xmlteam/archive/2011/09/26/effective-xml-part-3-didn-t-you-say-xslcompiledtransform-was-fast.aspx

“XslCompiledTransform is fast.”

“Really?”
“Yeah, XslCompiledTransform is fast… if used correctly.”

Indeed, depending on how you use XslCompiledTransform API your transformations can be really fast or not-that-fast (to say the least). The key to understanding where this difference comes from is to understan what XslCompiledTransform.Load() method actually does. The name of the method indicates that it is just used to load Xslt stylesheets. In reality the method does much more than this. It not only loads the Xslt stylesheet but it also compiles it to IL (Intermediate Language). Yes, after the stylesheet has been “loaded” somewhere there in memory is the real code built based on the provided stylesheet. Generating this code (i.e. “loading” the stylesheet) is costly but it allows for fast transformations – when you start a transformation in your app it no longer deals with the stylesheet, it just invokes the code the Xslt stylesheet was compiled to.

继续阅读《高效 XML》第3部分 Didn’t you say XslCompiledTransform was fast?

《高效XML》第2部分 How to kill the performance of an app with XPath…

来源:http://blogs.msdn.com/b/xmlteam/archive/2011/09/26/effective-xml-part-2-how-to-kill-the-performance-of-an-app-with-xpath.aspx

XPath expressions are pretty flexible. This flexibility allows for very creative ways of using XPath. Unfortunately some of them are suboptimal and cause bad performance of apps. This is especially visible in Xslt transformations where stylesheets contains tens if not hundreds of XPath expressions. Here is the list of the most common bad practices (or even anti-patterns) I have seen:

继续阅读《高效XML》第2部分 How to kill the performance of an app with XPath…