GridView Size Problem? - vb.net

Using VB.NET 2008
Am using Datagridview in my application, Datagridview should display according to the windows screen size, Before I used vb6
code.
Private Sub Form_Resize()
On Error Resume Next
If Me.WindowState = vbMinimized Then
Exit Sub
End If
listview1.Top = 1550
listview1.Left = 0
If ScaleHeight > 1550 Then
listview1.Height = ScaleHeight - 1550
End If
listview1.Width = ScaleWidth
End Sub
Am new to vb.net, How to set a datagridview size according to windows screen size, In Datagridview property itself any option is available or i have to make a code as like vb. If i have to make a code, how to give form_resize in vb.net.
Need vb.net code Help.

I'm not sure I understand your question, but I'll give it a try. This should be pretty simple. You set the DataGridView size using the Size property.
If you want it to fill the entire window you would say something like this:
Private Sub frmBar_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
If (Me.WindowState = FormWindowState.Minimized) Then
Exit Sub
End If
dataGridView.Location = New Point(0, 0)
dataGridView.Size = Me.Size - New Size(4, 30)
End Sub
But you can make it whatever size you want. All you have to do is change what you are setting for the dataGridView.Size property.

Related

vb.net datagrid to datagridview winforms

We work with vb.net - Visual Studio 2013 (WinForms)
We are changing datagrids to datagridviews in our code.
At the moment I'm trying to convert a column that was described in the datagrid as follows:
Dim grdcolstyle5 As New PTSoft.FrameWork.DataBrowserTextColumnColorDecimal
With grdcolstyle5
.HeaderText = "text"
.MappingName = "vrd_199"
.Width = 80
.FormatString = "###,###,##0.00"
.[Operator] = "<"
.ParameterCol = 2
.ForeBrush = New SolidBrush(Color.Red)
End With
What is does is compare the value of one column with another and color the text when it's smaller (in this case).
I know how to do it in a loop after the grid has been filled, but this slows things somewhat down.
Does anyone know how this can be done "on the fly", on the moment the row is filled?
In short I am looking for the equivalent for the datagridview...
You can use RowPrePaint event, where you have large number of rows to paint.
Private Sub dataGridView1_RowPrePaint(ByVal sender As Object, ByVal e As DataGridViewRowPrePaintEventArgs)
If Convert.ToInt32(dataGridView1.Rows(e.RowIndex).Cells(2).Text) < Convert.ToInt32(dataGridView1.Rows(e.RowIndex).Cells(5).Text) Then
dataGridView1.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Red
End If
End Sub
The above code is comparing Cells(2) against Cells(5). You can customize that as per your need.
Another option is using CellFormattingEvent
Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs)
For Each Myrow As DataGridViewRow In dataGridView1.Rows
If Convert.ToInt32(Myrow.Cells(2).Value) < Convert.ToInt32(Myrow.Cells(1).Value) Then
Myrow.DefaultCellStyle.ForeColor = Color.Red
End If
Next
End Sub

Working with too many PictureBoxes, any way to use a For?

Warning : Im completely new to VB.net and only know the most basic form of programming, maybe even less
Visual Basic 2010 Express
I have declared an Array Equipa(x,y) as integer.
I have 30 PictureBoxes ( PictureBox1 to PictureBox30)
In my mind, I assigned each PictureBox to a X and Y in the Array.
At load form, I want to change the image of PictureBox5 to PictureBox25.
For this I copy pasted 20 times PictureBox(x).Image = My.Resources.GreyHexagon
I would like to have a Loop that can do this without me copy pasting so much.
During the program, clicking on PictureBox5, for example, changes it to a different image, depending on the value, of the Array declared.
If Equipa(0, 0) = 0 Then
PictureBox4.Image = My.Resources.GreyHexagon
ElseIf Equipa(0, 0) = 1 Then
PictureBox4.Image = My.Resources.BlueHexagon
Else
PictureBox4.Image = My.Resources.RedHexagon
End If
The problem, again, is I have to repeat this code, for every Array position, since each PictureBox is assigned to each position.
So what I need is a Loop that can go through each PictureBox, I'm not asking you to make that code, I just don't know how to go through each PictureBox, for example PictureBox5 to PictureBox25.
PS . The following code changes EVERY PictureBox to the Image I wan't. But I do not understand any of this code, therefor can't change it to only go through PictureBox5 to PictureBox25.
Me.SuspendLayout()
For Each box As PictureBox In Me.Controls.OfType(Of PictureBox)()
box.Image = My.Resources.RedHexagon2
Next box
Me.ResumeLayout()
What I tried once before: In the declarations,
Dim pic(29) As PictureBox
And then in Public Sub New, I wrote picarray() and then made a Private Sub picarray. In this sub I dimmed each picture to the array
pic(0) = picturebox1
pic(1) = picturebox2
pic(2) = picturebox3
...
pic(29) = picturebox30
In my case, I wanted to move all 30 at once, and I had a timer, so I did:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Timer1.Tick
For i = 0 To 29
pic(i).Left = pic(i).Left + 3
Next
End Sub
To move them all to the right at once. I used the same way to move them back if the last one on the left hits the form's edge.
Me.SuspendLayout()
For Each box As PictureBox In Me.Controls.OfType(Of PictureBox)()
box.Image = SomeNewPicture
Next box
Me.ResumeLayout()
If you want to be able to look at specific images, you can still just say things like PictureBox4.Image = SomeNewImage. But if you really want to access them by index, first define this variable as a member of the form class:
Private PictureBoxes() As PictureBox
And then do this in your form's Load event:
PictureBoxes = Me.Controls.OfType(Of PictureBox)().ToArray()
Now you can reference each PictureBox by index:
PictureBoxes(4).Image = SomeNewPicture
PictureBoxes(10).Image = SomeOtherNewPicture
You could also do something like this:
Me.SuspendLayout()
For i As Integer = 5 to 25
PicturesBoxes(i) = My.Resources.RedHexagon2
Next i
Me.ResumeLayout()
Finally, to address the If/Then code in your edited question, I'd do something like this:
'Note this Array shorthand requires a more recent version of Visual Studio
' I used it for brevity in the response. You should be able to convert it on your own
Dim Images() As Image = {My.Resources.GreyHexagon, My.Resources.BlueHexagon, My.Resources.RedHexagon}
Dim ImageIndex As Integer = Equipa(0, 0)
If ImageIndex < 0 OrElse ImageIndex > 1 Then ImageIndex = 2
PictureBox4.Image = Images(ImageIndex)

Resize form on listview height

I have form with only docked.full listview on it. Listview show computer drives so it's content is changeable on runtime. On listview column headers are allways visible.
I would like my form to resize with listview regarding of filled items.
This is my code:
Dim rc As Rectangle = lvDriveInfo.Items(0).GetBounds(ItemBoundsPortion.Entire)
Me.Height = (rc.Height * lvDriveInfo.Items.Count) +
SystemInformation.CaptionHeight +
SystemInformation.BorderSize.Height
But something misses here or is incorrect.
How to get exact height of listview with headers regarding on items.count and properly set height of form with this value?
ListView metrics are rather convoluted, what you are trying to do is most definitely not easy. The one thing you overlooked is the space required by the column headers. That however is not enough, ListView also requires a bit of extra elbow room at the bottom to avoid displaying the scrollbar.
The most straight-forward code that works on my machine is:
Private Sub ResizeView()
If ListView1.Items.Count = 0 Then Exit Sub
Dim last = ListView1.Items(ListView1.Items.Count - 1)
Me.ClientSize = New Size(Me.ClientSize.Width, _
ListView1.Top + last.Bounds.Bottom + 4)
End Sub
The +4 is the rub, I can't give you a warranty that this will work on every Windows version at every video DPI. It is independent of the item height so that's encouraging. But testing required to be sure. If you can guarantee that there will never be too many items in the list then you can avoid the problem by setting the list view's Scrollable property to False.
Try the following code
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lvDriveInfo.BorderStyle = BorderStyle.None
lvDriveInfo.Dock = DockStyle.Fill
With lvDriveInfo
.View = View.Details
.GridLines = True
.Columns.Add("Drive")
End With
SetFormHeight()
End Sub
Private Sub SetFormHeight()
lvDriveInfo.Items.Clear()
For Each Drive In IO.DriveInfo.GetDrives
lvDriveInfo.Items.Add(Drive.Name)
Next
Dim ListViewHeaderHeight As Integer = lvDriveInfo.Items(0).Bounds.Top
Dim ListViewRowHeight As Integer = lvDriveInfo.Items(0).Bounds.Height
Dim ListViewRowsCount As Integer = lvDriveInfo.Items.Count
Dim NewHeight As Integer = ListViewHeaderHeight + (ListViewRowHeight * ListViewRowsCount)
Me.ClientSize = New Size(Me.ClientSize.Width, NewHeight)
End Sub

How to use a ProgressBar properly in VB.NET

I have to use a progress bar in my VB.NET programs which behaves very different from those in VB6. For example, if I have a procedure to fill a datagridview through some loop and show that progress with progressbar what happend?
Datagridview fill's 100% while progressbar comes to about 50%!
Here is example program to illustrate a problem.
Create a new project, add a windows form and just copy this code on Form1's code.
Public Class Form1
Dim myMax As Integer = 100000
Dim pb As New ProgressBar
Dim dgv As New DataGridView
Dim WithEvents ti As New Timer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
With Me
.Width = 400
.Height = 250
.Controls.Add(pb)
With pb
.Maximum = myMax
.Dock = DockStyle.Bottom
End With
.Controls.Add(dgv)
With dgv
.ColumnCount = 2
.Dock = DockStyle.Fill
.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
.Visible = False
End With
End With
ti.Start()
End Sub
Private Sub OnFormLoaded(ByVal sender As Object, ByVal e As EventArgs) Handles ti.Tick
ti.Enabled = False
ti.Dispose()
Dim temp As Integer
For t = 0 To myMax
If t Mod 100 = 0 Then
pb.Value = t
pb.Refresh()
Application.DoEvents()
temp += 1
dgv.Rows.Add(New String() { _
t.ToString, _
temp.ToString _
})
End If
Next
pb.Value = myMax
pb.Visible = False
dgv.Visible = True
dgv.Focus()
End Sub
End Class
This code creates few controls, loads a form and starts a loop to fill data and show that progress in progressbar. After that program hides a progressbar and shows a datagridview what is usual situation in concrete (real-world) programs.
Problem is that although both, datagridview filling and updating a progressbar goes from same loop (in steps by 100) filling of datagridview ends much faster than progressbar show a progress and hides it on about 50%.
This is much different from VB6 where filling and showing is totally sinchronized and grid will be showed after progressbar reaches value of 100%.
How to get such functionality of progressbar in VB.NET on showed code?
I try with refresh of progressbar and DoEvents but this is not enough to get it work as expected.
In order to solve this problem without to do a "threaded science fiction" from just a ProgressBar you have to use one technique which is often with microsoft's GUI toolkits.
Such approach can probably solve your concrete problem:
If t Mod 100 = 0 Then
pb.Value = t
If pb.Value > 0 Then pb.Value -= 1
temp += 1
dgv.Rows.Add(New String() { _
t.ToString, _
temp.ToString _
})
End If

Moving .NET controls at runtime

I am attempting to move all controls on a form down or up by the height of a menubar depending on whether it is visible or not. I have code which I think ought to work well for this, however it seems that Me.Controls is empty at runtime, so my for each loop is never entered. Could someone please offer a suggestion as to how I can move the controls?
Private Sub uxMenuStrip_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxMenuStrip.VisibleChanged
For Each control As Control In Me.Controls
If control.Name <> "uxMenuStrip" Then
Dim temp As AnchorStyles = control.Anchor
control.Anchor = AnchorStyles.None
control.Top -= ((CInt(uxMenuStrip.Visible) * 2 - 1) * uxMenuStrip.Height)
control.Anchor = temp
End If
Next
Me.Height += ((CInt(uxMenuStrip.Visible) * 2 - 1) * uxMenuStrip.Height)
End Sub
Add a new Handler with Control and change location in address
Public Sub ChngPostion(ByVal sender As System.Object, ByVal e As System.EventArgs)
For Each cntrl As Control In Me.Controls
If cntrl.Name = sender.Name Then
cntrl.Location = New System.Drawing.Point(sender.Location.X,sender.Location.Y)
End If
Next
End Sub
As Michael Todd points out, Me.Controls can't be empty. Also, this might not work as well as you're thinking. Controls on WinForms apps are hierarchical. The only way to do this 100% cleanly is to make the move code recursive. IE, perform the same operation on every control in each control's controls collection. (Now I'm sounding like Dr. Seuss...) If your form is simple, this wouldn't be an issue, obviously.
At the end of the day, though, you'll probably be better off just putting everything on the form inside a Panel and just moving the Panel control explicitly by name. It would make what you're trying to do more clear.
Try this:
Private Sub uxMenuStrip_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxMenuStrip.VisibleChanged
Dim menu As Control = sender
Dim dh As Integer = IIf(menu.Visible, 1, -1) * menu.Height
For Each control As Control In Controls
If control.Parent Is Me And Not control Is menu Then
control.Top += dh
End If
Next
Height += dh
End Sub
Update:
Anyway, i strongly recomment using container, in case with MenuStrip - ToolStripContainer.