Extend the properties of datagridview and its cell - vb.net

I am working on the datagridview now days. And I have to assign some custom properties to the datagridview which I am able to do. problem comes when I want to extend the properties of the cell. for example I already have my custom textbox control which user can set the behaviour like if its numeric or alphanumeric, allow negative, allow decimals etc etc. which works fine. Now I want to include that textbox control in my extended grid. So user can set all those properties while adding columns.
First of all is it possible? If yes then any tutorial or help please.
thanks in advance.

I'm not fully certain so you may want to keep looking, but as far as I know you have to manually set each property of the text box to what you want. There isn't a copy all cell style settings to textbox call you can make.
Public Sub funwithDGVs()
Dim DataGridView1 As New DataGridView
Dim DataGridViewCell1 As DataGridViewCell
DataGridViewCell1.ForeColor = Color.Aquamarine
DataGridView1.Item(0, 1).Style.ForeColor = DataGridViewCell1.ForeColor
TextBox1.ForeColor = DataGridViewCell1.ForeColor
End Sub

Related

Conditional visibility on MS Access Form - how to write in VBA or Macro

I have some very (very) basic MS Access knowledge. I'm trying to expand a bit into either VBA or macros as I'd like to put in some conditional visibility for my form. Basically, I have a checkbox. If it's checked, I want three or four more fields to pop up. Someone was able to point me to a basic VBA formula of if (this checkbox) = true then, (fieldx).visible = true, else, (fieldx).visibility = false, end if.
But I'm so new to this that I need more help and explanation. I tried putting it in but couldn't get it to work (no error message, just nothing changed at all).
Specific questions:
-Does this formula seem right?
-If I want multiple fields to be visible, can I combine them into one formula or should I create a new "if" statement for all?
-Where do I enter this code? I'm running the Office 365 version. For all I know, I'm not even putting it in the right place.
-How do I determine the field names to replace the (this checkbox) and (fieldx) in the formula? I tried entering the name I title the fields as, but with the spaces in the name I got an error message, and without the spaces nothing happened. Is there a specific naming convention to turn the field names into formula-appropriate titles? Is the name listed somewhere?
-Once I get the formula entered, is there something I have to do to get it to run/take effect? I tried saving, closing and reopening with no changes.
-Is this the best way to go about this?
If there's anything else you think I should know, I would love to hear it - but please keep in mind I'm very new to this so if you could keep it at "dummy" or ELI5 levels of explanation, I'd appreciate it!
after creating a form with 4 textboxes and a checkbox put the form in design mode (lower right corner has design mode selected, select a textbox and hit property sheet on the ribbon (or f4).
On the property sheet note the visible property. set the visible property to false. Now the textbox will be invisible when the form starts.
Tip you can select all the textboxes at the same time and set their properties all at once.
Every control on the form and even the various parts of the form have properties you can set and play with. For instance you can give any name you want to any control. On the property sheet go to the other tab and set the name property.
Tip: choose a name you you will remember without having to look it up and describes the controls function.
Next select the checkbox (not the checkbox's label). On the property sheet go to the event tab and select the on click event. hit the ellipsis and choose code builder. Access is Event Driven. We want the textboxes to appear when the checkbox is selected so we put that code in the checkbox click event.
after choosing code builder we get the code window where we can browse among all the events for all our forms. for now all you should see is something like:
Private Sub mycheckbox_Click()
End Sub
So insert some code to handle the checkboxes like:
Private Sub mycheckbox_Click()
If mycheckbox = True Then
txtbox1.Visible = True
txtbox2.Visible = True
txtbox3.Visible = True
txtbox4.Visible = True
Else
txtbox1.Visible = False
txtbox2.Visible = False
txtbox3.Visible = False
txtbox4.Visible = False
End If
End Sub
now when the checkbox is not checked no textboxes are visible.
but when the checkbox is checked they appear

MS ACCESS VBA - best way to change all properties values of a form's textboxes at once

I have a form with 3 textboxs and i want to change their properties values according to some events.
My idea is to set these properties to a global form's variables in order to use them whenever i want with a function or module (this depends on your suggestion).
Assuming that i want to change all textboxs.enabled property by pressing a button
and i want to change another different property if needed in the future (like bordercolor),
my example is this:
in the form VBA,
Option Compare Database
Option Explicit
Private Const AllTxtboxes = *** all txtboxes ***
in the VBA button
Private Sub button1_Click()
AllTxtboxes.enabled = True
End sub
Thanks in advance.
You need to loop trough all controls and identify those you want to change. The problem is that each control type got their own properties, so you need to make sure you are in the right one.
And for that, there is a property common to all controls named Tag
There, you can specify a value and check it. It's really helpful to select only what you want.
I made a simple form with 6 textboxes:
Note that in design view, properties panel at right, tab others, last property is Tag (sometimes it comes as aditional info or something like it).
There, I typed 99 but only on textboxes 2, 4 and 6, because those are the textboxes I want to handle (that would be kind of your array of textboxes)
I added a command button to change forecolor of those textboxes to red when clicked. My code is:
Private Sub CMD_CHANGE_FONT_COLOR_Click()
Me.Painting = False
Dim MyControl As Control
Dim MyTxt As TextBox
For Each MyControl In Me.Controls
If MyControl.Tag = 99 Then
'we set MyTxt to MyControl so we can use Intellisense, not really needed, but it makes coding easier
Set MyTxt = MyControl
MyTxt.ForeColor = vbRed
Set MyTxt = Nothing
End If
Next MyControl
Me.Painting = True
End Sub
When the form loads, you will see:
But after click on command button, the forecolor of those 3 textboxes will be red:
There you go. So everytime you want to do something with that array of textboxes, you can use this code to loop and change the properties you want.

VB.net checking if active control is specific DGV

I need to check if my Current DGV is a specific datagridview in my program named DgvDiameterData, so how do I preform this check?
I got this code which sets the active current DGV which works but the check doesnt and no idea how to get it to work
Dim CurrentDGV As DataGridView = ActiveControl
If CurrentDGV = DgvDiameterData Then 'the part after the = doesnt work
End If
Have a look at the Is Operator.
From the link:
The Is operator determines if two object references refer to the same object
So you need:
If CurrentDGV Is DgvDiameterData Then
This is less foolproof but you could also check the Name property.
If CurrentDGV.Name = DgvDiameterData.Name Then

Custom Design of an Excel Textbox VBA

Well I was wondering whether we can somehow create custom looking text boxes that act as an input box and is linked to VBA.
As far as I am aware the standard procedure would entail adding an ActiveX Textbox Control and then using the TextBox1_Change event to add the code as to what needs to happen when the user enters something in to.
Sadly the look of the default textbox isn't matching the way I want by spreadsheet to look. So is there any way to change how it looks or have something replace it while serving the same purpose?
One thing I could think of and have tried is inserting a shape (blue):
Shape http://im52.gulfup.com/qD2F0B.png
I can get the text that is in the shape using VBA by:
InputText = Shapes("Rounded Rectangle 1").TextFrame.Characters.Text
But I don't suppose there is a way to detect a change of shape text event?
Suggestions / Workarounds are welcome!
Thanks
There are limitations on what you can change on an ActiveX TextBox, such as Font/Color/Border/SpecialEffects, but the basic rectangle shape cannot be changed.
However you can make the TextBox transparent by BackStyle property and group it to a shape (bring the TB forward) and still use the TextBox1_Change method for changes.
If you need to access the value in the TextBox somewhere else, a quick way is to use TextBox1.LinkedCell and below to set the value to a cell, or a Named Range.
Private Sub TextBox1_Change()
' Same Sheet as TextBox1
ActiveSheet.Range(TextBox1.LinkedCell).Value = TextBox1.Value
' Or Below for Named Range
ThisWorkbook.Names(TextBox1.LinkedCell).RefersToRange.Value = TextBox1.Value
End Sub

Setting the focus in a datagridview in windows form

I have a datagridview that accepts a list(of myObject) as a datasource. I want to add a new row to the datagrid to add to the database. I get this done by getting the list... adding a blank myObject to the list and then reseting the datasource. I now want to set the focus to the second cell in the new row.
To CLARIFY i am trying to set the focus
You can set the focus to a specific cell in a row but only if the SelectionMode on the DataGridView is set to CellSelect. If it is, simply do the following:
dataGridView.Rows[rowNumber].Cells[columnNumber].Selected = true;
In WinForms, you should be able to set the
Me.dataEvidence.SelectedRows
property to the row you want selected.
In Visual Studio 2012 (vb.NET Framework 4.50), you can set the focus on any desired cell of a DataGridView control.
Try This:
Sub Whatever()
' all above code
DataGridView1.Focus()
DataGridView1.CurrentCell = DataGridView1.Rows(x).Cells(y) 'x is your desired row number, y is your desired column number
' all below code
End Sub
Okay, that works for me. I hope that it works for you, too.