|
Hello, I am fairly new at programming, but I am learning. I am making a small application for my wife so she can record her babysitting scheduled through the week. Each day has three textboxes, the start time, the end time, and result. I need to know what to dim result as so I can insert the difference of the times entered into the textbox into the third textbox.
Public Class Form1
Dim startTime As String
Dim endTime As String
'Dim result As
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dFrom As DateTime
Dim dTo As DateTime
Dim sDateFrom As String = startTime
Dim sDateTo As String = endTime
If DateTime.TryParse(sDateFrom, dFrom) AndAlso DateTime.TryParse(sDateTo, dTo) Then
Dim TS As TimeSpan = dTo - dFrom
Dim hour As Integer = TS.Hours
Dim mins As Integer = TS.Minutes
Dim timeDiff As String = ((hour.ToString("00") & ":") + mins.ToString("00"))
'result = timeDiff
End If
End Sub
Private Sub rbM1_CheckedChanged(sender As Object, e As EventArgs) Handles rbW1.CheckedChanged, rbTh1.CheckedChanged, rbT1.CheckedChanged, rbM1.CheckedChanged, rbF1.CheckedChanged
Dim rb As RadioButton = DirectCast(sender, RadioButton)
If rb.Checked = True Then
Select Case rb.Name
Case "rbM1"
startTime = m1start.Text
endTime = m1end.Text
'result = m1result.Text
Case "rbT1"
startTime = t1start.Text
endTime = t1end.Text
'result = t1result.Text
Case "rbW1"
startTime = w1start.Text
endTime = w1end.Text
'result = w1result.Text
End Select
End If
End Sub
End Class
<pre lang="vb">
|
|
|
|
|
I dimmed result as textbox and it works great
|
|
|
|
|
You must write the result to the Text property of that TextBox. When you do
startTime = w1start.Text
you read the text from the TextBox w1start into the variable startTime .
That is a oneway operation: it happens at that one moment when that line of code gets executed. No connection of the TextBox and the variable remains (if you want to do such a thing, look for "binding").
Hence when you want to update the value shown in the TextBox after changing the value of the variable, you need to do the operation the other way round:
w1result.Text = result
Do that after you assigned a value to result .
|
|
|
|
|
Hi,
I Have a form in VB.Net Auto scroll property to true, Contains Two datagridview with 150 rows in each, lets say TopDGV & BottomDGV.
In run mode when i click any cell of BottomDGV when scroll bar is in top, it select the range of cells from BottomDGV and scrollbar gets down. In the same way if my scroll bar is in bottom and i click on TopDGV it selects the range of cell from TopDgv.
Does anyone faced this sort of issue...I googled a lot, but did not find solution on this.
Thanking in Anticipation.
Dipesh
|
|
|
|
|
This may be a silly question but do the DGVs use the same collection as a datasource?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I'm trying to sort my GridView using a User Session but whenever I run my program I get an error message saying 'Cannot find Column Name' but the table columns are correct so I don't know why I'm getting the error message.
If Not Page.IsPostBack Then
BindEmpGrid()
txtSearch.Enabled = False
Dim dt As New DataTable()
**** I'm sure the line below is whats causing the error *******
grdEmp.DataSource = dt
Session("data") = dt
End If
Protected Sub grdEmp_sorting(sender As Object, e As GridViewSortEventArgs) Handles grdEmp.Sorting
Dim dataTable As DataTable = TryCast(Session("data"), DataTable)
If dataTable IsNot Nothing Then
Dim dataView As New DataView(dataTable)
dataView.Sort = Convert.ToString(e.SortExpression) & " " & ConvertSortDirection(e.SortDirection)
grdEmp.DataSource = dataView
grdEmp.DataBind()
End If
End Sub
Private Function ConvertSortDirection(sortDirection1 As SortDirection) As String
Dim newSortDirection As String = [String].Empty
Select Case sortDirection1
Case SortDirection.Ascending
newSortDirection = "ASC"
Exit Select
Case SortDirection.Descending
newSortDirection = "DESC"
Exit Select
End Select
Return newSortDirection
End Function
|
|
|
|
|
Commish13 wrote: Dim dt As New DataTable()
**** I'm sure the line below is whats causing the error *******
grdEmp.DataSource = dt
Session("data") = dt
Well, a New DataTable is empty - it has exactly zero rows and exactly zero columns. Conseqeuently you cannot sort by columnname - it does not exist.
|
|
|
|
|
I'm trying to call 2 different Stored Procedures from 1 function that I have in a separate class page. Everything is the same Connection, SearchBy, SearchVal, etc.. except the Stored Procedures are different. Is it possible to place 2 different Stored Procedure in the code like this?
Public Shared Function SearchEmpRecords_Sp(searchBy As String, searchVal As String) As DataTable
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("strName").ConnectionString)
Dim adp As New SqlDataAdapter()
Dim cmd As New SqlCommand()
Dim cmd2 As New SqlCommand()
Dim dt As New DataTable()
Try
cmd.CommandType = CommandType.StoredProcedure
** My 2 Different Stored Procedures **
cmd.CommandText = "SearchEmpRecords_Sp"
cmd.CommandText = "SearchEmpRecords_Sp2"
cmd.Parameters.AddWithValue("@SearchBy", searchBy)
cmd.Parameters.AddWithValue("@SearchVal", searchVal)
cmd.Connection = con
adp.SelectCommand = cmd
' con.Open()
adp.Fill(dt)
Return dt
Catch ex As Exception
MsgBox(ex.Message)
Finally
adp.Dispose()
con.Close()
End Try
End Function
|
|
|
|
|
cmd.CommandText = "SearchEmpRecords_Sp"
cmd.CommandText = "SearchEmpRecords_Sp2"
What do you think will be the contents of cmd.CommandText after these two statements? You need to run your adp.SelectCommand twice, first with one command and its parameters, and second with the others.
Veni, vidi, abiit domum
|
|
|
|
|
Assuming he would concatenate the strings, would it work? I'd say that it probably would work, since one can put multiple SQL-statements in a command.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Yes of course, but not something many modern devopers use.
Veni, vidi, abiit domum
|
|
|
|
|
Commish13 wrote: Is it possible to place 2 different Stored Procedure in the code like this? Dunno; I assume you tried the code you posted. If it didn't work; what error did it throw?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
It didn't throw an error. My custom error displayed saying "* No Records Found"
|
|
|
|
|
Should there be? Btw;
cmd.CommandText = "SearchEmpRecords_Sp;"
cmd.CommandText += "SearchEmpRecords_Sp2;" That's three modifications.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
how to display the size of the camera sensor in vb.net, I use component for displaying camera avicap32.d
|
|
|
|
|
fitriadinurdin wrote: how to display the size of the camera sensor in vb.net Under WinForms, one would use a label to display information. Retrieving the size would be somewhat more difficult than displaying it.
I'm afraid I don't understand the question, can you rephrase it a bit or elaborate?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I'm having this problem, in my Windows Form App. (WFA) when the user ask an action clicking on a button, if the related field is empty an arrow should guide the user to add some value.
The problem is the following, I programmatically add this arrow img (.png) into a picturebox indicating the user where to add the missing data but the image overlaps the other controls covering lines and borders. I would like to add this image (may be still using picturebox or other) with transparent background in the way that only the arrow is shown and the remaining rectangle is transparent. I've tried with opacity property but I really don't know if it was right well coded.
I'm using just a sub to create a picturebox and place in it the arrow (or whatever) image. here the code:
Public Sub proPaintArrow()
Dim mypic As PictureBox = New PictureBox
Dim HasAlpha As Boolean
mypic.Image = Image.FromFile("C:\APP\backward.png")
mypic.BackgroundImage = Nothing
mypic.Height = 30
mypic.Width = 30
mypic.Left = txtDSFID.Location.X + 200
mypic.Top = txtDSFID.Location.Y + 10
mypic.Visible = True
mypic.BorderStyle = BorderStyle.None
mypic.SizeMode = PictureBoxSizeMode.StretchImage
mypic.BackColor = Color.FromArgb(0, 0, 0, 0)
Using I = System.Drawing.Image.FromFile("C:\APP\backward.png")
HasAlpha = (I.Flags And System.Drawing.Imaging.ImageFlags.HasAlpha) = System.Drawing.Imaging.ImageFlags.HasAlpha
End Using
mypic.Parent = Me
mypic.BringToFront()
mypic.Show()
End Sub
The call is made here:
If viTypeUpdate = 0 Then
MsgBox("Scelta non completamente effetauta" + vbNewLine + "Scegliere se 'Lock' o 'Write'", MsgBoxStyle.Exclamation, "Update TAG")
proPaintArrow()
Exit Sub
End If
The function to manage opacity is the following, but it didn't work at all.
Public Function SetImgOpacity(ByVal imgPic As Image, ByVal imgOpac As Single) As Image
Dim bmpPic As New Bitmap(imgPic.Width, imgPic.Height)
Dim gfxPic As Graphics = Graphics.FromImage(bmpPic)
Dim cmxPic As New ColorMatrix()
Dim iaPic As New ImageAttributes()
cmxPic.Matrix33 = imgOpac
iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap)
gfxPic.DrawImage(imgPic, New Rectangle(100, 300, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, iaPic)
gfxPic.Dispose()
iaPic.Dispose()
Return bmpPic
End Function
Thank you
|
|
|
|
|
jose mandurrino wrote: in my WFA app.
Which one of these do you mean[^]?
Veni, vidi, abiit domum
|
|
|
|
|
Oops!!!, it is right, updated. Thanks!!
|
|
|
|
|
WinForms wasn't built with transparancy in mind and it is not very well supported.
Easiest solution is to draw the image yourself, in the Paint-event.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
OK! Could you post some code example to give me an idea in how to proceed? Thank you.
|
|
|
|
|
Delete the control. Change the Paint handler[^] of the container that contained the control to DrawImage[^] the bitmap onto it's canvas.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
How about a different approach using an ErrorProvider[^]?
Public Class Form1
Private err As New ErrorProvider
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
err.Icon = My.Resources.warning ' you can change the icon to display if desired
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If String.IsNullOrEmpty(TextBox1.Text) Then
TextBox1.Focus()
SetError(TextBox1, "please enter something")
Else
'do something
End If
End Sub
Private Sub SetError(ByVal cntrl As Control, ByVal msg As String)
err.SetError(cntrl, msg) ' displays err icon
AddHandler cntrl.Validated, AddressOf ClearErr ' provide a handler to clear the error after leaving the control
End Sub
Private Sub ClearErr(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cntrl As Control = DirectCast(sender, Control)
RemoveHandler cntrl.Validated, AddressOf ClearErr
err.SetError(cntrl, String.Empty) ' setting the message to String.Empty clears the error
End Sub
End Class
|
|
|
|
|
TnTinMn, thank you a lot, that is perfect and for sure I will use it since now on. I'm going to change my code to using this suggestion. For what concern my question, now it is solved.
I discover also that with this suggestion or with just a picturebox, if it is inside a groupbox, the image/icon will not be show complete, it will be cut after the border. Interesting.
(However, it would be interesting to find an answer to what was the original question and make a tutorial to help beginners like me. I saw a lot of people asking the same thing in different forums.)
Thank you again
|
|
|
|
|
First off, you are welcome. Nice to see that a poster actually appreciates our efforts.
Quote: I discover also that with this suggestion or with just a picturebox, if it is inside a groupbox, the image/icon will not be show complete, it will be cut after the border. Sheesh, some people want everything.
Ok, I did a bit of playing around and found that if the target control is parented directly to the Form and not a sub container control, that the ErrorIcon will paint on top of everything. So with a little hacking, here is a custom ErrorProvider that will add a DummyControl directly to the form and post the error against that control instead.
Warning!, I have given this only minimal testing and did not include any fault detection/handling.
Maybe I will clean it up a bit when I have time and post it as Tip/Article.
Give it a go and see how it works for you. Just add this code to your project and use it place of ErrorProvider when you declare an instance.
Public Class FloatingErrorProvider
Inherits ErrorProvider
' Create a storage structure to hold the DummyControls that will
' be parented to the form that contains the control be error reported
' The ErrorProvider will display the ErrorIcon against these controls
' instead of against the source control if the source control is not
' directly parented to the form.
Private AnchorControls As New Dictionary(Of Control, DummyControl)
Private Function GetDummy(ByVal control As Control) As DummyControl
Dim dummy As DummyControl
If AnchorControls.ContainsKey(control) Then
dummy = AnchorControls(control)
Else
dummy = New DummyControl ' a zero sized, non-painting control
dummy.Parent = control.FindForm ' parent to the host form
dummy.SendToBack() ' send to bottom of Z-order
AnchorControls.Add(control, dummy) ' store a reference to it
End If
Return dummy
End Function
''' <summary>
''' sets the dummy control location
''' </summary>
Private Sub SetDummyPosition(ByVal dummy As DummyControl, ByVal control As Control)
Dim errloc As Point
Dim left As Int32 = control.Location.X
Dim right As Int32 = left + control.Width
Dim top As Int32 = control.Location.Y
Dim bottom As Int32 = top + control.Height
Dim mid As Int32 = top + (control.Height \ 2)
Dim align As ErrorIconAlignment = GetIconAlignment(control)
Select Case align
Case ErrorIconAlignment.BottomLeft
errloc = New Point(left, bottom)
Case ErrorIconAlignment.BottomRight
errloc = New Point(right, bottom)
Case ErrorIconAlignment.MiddleLeft
errloc = New Point(left, mid)
Case ErrorIconAlignment.MiddleRight
errloc = New Point(right, mid)
Case ErrorIconAlignment.TopLeft
errloc = New Point(left, top)
Case ErrorIconAlignment.TopRight
errloc = New Point(right, top)
End Select
dummy.Location = control.FindForm.PointToClient(control.Parent.PointToScreen(errloc))
End Sub
Public Shadows Sub SetError(ByVal control As Control, ByVal text As String)
Dim errcontrol As Control
If NeedsDummy(control) Then
errcontrol = GetDummy(control)
SetDummyPosition(DirectCast(errcontrol, DummyControl), control)
Else
errcontrol = control
End If
MyBase.SetError(errcontrol, text)
End Sub
''' <summary>
''' determine if control is parented directly to the form
''' </summary>
Private Function NeedsDummy(ByVal control As Control) As Boolean
Dim frm As Form = control.FindForm
Return frm.Handle <> control.Parent.Handle
End Function
#Region "Method Overrides"
Public Shadows Sub SetIconAlignment(ByVal control As Control, ByVal alignment As ErrorIconAlignment)
If NeedsDummy(control) Then
Dim errcontrol As DummyControl = GetDummy(control)
MyBase.SetIconAlignment(errcontrol, alignment)
SetDummyPosition(errcontrol, control)
Else
MyBase.SetIconAlignment(control, alignment)
End If
End Sub
Public Shadows Function GetError(ByVal Control As Control) As String
Dim errcontrol As Control
If NeedsDummy(Control) Then
errcontrol = GetDummy(Control)
Else
errcontrol = Control
End If
Return MyBase.GetError(errcontrol)
End Function
Public Shadows Function GetIconAlignment(ByVal control As Control) As ErrorIconAlignment
Dim errcontrol As Control
If NeedsDummy(control) Then
errcontrol = GetDummy(control)
Else
errcontrol = control
End If
Return MyBase.GetIconAlignment(errcontrol)
End Function
Public Shadows Function GetIconPadding(ByVal control As Control) As Int32
Dim errcontrol As Control
If NeedsDummy(control) Then
errcontrol = GetDummy(control)
Else
errcontrol = control
End If
Return MyBase.GetIconPadding(errcontrol)
End Function
Public Shadows Sub SetIconPadding(ByVal control As Control, ByVal padding As Int32)
Dim errcontrol As Control
If NeedsDummy(control) Then
errcontrol = GetDummy(control)
Else
errcontrol = control
End If
MyBase.SetIconPadding(errcontrol, padding)
End Sub
#End Region
''' <summary>
''' A non-painting, zero sized control to post an Error against
''' </summary>
Private Class DummyControl
Inherits Control
Public Sub New()
Size = Size.Empty
Visible = True
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
'don't paint anything
End Sub
End Class
End Class
|
|
|
|
|