add image to a Toolstripbutton from image - vb.net

I have a small issue with my taskbar.The main idea is to create my own taskbar. And I would like to get the application icon to the ToolbarButton
I am using this code
ToolStrip2.Items.Clear()
For Each proc As Process In Process.GetProcesses
Try
ImgList.Images.Add(Icon.ExtractAssociatedIcon(proc.MainModule.FileName))
If proc.MainWindowTitle <> "" Then
Dim menuitem As New ToolStripButton() With
{
.Text = proc.MainWindowTitle,
.Image = ImgList.Images.Count - 1 **//The Error is here**
}
ToolStrip2.Items.Add(menuitem)
End If
Catch ex As Exception
End Try
Next
And I get this error "Value of type 'Integer' cannot be converted to 'Image'"
I know what does it mean, but I dont know how to solve the problem.
Thanks for Help

Use :
ImgList.Images(ImgList.Images.Count - 1)
Remember image of toolstrip must be image not integer!

Related

Is there a way to retrieve text from a webpage to a TextBox in VB.NET?

Let say I have a TextBox1 and a SaveButton. A website "www.example.com/code1.text" with just a content:
303981
How to retrieve this 303981 to a TextBox?
Do we need a WebBrowser1?
Please help me I'm self learning and beginner also.
Look at the System.Net.WebClient type.
Using wc As New WebClient()
TextBox1.Text = wc.DownloadString("www.example.com/code1.text")
End Using
You need to place a webbrowser control on your form. I would use CefSharp for this
(see: https://www.youtube.com/watch?v=6d-AgfFzr70)
then you can do something like:
settings = New CefSettings
CefSharp.Cef.Initialize(settings)
browser = New ChromiumWebBrowser("www.google.com")
Which loads google.com, just change it to the page you want to load.
Now check your website and see if you can get that textbox value by javascript from the console (F12). Check the source if you can access it with it's ID or that you need to use it's class.
for example:
document.getElementsByClassName('textboxclass')[0].value;
if you manage to get the right value back in your browser console, you can go ahead in your program and do somethinh like:
Dim task As Task(Of JavascriptResponse) = browser.EvaluateScriptAsync(code)
where code contains your javascript.
you may do something like:
task.ContinueWith(
Sub(t)
Try
If t.IsFaulted = False And t.Result IsNot Nothing Then
Dim response = t.Result 'Error: Result is not a member of Task'
If response.Success And response.Result IsNot Nothing Then
taskResult = response.Result
Else
taskResult = "bad result"
End If
End If
Catch ex As Exception
'Dbg("!!!!!!!!!!!!!!! Exception: " & ex.ToString)
taskResult = ex.ToString
End Try
End Sub)
to get the result in a string.

Is it possible to customize error message "Column does not allow nulls" in a datatable?

I have a form with controls bound to a datatable in VB.net.
When keeping empty a field that should be filled, I'm receiving the error message : Column does not allow nulls.
Is it possible to replace this error message string by another one ?
There are a lot of ways when it comes to error handling.
You could get your code to throw a custom error alert:
This will throw an alert with the text: NullCollumContent
Try
'your code here
Catch ex As Exception
Throw New System.Exception("NullCollomContent")
End Try
Also as K3rnel31 explained:
This will just show a simple message box to the user
Try
'your code here
Catch ex As Exception
msgbox("error message here")
End Try
You could also use If statements to check the string:
This if checks the length of the string and checks if its equal to 0:
If Not yourString.Length = 0 Then
'your code here
else
'some error handling here
End If
This if checks if your string is equal to "" which basically means an empty string:
If Not yourString Is "" Then
'your code here
Else
'some error handling here
End If
Try
'your code here to the datatable
Catch ex As Exception
msgbox("changed error string here")
End Try
Thank you all for your answers but that's not really my problem.
Everything is about DataTable Object.
My problem is that I don't know where the exception is raised.
Let me explain.
Assume Column X has AllowDBNull property to false
And I'm not creating manually a new row that I add to the table
Which means I'm not using such code:
Dim NewRow as DataRow = DataTable.NewRow
NewRom.Item("X") = textbox1.text
DataTable.rows.add(NewRow)
If I was using that code, I would have indeed added the Try/Catch
Dim NewRow as DataRow = DataTable.NewRow
NewRom.Item("X") = textbox1.text
try
DataTable.rows.add(NewRow)
catch Ex as Exception
...
end try
I'm using a DataNavigator which adds in a hidden manner the new row to be added to the table.
So, if Column X is empty, he raises by himself the error. It's not an Exception to be handled. It's an error displayed at run time. Programmatically speaking, nothing went wrong at run time. It seems like DataTable object is designed with this feature and default Error Message.
I hope it's clear now.

How to put noone image in image column of datagridview

I have to put an image to specific column of datagridview but not in every row.
If getExists(myTable, CInt(reader.GetValue(0)), dbConn) Then
.Cells("myCol").Value = Image.FromFile("C:\myPath\myIco_16x16.ico")
Else
.Cells("myCol").Value = "" ' error here
End If
With this code I get error while executing probably because I'm try to put a string in image column. Second I try is:
.Cells("myCol").Value = Nothing
This don't cause error but put "error image" picture (with red X) to grid.
Is here a way to put no one image (blank) to datagridview's image column without loading a "blank image" from file or resource?
I've had to do this in the past and just used a 1 pixel transparent PNG. The performance was acceptable in my application even with 18 image columns and ~2000 rows and the background color of the cell shows through it just fine.
You should be able to create a 1x1 pixel transparent PNG fairly easily using a free program such as "Paint.Net".
You could just create a temporary blank bitmap and assign this to rather than loading an image from file.
Friend Function BlankImage() As Image
Try
Dim oBM As New Bitmap(1, 1)
oBM.SetPixel(0, 0, Color.Transparent)
Return oBM
Catch ex As Exception
Return Nothing
End Try
End Function
Slightly better method that can improve performance:
Friend Function BlankImage() As Image
Static oBM As New Bitmap(1, 1)
Try
If oBM Is Nothing Then
oBM.SetPixel(0, 0, Color.Transparent)
End If
Return oBM
Catch ex As Exception
Return Nothing
End Try
End Function

Close a form from another form

I face a problem with a multiple forms application
I have the mainForm and several mdiForms.
One of these child forms (frmDashboardManager) is open new ownedforms (frmDahboard) outside the mainForm
No I want to check if a frmDahboard is Open and if Yes to close it.
Here is what I have:
Dim fDash As New frmDashboard
fDash = isDashboardOpen(tempDash)
If fDash IsNot Nothing Then
fDash.Close() 'HERE I GET THE ERROR
End If
Private Function isDashboardOpen(ByVal dash As clsDashboard) As frmDashboard
isDashboardOpen = Nothing
Try
'search if Dashboard is already open
For Each fr As frmDashboard In Me.OwnedForms
If fr.My_Dashboard.Id = dash.Id Then
isDashboardOpen = fr
Exit For
End If
Next
Catch ex As Exception
gError.GetAppEx(ex, FORM_NAME & ".isDashboardOpen")
Finally
End Try
End Function
The error that I get is :
Object reference not set to an instance of an object.
The crazy thing is that I checked and isDashboardOpen returns actually a frmDashboard (also that's why fDash.Close() is executed).
Any ideas?
Thanks
I just found my error.
I disposed twice a user control that I have in frmDashboard.
I corrected that and everything works fine.
Thank you for your time.

images vb.net file used by another process error

I'm writing a little program where I select a picture through an open file dialogue. When I selected a picture I want it to overwrite the current picture and display the new image. Now I don't have any problems with picking an image with a different extension. So when I currently have a .png I can select a .jpg but when I choose an image with the same extension as the current image I get an error:
The process cannot access the file 'C:\Users....\woontypeimages\chalet_foto.jpg' because it is being used by another process.
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim sFilename As String = cboWoningtypesWoningtype.SelectedItem.ToString & "_foto" & System.IO.Path.GetExtension(ofd.FileName)
System.IO.File.Copy(ofd.FileName, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & sFilename, True)
txtWoningtypesFoto.Text = sFilename
updateImages()
End If
Private Sub updateImages()
Try
picFoto.Image = Nothing
txtWoningtypesFoto.BackColor = clrReadonly
txtWoningtypesFoto.ForeColor = Color.Black
picFoto.Image = System.Drawing.Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & txtWoningtypesFoto.Text)
Catch ex As Exception
txtWoningtypesFoto.BackColor = clrError
txtWoningtypesFoto.ForeColor = Color.White
End Try
Try
picGrondplan.Image = Nothing
txtWoningtypesGrondplan.BackColor = clrReadonly
txtWoningtypesGrondplan.ForeColor = Color.Black
picGrondplan.Image = System.Drawing.Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & txtWoningtypesGrondplan.Text)
Catch ex As Exception
txtWoningtypesGrondplan.BackColor = clrError
txtWoningtypesGrondplan.ForeColor = Color.White
End Try
End Sub
If anyone could help me I would be pleased
Thanks in advance
Instead of worrying about Dispose() you can instead use the Load(string) method of the PictureBox which won't lock the file.
Me.PictureBox1.Load("C:\test.png")
Use these :
picFoto.Image.Dispose()
picGrondplan.Image.Dispose()
instead of :
picFoto.Image = Nothing
picGrondplan.Image = Nothing
The Image.FromFile method maintains a lock on the source file until the image has been disposed. Setting an object to nothing does not immediately dispose it - the garbage collector will take care of that in its own time (which might well not be until you've closed the form with the picture box on). Dispose is required to immediately free the file handle.