Centar za edukaciju-BiH



#1 08.01.2019 13:07
xl_kid Van mreze
Clan
Registrovan od:11.11.2008
Postovi:154


Predmet:DataGridView - masked field text to time
Kako da u DataGridView postavim mask da upišem recimo 1111 ili 11,11 ili 11.11 i da mi to pretvori u vreme 11:11?
Stavio sam da mi je polje time (t) i ako upišem 11:11 radi ok ali hteo bih da olakšam unos da izbegnem : (shift + .)
↑  ↓

#2 08.01.2019 16:04
Gjoreski Van mreze
Administrator
Registrovan od:02.02.2009
Postovi:1,854


Predmet:Re: DataGridView - masked field text to time
nisam imao vremena da isprobam ova , kako vidim ovo bi trebalo raditi .
PreuzmiIzvorni kôd (vbnet):
  1. The masked textbox is a nice way to make sure data is entered in the proper format.  Here is a way to place a maskedtextbox in the datagridview.
  2.  
  3. Public Class MaskedEditColumn
  4.     Inherits DataGridViewColumn
  5.     Public Sub New()
  6.         MyBase.New(New MaskedEditCell())
  7.     End Sub
  8.     Public Overrides Property CellTemplate() As DataGridViewCell
  9.         Get
  10.             Return MyBase.CellTemplate
  11.         End Get
  12.         Set(ByVal value As DataGridViewCell)
  13.  
  14.             ' Ensure that the cell used for the template is a CalendarCell.
  15.             If Not (value Is Nothing) AndAlso _
  16.                 Not value.GetType().IsAssignableFrom(GetType(MaskedEditCell)) _
  17.                 Then
  18.                 Throw New InvalidCastException("Must be a MaskedEditCell")
  19.             End If
  20.             MyBase.CellTemplate = value
  21.         End Set
  22.     End Property
  23.     Private m_strMask As String
  24.     Public Property Mask() As String
  25.         Get
  26.             Return m_strMask
  27.         End Get
  28.         Set(ByVal value As String)
  29.             m_strMask = value
  30.         End Set
  31.     End Property
  32.     Private m_tyValidatingType As Type
  33.     Public Property ValidatingType() As Type
  34.         Get
  35.             Return m_tyValidatingType
  36.         End Get
  37.         Set(ByVal value As Type)
  38.             m_tyValidatingType = value
  39.         End Set
  40.     End Property
  41.     Private m_cPromptChar As Char = "_"c
  42.     Public Property PromptChar() As Char
  43.         Get
  44.             Return m_cPromptChar
  45.         End Get
  46.         Set(ByVal value As Char)
  47.             m_cPromptChar = value
  48.         End Set
  49.     End Property
  50.     Private ReadOnly Property MaskedEditCellTemplate() As MaskedEditCell
  51.         Get
  52.             Return TryCast(Me.CellTemplate, MaskedEditCell)
  53.         End Get
  54.     End Property
  55. End Class
  56.  
  57.  
  58. Public Class MaskedEditCell
  59.     Inherits DataGridViewTextBoxCell
  60.     Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
  61.         ByVal initialFormattedValue As Object, _
  62.         ByVal dataGridViewCellStyle As DataGridViewCellStyle)
  63.         ' Set the value of the editing control to the current cell value.
  64.         MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
  65.             dataGridViewCellStyle)
  66.         Dim mecol As MaskedEditColumn = DirectCast(OwningColumn, MaskedEditColumn)
  67.         Dim ctl As MaskedEditingControl = _
  68.             CType(DataGridView.EditingControl, MaskedEditingControl)
  69.         Try
  70.             ctl.Text = Me.Value.ToString
  71.         Catch
  72.             ctl.Text = ""
  73.         End Try
  74.         ctl.Mask = mecol.Mask
  75.         ctl.PromptChar = mecol.PromptChar
  76.         ctl.ValidatingType = mecol.ValidatingType
  77.     End Sub
  78.     Public Overrides ReadOnly Property EditType() As Type
  79.         Get
  80.             ' Return the type of the editing contol that CalendarCell uses.
  81.             Return GetType(MaskedEditingControl)
  82.         End Get
  83.     End Property
  84.     Public Overrides ReadOnly Property ValueType() As Type
  85.         Get
  86.             ' Return the type of the value that CalendarCell contains.
  87.             Return GetType(String)
  88.         End Get
  89.     End Property
  90.     Public Overrides ReadOnly Property DefaultNewRowValue() As Object
  91.         Get
  92.             ' Use the current date and time as the default value.
  93.             Return ""
  94.         End Get
  95.     End Property
  96.     Protected Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal cellBounds As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal cellState As System.Windows.Forms.DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
  97.         MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
  98.     End Sub
  99. End Class
  100.  
  101.  
  102. Class MaskedEditingControl
  103.     Inherits MaskedTextBox
  104.     Implements IDataGridViewEditingControl
  105.     Private dataGridViewControl As DataGridView
  106.     Private valueIsChanged As Boolean = False
  107.     Private rowIndexNum As Integer
  108.     Public Property EditingControlFormattedValue() As Object _
  109.         Implements IDataGridViewEditingControl.EditingControlFormattedValue
  110.         Get
  111.             Return Me.Text
  112.         End Get
  113.         Set(ByVal value As Object)
  114.             Me.Text = value.ToString
  115.         End Set
  116.     End Property
  117.     Public Function EditingControlWantsInputKey(ByVal key As Keys, _
  118.            ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
  119.            Implements IDataGridViewEditingControl.EditingControlWantsInputKey
  120.         Return True
  121.     End Function
  122.     Public Function GetEditingControlFormattedValue(ByVal context _
  123.         As DataGridViewDataErrorContexts) As Object _
  124.         Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
  125.         Return Me.Text
  126.     End Function
  127.     Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As  _
  128.         DataGridViewCellStyle) _
  129.         Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
  130.  
  131.         Me.Font = dataGridViewCellStyle.Font
  132.         Me.ForeColor = dataGridViewCellStyle.ForeColor
  133.         Me.BackColor = dataGridViewCellStyle.BackColor
  134.     End Sub
  135.     Public Property EditingControlRowIndex() As Integer _
  136.         Implements IDataGridViewEditingControl.EditingControlRowIndex
  137.         Get
  138.             Return rowIndexNum
  139.         End Get
  140.         Set(ByVal value As Integer)
  141.             rowIndexNum = value
  142.         End Set
  143.     End Property
  144.     Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
  145.         Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
  146.         ' No preparation needs to be done.
  147.     End Sub
  148.     Public ReadOnly Property RepositionEditingControlOnValueChange() _
  149.         As Boolean Implements _
  150.         IDataGridViewEditingControl.RepositionEditingControlOnValueChange
  151.         Get
  152.             Return False
  153.         End Get
  154.     End Property
  155.     Public Property EditingControlDataGridView() As DataGridView _
  156.         Implements IDataGridViewEditingControl.EditingControlDataGridView
  157.         Get
  158.             Return dataGridViewControl
  159.         End Get
  160.         Set(ByVal value As DataGridView)
  161.             dataGridViewControl = value
  162.         End Set
  163.     End Property
  164.  
  165.     Public Property EditingControlValueChanged() As Boolean _
  166.         Implements IDataGridViewEditingControl.EditingControlValueChanged
  167.         Get
  168.             Return valueIsChanged
  169.         End Get
  170.         Set(ByVal value As Boolean)
  171.             valueIsChanged = value
  172.         End Set
  173.     End Property
  174.     Public ReadOnly Property EditingControlCursor() As Cursor _
  175.         Implements IDataGridViewEditingControl.EditingPanelCursor
  176.         Get
  177.             Return MyBase.Cursor
  178.         End Get
  179.     End Property
  180.     Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
  181.         ' Notify the DataGridView that the contents of the cell have changed.
  182.         valueIsChanged = True
  183.         Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
  184.         MyBase.OnTextChanged(e)
  185.     End Sub
  186. End Class
  187.  
  188.  
  189. This works:
  190.  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  191.         'TODO: This line of code loads data into the 'EquipMilesDataSet.equipment' table. You can move, or remove it, as needed.
  192.         Me.EquipmentTableAdapter.Fill(Me.EquipMilesDataSet.equipment)
  193.         'TODO: This line of code loads data into the 'EquipMilesDataSet.equipmiles' table. You can move, or remove it, as needed.
  194.         Me.EquipmilesTableAdapter.Fill(Me.EquipMilesDataSet.equipmiles)
  195.  
  196.         Dim mtbc = New MaskedEditColumn
  197.         mtbc.Mask = "00/00/0000"
  198.         EquipmilesDataGridView.Columns.Add(mtbc)
  199.  
  200.     End Sub
↑  ↓

#3 10.01.2019 09:10
xl_kid Van mreze
Clan
Registrovan od:11.11.2008
Postovi:154


Predmet:Re: DataGridView - masked field text to time
Ovo radi Smiling

sada imam sledeći problem. kada uklonim

Dim mtbc = New MaskedEditColumn
mtbc.Mask = "00/00/0000"
EquipmilesDataGridView.Columns.Add(mtbc)

iz load i u datagridu promenim ColumnType na MaskedEditColumn otvori mi dodatne opcije ali ne prihvata Mask.
↑  ↓

#4 10.01.2019 11:18
Gjoreski Van mreze
Administrator
Registrovan od:02.02.2009
Postovi:1,854


Predmet:Re: DataGridView - masked field text to time
Ne prohvata mask sta to znaci ?
Mozes malo pojasniti
↑  ↓

#5 10.01.2019 12:19
xl_kid Van mreze
Clan
Registrovan od:11.11.2008
Postovi:154


Predmet:Re: DataGridView - masked field text to time
Evo slike. Kada promenim columntype u datagrid-u na bound polju ne prihvata tj ne radi mask.

Slicice prilozenih slika:
1.png
Tip datoteke:Informacije o tipu datoteke za:png png
Preuzimanja:309
Velicina datoteke:270.65 KB
Velicina slike: 1920 x 1080 Pikseli
2.png
Tip datoteke:Informacije o tipu datoteke za:png png
Preuzimanja:307
Velicina datoteke:266.77 KB
Velicina slike: 1920 x 1080 Pikseli

↑  ↓

#6 11.01.2019 11:48
xl_kid Van mreze
Clan
Registrovan od:11.11.2008
Postovi:154


Predmet:Re: DataGridView - masked field text to time
Kada dodam ovaj kod radi. Doda novo polje i prihvata mask:
Dim mtbc = New MaskedEditColumn
mtbc.Mask = "00:00"

CustomDataGridView1.Columns.Add(mtbc)
CustomDataGridView1.Columns(14).Name = "Test"
CustomDataGridView1.Columns(14).HeaderText = "TEST"

Ali kada hoću da promenim u DataGrid-u (CustomDataGridView1.Columns(13))kao na slici, mask ne radi na polju.
↑  ↓

#7 11.01.2019 15:43
Gjoreski Van mreze
Administrator
Registrovan od:02.02.2009
Postovi:1,854


Predmet:Re: DataGridView - masked field text to time
Ja nikad ne koristim ovaj grid imam ja drugi grid koj koristim ali on ne radi na vbNet.

Kako ja vidim na slike ti pravis program za evidencija neki vozila.
Ne znam zasto kompliciras svoj posoa i pravis unos u Gridi a mozes staviti par polja na fori a u gridu samo da vidis unosene zapisa.
↑  ↓

#8 14.01.2019 12:28
xl_kid Van mreze
Clan
Registrovan od:11.11.2008
Postovi:154


Predmet:Re: DataGridView - masked field text to time
Da, aplikacija je za kontrolu vozila i sadrži:
- vozač 1 - ime i prezime
- vozač 2 - ime i prezime (ako su bila 2 vozača)
- relacija - teren na kome je išlo vozilo
- početna KM (polazak)
- završna KM (povratak)
- gorivo - natočeno litara
- vreme polaska - (polazak)
- vreme povratka - (povratak)

Ima viÅ¡e vozača, terena i naravno vozila u toku dana. Imam ovu aplikaciju u ms access-u i radi lepo i laka je koriÅ¡ćenje. Manje viÅ¡e sve radi na enter, minimalno koriÅ¡ćenje miÅ¡a (klik ili dva). Ako bih stavio unos u formi a pregled u DataGrid-u bilo bi nepregledno (malo). Gore unosiÅ¡ dole kontroliÅ¡eÅ¡. Možda ima neko bolje reÅ¡enje ali je ovako jednostavnije. Možda smo navikli tako da radimo pa zbog toga. U svakom slučaju držimo se ovakvog načina unosa. Ova trenutna aplikacija radi super (viÅ¡e korisnika, LAN) jedino Å¡to nam je problem (za lakÅ¡i i brži unos) su dve tačke ":" kada treba da se unese vreme. Ali i to je reÅ¡ivo kao Å¡to si poslao kod. Samo da nađem načina da ga povežem sa poljem.

U svakom slučaju, joÅ¡ jednom, hvala ti na pomoći i vremenu. Puno si mi pomogao
... a i devojkama koje unose ove podatke Smiling
↑  ↓

#9 14.01.2019 14:23
Gjoreski Van mreze
Administrator
Registrovan od:02.02.2009
Postovi:1,854


Predmet:Re: DataGridView - masked field text to time
daj postavi baza pa da vidimo gde si zapeo , ovaku ne mogu nista .
I
↑  ↓

Stranice (1):1


Sva vremena su GMT +01:00. Trenutno vrijeme: 1: 59 am.