none
user control create property RRS feed

  • Question

  • hai

    I have created one user controll using datagridview by inheriting DataGridViewColumn called Numbox.

    I have added one property for entering format. My error is when i use this control on form and add more than 2 numbox column and set property for one column, all the column is changed to the same property.

    for eg when i change column1 formatstring property to 0.00 then column2 also changed to 0.00, but i want column2 to set 0.000

    please tell what is wrong with my code.

     I use the code below

      
    Private Shared   m_FormatString As String


       Public Class DataGridViewNumBoxColumn
            Inherits DataGridViewColumn
            Public Sub New()
                MyBase.New(New NumboxCell())
                MyBase.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End Sub


     Public Property FormatString() As String
                Get
                    Return m_FormatString
                End Get
                Set(ByVal value As String)
                    m_FormatString = value
                End Set
            End Property
        End Class

     

    Sunday, August 28, 2011 5:29 PM

All replies

  • Because you are declaring m_FormatString as shared. Remove shared keyword and it will work.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Sunday, August 28, 2011 6:13 PM
  • hai

     

    I changed to  

    Public m_FormatString As String

    but when i use this variabe i get this error

    Reference to a non-shared member requires an object reference

    Please tell me a solution

     

    thanks.

     

    Monday, August 29, 2011 3:53 AM
  • I knew that you will get that error. Can you tell me where exactly you are getting that error (full function)? If you are getting the error in FormatString() property, then is your class 'shared'?


    Please mark this post as answer if it solved your problem. Happy Programming!
    Monday, August 29, 2011 4:18 AM
  • hai

     

    yes i get error in formatstring() property.

    What shall i do for this?

    thanks 

    Monday, August 29, 2011 5:56 AM
  • hai

    yes i get error in the property, No this is not a shared class, please tell me how to do this

     i am inserting the full code i use

    thanks


    Public Class IGrid
    
    Public m_FormatString As String
    
     Public Sub New()
        InitializeComponent()
    
     End Sub
      Public Class DataGridViewNumBoxColumn
        Inherits DataGridViewColumn
        Public Sub New()
          MyBase.New(New NumboxCell())
          MyBase.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End Sub
        Public Property FormatString() As String
          Get
            Return m_FormatString
          End Get
          Set(ByVal value As String)
            m_FormatString = value
          End Set
        End Property
        
        Public Overrides Property CellTemplate() As DataGridViewCell
          Get
            Return MyBase.CellTemplate
          End Get
          Set(ByVal value As DataGridViewCell)
            MyBase.CellTemplate = value
          End Set
        End Property
      End Class
      Public Class NumboxCell
        Inherits DataGridViewTextBoxCell
        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
          MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
          Dim ctl As NumBoxEditingControl = CType(DataGridView.EditingControl, NumBoxEditingControl)
          If IsDBNull(Me.Value) = False Then
            ctl.Text = Me.Value
          Else
            ctl.Text = "0"
          End If
        End Sub
        Public Overrides ReadOnly Property EditType() As Type
          Get
            Return GetType(NumBoxEditingControl)
          End Get
        End Property
        Public Overrides ReadOnly Property ValueType() As Type
          Get
            Return GetType(String)
          End Get
        End Property
        Public Overrides ReadOnly Property DefaultNewRowValue() As Object
          Get
            Return "0"
          End Get
        End Property
      End Class
      Private Class NumBoxEditingControl
        Inherits NumBox
        Implements IDataGridViewEditingControl
        Private DataGridViewControl As DataGridView
        Private ValueIsChanged As Boolean = False
        Private RowIndexNum As Integer
        Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
          Get
            Return Me.Result
          End Get
          Set(ByVal value As Object)
            Me.Text = value
          End Set
        End Property
        Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
          Dim CString As String = Val(Me.Text)
          Dim x As String = ""
          If m_Truncate < 0 Then
             CString = Format(Val(CString), m_FormatString)
          Else
            x = (Int(CString * Val(m_TruncatePoint)) / Val(m_TruncatePoint))
    
            If m_ShowDecimals <> 0 Then
              If InStrRev(x, ".", -1, CompareMethod.Text) <> 0 Then
                Dim EndString As String
                EndString = (Microsoft.VisualBasic.Right(x, (Len(x) - InStrRev(x, ".", -1, CompareMethod.Text))))
                CString = Int(CString) & "." & EndString
                If Len(EndString) < m_ShowDecimals Then
                  For a = 1 To (m_ShowDecimals - Len(EndString))
                    CString = CString & "0"
                  Next
                End If
              Else
                CString = Int(CString) & "."
                For a = 1 To m_ShowDecimals
                  CString = CString & "0"
                Next
              End If
    
            End If
          End If
          Return CString
        End Function
        Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
          Me.Font = dataGridViewCellStyle.Font
          Me.BackColor = dataGridViewCellStyle.BackColor
          Me.ForeColor = dataGridViewCellStyle.ForeColor
        End Sub
        Public Property EditingControlRowIndex() As Integer Implements IDataGridViewEditingControl.EditingControlRowIndex
          Get
            Return RowIndexNum
          End Get
          Set(ByVal value As Integer)
            RowIndexNum = value
          End Set
        End Property
        Public Function EditingControlWantsInputKey(ByVal key As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey
          Select Case key And Keys.KeyCode
            Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
              Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp, Keys.Decimal
              Return True
            Case Else
              Return False
          End Select
        End Function
        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
          ' No preparation needs to be done.
        End Sub
        Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
          Get
            Return False
          End Get
        End Property
        Public Property EditingControlDataGridView() As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGridView
          Get
            Return DataGridViewControl
          End Get
          Set(ByVal value As DataGridView)
            DataGridViewControl = value
          End Set
        End Property
        Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged
          Get
            Return ValueIsChanged
          End Get
          Set(ByVal value As Boolean)
            ValueIsChanged = value
          End Set
        End Property
        Public ReadOnly Property EditingControlCursor() As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
          Get
            Return MyBase.Cursor
          End Get
        End Property
        Protected Overrides Sub OnTextChanged(ByVal eventargs As EventArgs)
          ValueIsChanged = True
          Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
          MyBase.OnTextChanged(eventargs)
        End Sub
      End Class
    End Class
    

    Monday, August 29, 2011 6:02 AM
  • Then you shouldn't get this error. Does your class look like below?

    Imports System.Windows.Forms
    
    Public Class DataGridViewNumBoxColumn
        Inherits DataGridViewColumn
    
        Private m_FormatString As String
    
        Public Sub New()
           MyBase.New(New NumboxCell())
           MyBase.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End Sub
    
    
        Public Property FormatString() As String
           Get
               Return m_FormatString
           End Get
           Set(ByVal value As String)
               m_FormatString = value
           End Set
        End Property
    End Class
    


    This piece of code works. If not please post your user control here (atleast relevant)


    Please mark this post as answer if it solved your problem. Happy Programming!
    Monday, August 29, 2011 6:39 AM
  • hai But i want to use m_FormatString in another class called Private Class NumBoxEditingControl Inherits NumBox Implements IDataGridViewEditingControl please read the code i posted above thanks.
    Tuesday, August 30, 2011 5:00 AM
  • Ok. I have updated your DataGridViewNumBoxColumn as below,

    Public Class DataGridViewNumBoxColumn
        Inherits DataGridViewColumn
    
        Dim oIGrid As New IGrid    Public Sub New()
            MyBase.New(New NumboxCell())
            MyBase.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End Sub
    
        Public Property FormatString() As String
            Get
                Return oIGrid.m_FormatString
            End Get
            Set(ByVal value As String)
                oIGrid.m_FormatString = value
            End Set
        End Property
    
        Public Overrides Property CellTemplate() As DataGridViewCell
            Get
                Return MyBase.CellTemplate
            End Get
            Set(ByVal value As DataGridViewCell)
                MyBase.CellTemplate = value
            End Set
        End Property
    End Class
    

    Also, I have updated your NumBoxEditingControl class as below.

    Private Class NumBoxEditingControl
        Inherits NumBox
        Implements IDataGridViewEditingControl
        Private DataGridViewControl As DataGridView
        Private ValueIsChanged As Boolean = False
        Private RowIndexNum As Integer
        Private oIGrid As New IGrid
        Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
          Get
              Return Me.Result
          End Get
          Set(ByVal value As Object)
            Me.Text = value
          End Set
        End Property
    
        Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
          Dim CString As String = Val(Me.Text)
          Dim x As String = ""
          If m_Truncate < 0 Then
            CString = Format(Val(CString), oIGrid.m_FormatString)
          Else
            x = (Int(CString * Val(m_TruncatePoint)) / Val(m_TruncatePoint))
    
            If m_ShowDecimals <> 0 Then
              If InStrRev(x, ".", -1, CompareMethod.Text) <> 0 Then
                Dim EndString As String
                EndString = (Microsoft.VisualBasic.Right(x, (Len(x) - InStrRev(x, ".", -1, CompareMethod.Text))))
                CString = Int(CString) & "." & EndString
                If Len(EndString) < m_ShowDecimals Then
                  For a = 1 To (m_ShowDecimals - Len(EndString))
                    CString = CString & "0"
                  Next
                End If
              Else
                CString = Int(CString) & "."
                For a = 1 To m_ShowDecimals
                  CString = CString & "0"
                Next
              End If
    
            End If
          End If
          Return CString
        End Function
        Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
          Me.Font = dataGridViewCellStyle.Font
          Me.BackColor = dataGridViewCellStyle.BackColor
          Me.ForeColor = dataGridViewCellStyle.ForeColor
        End Sub
        Public Property EditingControlRowIndex() As Integer Implements IDataGridViewEditingControl.EditingControlRowIndex
          Get
            Return RowIndexNum
          End Get
          Set(ByVal value As Integer)
            RowIndexNum = value
          End Set
        End Property
        Public Function EditingControlWantsInputKey(ByVal key As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey
          Select Case key And Keys.KeyCode
            Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
             Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp, Keys.Decimal
              Return True
            Case Else
              Return False
          End Select
        End Function
        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
          ' No preparation needs to be done.
        End Sub
        Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
          Get
            Return False
          End Get
        End Property
        Public Property EditingControlDataGridView() As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGridView
          Get
            Return DataGridViewControl
          End Get
          Set(ByVal value As DataGridView)
            DataGridViewControl = value
          End Set
        End Property
        Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged
          Get
            Return ValueIsChanged
          End Get
          Set(ByVal value As Boolean)
            ValueIsChanged = value
          End Set
        End Property
        Public ReadOnly Property EditingControlCursor() As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
          Get
            Return MyBase.Cursor
          End Get
        End Property
        Protected Overrides Sub OnTextChanged(ByVal eventargs As EventArgs)
          ValueIsChanged = True
          Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
          MyBase.OnTextChanged(eventargs)
        End Sub
    End Class
    



    Try them


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Proposed as answer by Paul Zhou Tuesday, August 30, 2011 8:33 AM
    Tuesday, August 30, 2011 5:36 AM
  • hai thanks for the coperation, I tried as u said, there was no error, but after setting value from property window,  after closing and when i open the property window the value i entered disappears, please tell what is wrong with my code.

    ie value is not set for FormatString

     

    thanks

     

     


    Tuesday, August 30, 2011 4:15 PM
  • Have you check the value of the property? Though you can not see the value, the value may be have been set correctly.

    When you use the same instance of class, the property would be set correctly after modifying it. Unless you have created another instance of the class, the property value would be set back to default value.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 1, 2011 8:20 AM
  • hai

    sorry this is not working

     

    can u please create a new user control project with working code and sent to my mail id.

     

    my id is  babucr@hotmail.com

     

    thanks

     

     

    Friday, September 2, 2011 6:48 AM
  •  

    Hi,

     

    I wrote this sample to explain how the property works.

     

    MyControl is the user control that I would use. It has a new property names "newProperty". You can see that there are two constructors, one has no parameter, another has one parameter.

     

    class MyControl

        {

            public MyControl()

            {

                _newProperty = "NoParameterDefault";

            }

            public MyControl(string newProperty)

            {

                _newProperty = newProperty;

            }

            private string _newProperty;

            public string newProperty

            {

                get { return _newProperty; }

                set { this._newProperty = value; }

            }

        }

     

    When we create a instance of the control as below

    MyControl myControl = new MyControl();

    The new property would has a default value--"NoParameterDefault" in the default constructor:

    public MyControl()

            {

                _newProperty = "NoParameterDefault";

            }

     

    If you create a instance as below:

    MyControl myControl = new MyControl("TestValue");

    The new property would has a value that you defined.

     

    When you close the control, the instance would be freed. So even you change the value of  new property, it won't show to you when you open another new MyControl(it actually creates a new instance). But if you create an instance using another MyControl instance, the changed value would show to it.

    MyControl Copy1control = myControl;

     

    I hope this can help you understand it.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, September 2, 2011 8:01 AM
  • hai

    This wrks only in the same class but i want variable in two different class.

     

    if u can give me your  id i will send the project

     

    thanks.

    Friday, September 2, 2011 6:56 PM
  • Hi,

    You can send the project to my office-Email: v-paulz@microsoft.com.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, September 6, 2011 6:20 AM
  •  

    hai

     

     i have send my project to your id

    thanks

    Wednesday, September 14, 2011 3:50 AM
  • hai

     please send the project after correcting, it is urgent

    my project is pending

    thanks

    Wednesday, September 21, 2011 5:48 PM