I'm loading a DataSet into a DataGridView. The data is a hyperlink to a file on the local network but I can't get the link to actually launch the file. Do I have to go into the clickevent and actually launch it from there? Or is there a property I can set on the DataGridViewLinkCell to do it without the fuss?
Thanks, code is below.
'dgMain is the DataGridView
dgMain.DataSource = dataSet1.Tables(0)
'Just an example, will format entire column when I'm done
dgMain(10, 1) = New DataGridViewLinkCell
If I did go clickevent route I think it would be something like this but it doesn't work very well but I haven't tried much yet:
Private Sub dgMain_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgMain.CellContentClick
If e.RowIndex = -1 Then
Exit Sub
End If
If dgMain.Rows(e.RowIndex).Cells(e.ColumnIndex) Is DataGridViewLinkCell Then
Process.Start(dgMain.Rows(e.RowIndex).Cells(e.ColumnIndex).ToString)
End If
End Sub
Yes you need to handle the click event and launch the URL in code (Process.Start)
Related
I am looking to add a recent files list to an application I am writing.
I was thinking of adding the recent files to an xml file.
Where should this file be stored?
And how should it be called from the code?
I would imagine the xml would be stored in the same folder that the application is installed in, but not everybody will install the application in the same directory.
Is there a way to code it in such a manner that it will always be stored in the same folder as the application will be installed in?
much thanks in advance!
Here is an example using My.Settings. It requires you to open the Settings page of the project properties and add a setting of type StringCollection named RecentFiles as well as a ToolStripMenuItem with the text "Recent".
Imports System.Collections.Specialized
Public Class Form1
Private Const MAX_RECENT_FILES As Integer = 10
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadRecentFiles()
End Sub
Private Sub LoadRecentFiles()
Dim recentFiles = My.Settings.RecentFiles
'A StringCollection setting will be Nothing by default, unless you edit it in the Settings designer.
If recentFiles Is Nothing Then
My.Settings.RecentFiles = New StringCollection()
recentFiles = My.Settings.RecentFiles
End If
'Get rid of any existing menu items.
RecentToolStripMenuItem.DropDownItems.Clear()
'Add a menu item for each recent file.
If recentFiles.Count > 0 Then
RecentToolStripMenuItem.DropDownItems.AddRange(recentFiles.Cast(Of String)().
Select(Function(filePath) New ToolStripMenuItem(filePath,
Nothing,
AddressOf RecentFileMenuItems_Click)).
ToArray())
End If
End Sub
Private Sub UpdateRecentFiles(filePath As String)
Dim recentFiles = My.Settings.RecentFiles
'If the specified file is already in the list, remove it from its old position.
If recentFiles.Contains(filePath) Then
recentFiles.Remove(filePath)
End If
'Add the new file at the top of the list.
recentFiles.Insert(0, filePath)
'Trim the list if it is too long.
While recentFiles.Count > MAX_RECENT_FILES
recentFiles.RemoveAt(MAX_RECENT_FILES)
End While
LoadRecentFiles()
End Sub
Private Sub RecentFileMenuItems_Click(sender As Object, e As EventArgs)
Dim menuItem = DirectCast(sender, ToolStripMenuItem)
Dim filePath = menuItem.Text
'Open the file using filePath here.
End Sub
End Class
Note that the Load event handler includes a bit of code to allow for the fact that a setting of type StringCollection will be Nothing until you assign something to it. If you want to avoid having to do that in code, do the following.
After adding the setting, click the Value field and click the button with the ellipsis (...) to edit.
Add any text to the editor and click OK. Notice that some XML has been added that includes the item(s) you added.
Click the edit button (...) again and delete the added item(s). Notice that the XML remains but your item(s) is gone.
That XML code will cause a StringCollection object to be created when the settings are first loaded, so there's no need for you to create one in code.
EDIT:
I tested that by adding the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using dialogue As New OpenFileDialog
If dialogue.ShowDialog() = DialogResult.OK Then
UpdateRecentFiles(dialogue.FileName)
End If
End Using
End Sub
I was able to add ten files to the list via that Button and then they started dropping off the end of the list as I added more. If I re-added one that was already in the list, it moved to the top. If I closed the app and ran it again, the list persisted.
I'm having a problem with saving a setting to My Project and then exiting my program with an 'End' statement. If I save the setting but don't execute the end statement, everything works. If I save the setting and then execute the 'End', the setting doesn't get saved. Here's some code that illustrates the problem:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'reads the last setting correctly
TextBox1.Text = My.Settings.MySetting
End Sub
Private Sub btnWrite_Click(sender As Object, e As EventArgs) Handles btnWrite.Click
'write value, don't exit; works
My.Settings.MySetting = TextBox1.Text
End Sub
Private Sub btnWriteEnd_Click(sender As Object, e As EventArgs) Handles btnWriteEnd.Click
'write value and end; fails
My.Settings.MySetting = TextBox1.Text
End
End Sub
End Class
When I execute the code, whatever was last in My.Settings.MySetting appears in TextBox1. If I change the text in the textbox and click on the 'Write' button and manually exit the program by clicking on the 'X', the new text appears properly changed when I execute the program again. If I change the text and exit programmatically by clicking on 'WriteEnd', the changed setting text doesn't get written to 'MySetting'.
What am I doing wrong?
Thanks
Settings will be saved automatically at shutdown by default, so there's generally no need to call Save. End is definitely the issue. NEVER use End. Call Close on the startup form or call Application.Exit. I compare End with a bouncer grabbing someone by the scruff of the neck and throwing them out, spilling their drink on everyone and leaving their jacket behind, rather than asking them to leave of their own accord.
I have this code to save value from datetimepicker1:
Private Sub DateTimePicker1_Validating(sender As Object, e As EventArgs) _
Handles DateTimePicker1.Validating
My.Settings.dt1value= DateTimePicker1.Value.ToString
MsgBox("before save")
My.Settings.Save()
MsgBox("after save")
End Sub
It look that it saves value in My.Settings (From Message box 1,2)
Then when closing the app and running it again; it is not loading the My.Settings.dt1value into DateTimePicker1
The code for loading is:
Private Sub main_Shown(sender As Object, e As EventArgs) Handles Me.Shown
DateTimePicker1.Value = Convert.ToDateTime(My.Settings.dt1value)
End Sub
Other controls like Textbox1 is saving and loading properly but only for DateTimePicker is not working.
I tried to change from Handles Me.Shown to Handles Me.Load but same problem.
I have another problem,
When I deploy the application and setup in windows, My.Setting.Save() not working for all controls.
I had read other similar posts and try to follow them but nothing helps.
Any tip appreciated,
Thanks in advance.
First check whether the value is getting saved. Use
MsgBox(My.Settings.dt1value)
instead of
MsgBox("after save").
This will ensure the value is getting saved.
MsgBox("before save") & MsgBox("after save") does NOTHING useful here
But as from your code snippet, It seems the value is getting saved.
In the form Load event write the below mentioned code and check for the output:
string DatePattern = "dd/MM/yyyy HH:mm:ss";
DateTime ConvertedDateTime;
DateTimePicker1.Value = DateTime.TryParseExact(My.Settings.dt1value, DatePattern , null, DateTimeStyles.None, out ConvertedDateTime))
Edit : About your second problem
That is because whenever you update or modify the application in any way, the My.Settings(built-in settings file) gets flushed and a new one is generated. I would suggest you to save your config file in a separate external file. NOT in My.Settings
Good day. I Created a form that worked perfectlt, after testing the Command Button a few times and entering information into the form I decided to straiten out the form and make it tidy. after saving the form i tried to click the Command butten but this time it gave me a Error 424. Object Required.
(I tried to upload pictures with no success)
When I Check the Debug it highlight the .show command.
Private Sub CommandButton1_Click()
ClaimUserForm.Show
End Sub
I also tried:
Private Sub CommandButton1_Click()
Dim Claim as ClaimUserForm
Set Claim = new ClaimUser
claim.show
End Sub
But the debug re-appear.
in the form property window the form name is ClaimUserForm
Please help, I cant understand why it suddenly gave this problem.
Thanks
Is your button working properly? It seems it's missing the sender-object and the events.
Also set is not necessary.
Private Sub CommandButton1_Click(sender As Object, e As EventArgs) Handles CommandButton1.Click
Dim Claim As ClaimUserForm = New ClaimUser
Claim.Show
End Sub
sender As Object, e As EventArgs and Handles Button1.Click are missing.
It really drives me crazy, I have a form, I am calling a public sub called "timerss" in the form load event, when i run my form the sub "timerss" works perfectly, but when i add "Me.MdiParent = MDIParent1" in load event the sub "timerss" doesn't work!! i am really confused here!! any idea please.
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MdiParent = MDIParent1
timerss()
End Sub
update1:\ check the print screen of the result when running my form with and without setting MdiParent!
update2: I managed to fix part of the problem which is i got the data but what i want now is to set the color of the time cells with red, as i said the sub don't want to work, the timerss sub is:
For m As Integer = 0 To DataGridView1.Rows.Count - 1
If DataGridView1.Rows(m).Cells(4).Value > DataGridView1.Rows(m).Cells(9).Value Then
DataGridView1.Rows(m).Cells(4).Style.BackColor = Color.Red
MsgBox("red")
End If
Next
as u c in the above code i put a msgbox just to make sure that the code is working, so when i run my form the msgbox appears, but the backcolor function doesn't work when i set the MdiParent.
I don't know why the behavior is different without MDIParent, but you could try calling the sub from the DataBindingComplete event.
I had a similar problem and solved it using the event.