I am creating an application in vb.net. I have a datagridview control in my VB form. I need to view it on printpreview window with the contents in it. I have other control like labels and textboxes in the form and I can view all in printpreview. In the case of Datagridview control, I have a working printpreview code which I got from net. My problem is, I need to change the x and y positions of datagridview control. With the following code, the datagridview control is displaying over the other controls. I dont know how to do it in this code. Please help me.
I need to change the x and y positions of DataGridView like given in the below code(50 and 225).
e.Graphics.DrawString(Label7.Text, Label7.Font, Brushes.Black, 50, 225)
The code I used to display gridview is given below.
Code :
Dim ColumnCount As Integer = DataGridView1.ColumnCount
Dim RowCount As Integer = DataGridView1.RowCount
Dim CellTopPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Top
For Row = 0 To RowCount - 2
Dim CellLeftPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Left
For Cell = 0 To ColumnCount - 1
Dim CellValue As String = DataGridView1.Rows(Row).Cells(Cell).Value.ToString()
Dim CellWidth = DataGridView1.Rows(Row).Cells(Cell).Size.Width + 10
Dim CellHeight = DataGridView1.Rows(Row).Cells(Cell).Size.Height
Dim Brush As New SolidBrush(Color.Black)
e.Graphics.DrawString(CellValue, New Font("arial", 9), Brush, CellLeftPos, CellTopPos)
e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, CellTopPos, CellWidth, CellHeight)
CellLeftPos += CellWidth
Next
CellTopPos += DataGridView1.Rows(Row).Cells(0).Size.Height
Next
Use this class .. unfortunately there is characters limit so i can not able to post code
http://www.codeproject.com/Articles/18042/Another-DataGridView-Printer
Related
I want to call a random color between 8 different colors and display it in a label as its backcolor in Visual Basic. How can I display the colors without repeating a color that has been called out on a specific label?
For example, if color red is called out and displayed in labelA1, how can I make sure that color red won't be called out and displayed in labelB1, labelC1 or labelD1 but can be called out in labelA13 or labelB16?
Below is a picture to help understand the example above.
Use this code to make a list of colors then knock them off the list every time one is used.
Private Function RandomizeLabelColors() As Integer
Randomize()
Dim listOfColors As List(Of Color) = {Color.Red, Color.Blue, Color.Green, Color.Orange}.ToList
Dim labels As List(Of Label) = {Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8,
Label9, Label10, Label11, Label12, Label13, Label14, Label15, Label16}.ToList
Dim i As Integer = 0
Do Until listOfColors.Count = 0
Dim targetIndex As Integer = Int(Rnd() * listOfColors.Count)
labels(i).BackColor = listOfColors(targetIndex)
labels(i + 4).BackColor = listOfColors(targetIndex)
labels(i + 8).BackColor = listOfColors(targetIndex)
labels(i + 12).BackColor = listOfColors(targetIndex)
listOfColors.RemoveAt(targetIndex)
i += 1
Loop
Return 0
End Function
I have my labels in a 4x4 grid.
-Mg
I'm a bit in need for some help. I'm trying to scroll a PDF in Patagames PDFium .net control using the keyboard. Unfortunately I can't get it to scroll correctly and am looking for some sample code (VB or C#) to scroll 'line wize (lets say 10 pixels per keypress)' and 'page wize'. Can anybody help me out here? TIA a lot. Ole
Never mind. Found an answer.
Private Sub PdfViewer1_KeyDown(sender As Object, e As KeyEventArgs) Handles PdfViewer1.KeyDown
Dim pageStep As Integer = PdfViewer1.AutoScrollMinSize.Height / PdfViewer1.Document.Pages.Count
Dim lineStep As Integer = pageStep / 30
Dim asp As Point = PdfViewer1.AutoScrollPosition
Select Case e.KeyCode
Case Keys.Up
PdfViewer1.AutoScrollPosition = New Point(asp.X, -asp.Y - lineStep)
Case Keys.Down
PdfViewer1.AutoScrollPosition = New Point(asp.X, -asp.Y + lineStep)
Case Keys.PageUp
PdfViewer1.AutoScrollPosition = New Point(asp.X, -asp.Y - pageStep)
Case Keys.PageDown
PdfViewer1.AutoScrollPosition = New Point(asp.X, -asp.Y + pageStep)
End Select
End Sub
I'm a beginner in VB6.
I am creating an application that contains a TDBGrid.
My problem is how can i get the row selected in this TDBGrid.
As you said you have changed to dbgrid then you can do below
Dim row As GridViewRow = GridView1.SelectedRow
Dim CellValue = row.Cells(1).Text
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 ?)
i'm tring to create some label programmatically, the code doesn't return any error but i cannot see any label in my window.
dim dr As DatabaseRecord
dim sql As String
sql = "SELECT * FROM pack WHERE applicabilita_modello LIKE '%" + versione + "%'"
dim rs As RecordSet = database.SQLSelect(sql)
dim i As Integer = 1
dim test(10) As Label
while not rs.EOF
test(i) = new Label
test(i).Text = rs.Field("descrizione").StringValue
test(i).Left = me.Left
test(i).Top = me.Top * i
test(i).Enabled = true
test(i).Visible = true
rs.MoveNext
i = i + 1
wend
rs.Close
i've verified that the recordset contain some data, the loop work correctly but no label is shown and cannot understand why.
thanks for any help
There are two ways to create controls at runtime in Real Studio. The first is to create a control array. You could name the control MyLabel and give it an index of zero. Then your code would be:
test(i) = new MyLabel
The second is to use a ContainerControl. This container would contain a label and because you can add them to your window (or other container) using the NEW command and using the ContainerControl.EmbedWithin method.
I generally prefer the ContainerControl approach for many reasons, but mostly because control arrays make the logic a big more convoluted. The only drawback with containers is that it requires Real Studio Professional or Real Studio Enterprise.
http://docs.realsoftware.com/index.php/UsersGuide:Chapter_5:Creating_New_Instances_of_Controls_On_The_Fly
http://docs.realsoftware.com/index.php/ContainerControl