Folks I would like to set a datagridview to accept only one row and I'm having a hard time doing that.
Means after the first row disallow the user to add a new row.
So the datagridview will always have one row. In Visual Basic though
Any help would greatly appreciated.
Private Sub Datagridview1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DatagridviewCellCancelEventArgs) Handles DataGridView.CellBeginEdit
For Each r As DataGridViewRow in DataGridView1.Rows
If r.IsNewRow AndAlso DataGridView1.CurrentRow.Index = 0
e.Cancel = False
Else
e.Cancel = True
End If
Next
End Sub
Related
My DataGridView have some columns I want to turn a different color to indicate that they are auto-fill columns and does not need user input.
So I created the code to color columns:
Private Sub autocolumn()
Dim auto_cell As New DataGridViewCellStyle
auto_cell.BackColor = Color.LightSteelBlue
ProjTable.Columns(3).DefaultCellStyle = auto_cell
ProjTable.Columns(5).DefaultCellStyle = auto_cell
ProjTable.Columns(9).DefaultCellStyle = auto_cell
ProjTable.Columns(10).DefaultCellStyle = auto_cell
End Sub
And then place it into form Load event
Private Sub projectentry_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
autocolumn()
End Sub
On testing the form, the first row is colored no problem. Issue is on the 2nd row the color is white. Then the 3rd row its colored, and thee following white. And it follows this repeated pattern.
I even tried to make a DataGridView Rows Added event to call the sub
Private Sub color_load() Handles ProjTable.RowsAdded
autocolumn()
End Sub
Again same result. What may be the problem here?
I need to create many dropdownlists at runtime and select a different item for each one.
To avoid accessing the DB continuously I create a single dropdownlist of which I copy the items in the cloned dropdownlists. Strangely all the dropdownlists created select the item of the last dropdownlist and I don't understand why! I had to insert the cleaning of the selected items ["DlistClone.ClearSelection ()"] because otherwise the code goes wrong. Can anyone help me? thanks
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyRow As New TableRow
Dim MyCell As New TableCell
Dim DList As DropDownList
Dim DlistClone As DropDownList
DList = New DropDownList
DList.Items.Add("1")
DList.Items.Add("2")
DList.Items.Add("3")
MyRow = New TableRow
MyCell = New TableCell
DlistClone = New DropDownList
ClonaDList(DList, DlistClone)
DlistClone.ClearSelection()
DlistClone.Items.FindByText("3").Selected = True
MyCell.Controls.Add(DlistClone)
MyRow.Cells.Add(MyCell)
Table1.Rows.Add(MyRow)
MyRow = New TableRow
MyCell = New TableCell
DlistClone = New DropDownList
ClonaDList(DList, DlistClone)
DlistClone.ClearSelection()
DlistClone.Items.FindByText("2").Selected = True
MyCell.Controls.Add(DlistClone)
MyRow.Cells.Add(MyCell)
Table1.Rows.Add(MyRow)
End Sub
Sub ClonaDList(ByVal Origine As DropDownList, ByVal Destinazione As DropDownList)
Dim I As Integer
Dim EleList As ListItem
For I = 0 To Origine.Items.Count - 1
EleList = Origine.Items(I)
Destinazione.Items.Add(EleList)
Next I
End Sub
Ok, I got it myself!
When I do
EleList = Origin.Items (I)
Destination.Items.Add (EleList)
I create an inadvertent binding between the source Dropdownlist and the destination one.
To interrupt the binding I must necessarily use a bridge between the two controls, in this case I use the Listitem object by setting the 2 properties in 2 successive steps
Sub ClonaDList(ByRef Origine As DropDownList, ByRef Destinazione As DropDownList)
Dim I As Integer
Dim EleList As ListItem
For I = 0 To Origine.Items.Count - 1
EleList = New ListItem
EleList.Text = Origine.Items(I).Text
EleList.Value = Origine.Items(I).Value
Destinazione.Items.Add(EleList)
Next I
End Sub
Did I say it right?
I have a local sub that allows the user to move a row of a datagridview, triggered by a button click. The sub works fine in debugger but when it exits control is transfered to the calling form, i.e. the current form is closed. This also happens when no row is moved, i.e. when one of the abort conditions on entrance are met. Simply: exiting this sub will close the form!?!
Private Sub btnMove_Click(sender As Object, e As EventArgs) Handles btnMove.Click
Dim rowToGo As DataGridViewRow
Dim rtgIndex As Integer = 0
If (dgvAuftrag.RowCount <= 1) or (dgvAuftrag.CurrentRow Is Nothing) Then
Beep()
Exit Sub
End If
rowToGo = dgvAuftrag.CurrentRow
rtgIndex = rowToGo.Index + 1
If (rtgIndex >= dgvAuftrag.RowCount) Then rtgIndex = 0
Try
dgvAuftrag.Rows.Remove(rowToGo)
dgvAuftrag.Rows.Insert(rtgIndex, rowToGo)
Catch ex As Exception
IssueErrorMessage(ex)
End Try
End Sub
All other local subs and functions work normal, just this one behaves strange. Any ideas how to fix/avoid this bug?
This is not a solution of the problem but a functioning workaround based on the sugestion of Hans. I have introduced a global boolean var named OKtoExit which is intialized to false.
private OKtoExit as boolean = false
Then I have a new FormClose event handler that checks that var. If OKtoExit is false then e.Cancel = true and the handler exits. The regular Exit functions (Save and Quit) set OKtoExit to true, any other code leaves the values unchanged.
Private Sub Current_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If Not exitOK Then
e.Cancel = True
Exit Sub
End If
End Sub
As I said, this is just a workaround that has the same effect as a normal functioning VB-Code. I would appreciate if somebody could present a real solution!
After many months I discovered the true reason for the problem and I must give all credits to Hans Passant: I had a button on one of the first forms that had the Dialog Result property set to Cancel. This was a really beautyful button so many other buttons in the appliaction were a copy of this first button where I just modified the label. Thus they all led to the unwanted behavior that a form was closed as soon as a user clicked one of them no matter what the label said... After months I discovered that just by chance. Thanks to Hans again, I obviously overlooked his last hint "And look at the button's DialogResult property."!
I am making a risk-type game for school that dynamically creates a 4x4 grid of buttons inside a table layout panel in visual basic. I have successfully created the panel and buttons with names that correspond to the row and column of the button. There are also two parallel arrays - one for button owner and the other for button number - that correspond to the owner of the button and the number of "armies" in the button. My issue is that when the user clicks a certain button, I need to reference the button name/value to know how many "armies" the button has to control the "attack" portion of the player's turn.
The following code creates the table layout panel and the buttons with names.
'Create table Dynamically
Dim ColCount As Integer = 4
Dim RowCount As Integer = 4
Dim f As New System.Drawing.Font("Arial", 15)
riskTable.AutoScroll = True
riskTable.Dock = DockStyle.Fill
riskTable.ColumnCount = ColCount
riskTable.RowCount = RowCount
For rowNo As Integer = 0 To riskTable.RowCount - 1
For columnNo As Integer = 0 To riskTable.ColumnCount - 1
Dim buttonname As String
buttonname = "B" & rowNo & columnNo
Dim button As Control = New Button
button.Size = New Size(179, 100)
button.Name = buttonname
button.Text = "1"
button.ForeColor = Color.White
button.Font = f
AddHandler button.Click, AddressOf buttonname_Click
riskTable.Controls.Add(button, columnNo, rowNo)
Next
Next
Me.Controls.Add(riskTable)
This is the dynamic event handler that I created. I tried using 'Me.Click' to get the name of the button, but it only returns the name of the form. I need to have code in here that references the name of the currently clicked button to then in turn reference the box owner and box number arrays.
Private Sub buttonname_Click(sender As Object, e As EventArgs) Handles Me.Click
MessageBox.Show(Me.Name)
End Sub
Any help would be greatly appreciated! I think that once I get this working, the rest of the game will be pretty simple to figure out.
Thanks!
Put the name in 'button.Tag' instead/also:
button.Tag = buttonname
Then it is easy to get the name with:
Private Sub buttonname_Click(sender As Object, e As EventArgs) Handles Me.Click
Dim result As String = CType(CType(sender, System.Windows.Forms.Button).Tag, String)
End Sub
(Check the System.Windows.Forms.Button though, might need some tweak to match your buttons inside the table. riskTable.Controls.button ?)
Using VB6
Listview
ID Name
001 Raja
002 Ramu
003 Sajee
..
…
Code
Private Sub listview1_DblClick()
If Not (listview1.SelectedItem Is Nothing) Then
Textbox1.text = listview1.selectedItem(0)
Textbox2.text = listview1.SelectedItem(1)
End If
End Sub
Above code is not showing the values in the text box
How to show the list view row values in the text box.
Need VB6 Code Help
The ListView SelectedItem property does not return a collection of items selected on your ListView, so therefore you can't explicitly get the first selected item, the second selected item, etc. You will need to loop through all ListItems in your ListView and check if each is selected. If it is, do what you want to do.
One problem I see with your sample code is you're using the ListView DblClick event. I might be wrong, but it looks like whenever it fires only one ListView item can be selected (the one that fired the event). A solution for this is to put your code into a new procedure. Here's one that should work:
Private Sub GetSelectedItems()
' Make sure exactly two items are selected on our ListView.
If (CheckListViewSelectedItemCount(listview1, 2)) Then
Dim blnFoundFirstItem As Boolean
blnFoundFirstItem = False
Dim i As Integer
' Find out which items are selected.
For i = 1 To listview1.ListItems.Count
If (listview1.ListItems(i).Selected) Then
' Assign the Text of the 'first' selected item to Textbox1.Text.
If (Not blnFoundFirstItem) Then
Textbox1.Text = listview1.ListItems(i).Text
blnFoundFirstItem = True
' Assign the Text of the 'second' selected item to Textbox2.Text.
Else
Textbox2.Text = listview1.ListItems(i).Text
End If
End If
Next i
Else
MsgBox "You need to select two items."
End If
End Sub
I'm not sure in which order ListItems are iterated through in my For loop. It's possible that what would be assigned to Textbox1.Text in my code you might want to assign to Textbox2.Text.
Your code required at that at least two items are selected on the ListView. I don't know if VB6 has a way to return the number of selected items so I wrote a small function that does this:
' Return True if the passed ListView control has a number of selected items that's equal to the intExpectedItemCount parameter.
Private Function CheckListViewSelectedItemCount(listView As listView, intExpectedItemCount As Integer) As Boolean
Dim intSelectedItemCount As Integer
intSelectedItemCount = 0
Dim i As Integer
For i = 1 To listView.ListItems.Count
If (listView.ListItems(i).Selected) Then
intSelectedItemCount = intSelectedItemCount + 1
End If
Next i
CheckListViewSelectedItemCount = (intSelectedItemCount = intExpectedItemCount)
End Function
I dont have vb6 at hand and its been a while since I used it, but if memory serves me right:
ListView1.SelectedItem will return you a ListViewItem which gives you a Text property along with SubItems property that gives you access to associated columns as an array.