VB - How to get datagridview column to auto-format color? - visual-studio-2010

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?

Related

Proper Casing in VB6

I am doing a quiz game in VB6. I need the textbox to automatically capialize the first letter but this code
Private Sub Anstxt_Change()
Anstxt.Text = StrConv(Anstxt.Text, vbProperCase)
End Sub
causes the word to invert. So instead of "Trees" it turns into "Seert"
How do I change this?
Pay attention to where the cursor is positioned in the textbox when the event Change occurs: its at the start of the textbox. Add a Debug.Print statement to see what's going on while you type:
Private Sub Anstxt_Change()
Debug.Print StrConv(Anstxt.Text, vbProperCase)
Anstxt.Text = StrConv(Anstxt.Text, vbProperCase)
End Sub
The output looks like
T
T
Rt
Rt
Ert
Ert
Eert
Eert
Seert
Seert
Two things to notice here: the Change event is triggered twice: once from typing and once from changing the value of the textbox within the Change event. That gives you an idea that manipulating the text of a textbox in its Change event isn't a good idea. I suggest to put this code in the LostFocus event instead.
The second thing to notice is that as the cursor is always at the start of the textbox, the letters you type are inserted there in front of the existing letters. So after you change the .Text property of the textbox, you should position the cursor at the end of the textbox with the .SelStart method:
Anstxt.SelStart = Len(Anstxt.Text)
e.g.
Private Sub Anstxt_Change()
Anstxt.Text = StrConv(Anstxt.Text, vbProperCase)
Anstxt.SelStart = Len(Anstxt.Text)
End Sub

I want my code to show me 4 form2 when i press button2 but it wont why?

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For i As Integer = 0 To 4
Me.Hide()
Form2.Show()
i = i + 1
Next
End Sub
'This is my code with problem
I used for loop but the form2 only appears once so i have to keep pressing the button
It sounds like what you want is 4 different instances of your form to appear. (After all, the same form can't "appear 4 times" because once it's already visible, it's already visiblt.) For that you'd need 4 instances of your form. Something like this:
Private form2Instances As New List(Of Form2)
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For i As Integer = 0 To 4
Dim form2 As New Form2()
form2Instances.Add(form2)
form2.Show()
i = i + 1
Next
Me.Hide()
End Sub
The idea here is that your class is maintaining a collection of Form2 instances, and click your button essentially does a few things:
Populates that collection with those instances. (As it's assumed that you'll want to reference them later, outside the scope of this click handler.)
Shows each of those instances.
Hides the current form (which only needs to happen once, outside the loop).

Event Handlers for Dynamic Table Layout Panel in Visual Basic

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 ?)

Add/remove items from array that goes into combobox

This is what needs to be done.
This project / assigenment will maintain a list of movie titles in an array. The user can add and remove movie titles from the list, clear and print the list.
The form will contain a combo box initialized with the following entries: A Dependent's Pay, An American Resident, Dancing With Fools, Home By Yourself, and Ghost Man.
There will be six buttons on the form. An add button that will add an entry to the list using an input box. The user may not add a duplicate title to the list. A remove button that will remove the selected item off the list. An error is displayed if the user tries to add a blank title or remove a title without first selecting one. A clear all button will remove all the entries from the list. A count button will display in a message box the number of movie titles can be displayed. The print button will display the ‘Movie List’ report in a print preview window. Include your name on the report. And finally, an exit button.
This is what I have so far:
Public Class frmlist
Dim movielist(100) As String
Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub frmlist_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
movielist(0) = "A Dependent's Pay"
movielist(1) = "An American Resident"
movielist(2) = "Dancing With Fools"
movielist(3) = "Home By Yourself"
movielist(4) = "Ghost Man"
cboxlist.Items.AddRange(movielist)
End Sub
Private Sub AddToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AddToolStripMenuItem.Click
movielist(5) = InputBox("Enter a movie title: ", "Movies")
cboxlist.Items.AddRange(movielist)
End Sub
End Class

ModalPopupExtender + ASP.NET AJAX: Can't page grid

I'm trying to page and sort my DataGrid which is inside a ModalPopupExtender but I can't page it in any way, already tried with <Triggers>, put the UpdatePanel inside, outside, in the middle, and it does NOT work. Modal popup does not get closed but the grid just disappears. Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
SqlServerDS.SelectCommand = "SELECT * FROM emp WHERE name LIKE '%" & txtSearchName.Text & "%'"
BindData()
End Sub
Private Sub BindData()
grdSearch.DataSource = SqlServerDS
grdSearch.DataBind()
End Sub
Private Sub grdBuscaPaciente_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdSearch.PageIndexChanging
grdSearch.PageIndex = e.NewPageIndex
BindData()
End Sub
Inside the Designer, this is the code h:
<modalpopupextender>
</modalpopupextender>
<panel>
<updatepanel>
<gridview>
</gridview>
</updatepanel>
</panel>
Tommy is right, so what you just have to do is re-show the popup.
After the BindData() in the PageIndexChanging event show the panel again with the Show() method of the popup extender.
This code is in c# but is pretty much the same.
gvHorarioPB.DataSource = (DataTable)Session["Prueba"];
gvHorarioPB.PageIndex = e.NewPageIndex;
gvHorarioPB.DataBind();
//mpePB is my modalpopupextender
this.mpePB.Show();
If you are using the .NET AJAX toolkit, keep in mind that each time you click someting (paging, sorting, etc.), the page performs a postback, even if it looks AJAX-y. Meaning that you will need to rebind the data each time. Try removing the IfPostback in your page load and see what that does for you.

Resources