Good Morning all.
I have been working on this forever. but I am down to the very end. I need to get this submitted for may class Sunday.
I have a button that sends my data to the usb drive and I need to send it to a list on a second form. How do I pass this over.
Here is what I have:
Private Sub btnTestResults_Click(sender As Object, e As EventArgs) Handles btnTestResults.Click
' Sends data to the frmresults form three, and creates the "consult.txt file on a
' USB drive for the Nurse can contact the patients.
Dim objWriter As New IO.StreamWriter("e:\consult.txt")
Dim frmThird As New frmElevatedResults
Dim intHighCholesterol As Integer = 200I
' Write the file line by line until the file is completed.
If IO.File.Exists("e:\consult.txt") Then
If _intCholesterolLevel(intCount) < intHighCholesterol Then
objWriter.WriteLine(_strNames(intCount))
objWriter.WriteLine(_intCholesterolLevel(intCount))
Else
MsgBox("The file is not available.
Please restart the program when the file is available", , "Error")
objWriter.Close()
End If
End If
Hide()
frmThird.ShowDialog()
End Sub
Thank you very much
One option would be to make the listbox public on frmElevatedResults:
Add the listbox control via Toolbox to your frmElevatedResults (if not already done).
In the Project-Explorer click on Show all Files.
Open frmElevatedResults.Designer.vb
Find the listbox controle e.g. listBox1 as ListBox
Make it public: public listBox1 as ListBox
(I would have included screens but my VS is not in English).
On your first form you can now use:
Dim frmThird As New frmElevatedResults
...
frmThird.listBox1.Items.Add(string.concat(_strNames(intCount),
" - ",
_intCholesterolLevel(intCount))
...
Hide()
frmThird.Show()
or whatever you want to put in there.
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've got a program which calls a second form. This second form has a combo box populated from the contents of an external file, and the user needs to select an option in the combo box from the presented options. This selection is then passed back to the main form where a lot of work is done.
This all works nicely the first time it is done. However, the second time this second form is called, the drop-down is blank. I've confirmed via some debugging that the correct code is being run and that entries are being added via "SecondForm.ComboBox1.Items.Add" (I can clear the combobox, check it's zero, read the data and then check the items in the list again, it increases correctly) but they're just not being displayed on the form. I can't figure out why or how to fix it.
And so the pertinent parts of the code....
At the form level I have this line to set up the second form, I believe I need the WithEvents to pass the selected data back as far as I can tell:
Public Class Form1 Friend WithEvents SecondForm As New Form2
Public Sub OpenStripformatstxtToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenStripformatstxtToolStripMenuItem.Click
Dim fd As OpenFileDialog = New OpenFileDialog()
Dim pos1 As Integer
Dim pos2 As Integer
' Select the file to open
fd.Title = "Open File Dialog"
fd.InitialDirectory = "C:\BEST\Data"
fd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
fd.FilterIndex = 1
fd.RestoreDirectory = True
' Put the filename selected in strfilename2
If fd.ShowDialog() = DialogResult.OK Then
strFileName2 = fd.FileName
Else : Return
End If
If SecondForm.IsDisposed Then
Dim secondform As New Form2
I suspect this line above is the problem, I'm creating the form a second time but WITHOUT the WithEvents paramater. However I can't use that from within this part of the code, I get an error "'WithEvents' is not a valid local variable declaration". I've read that closing and reopening forms is not good coding and that I should be hiding / showing them
secondform.Show()
InitializeComponent()
Else
SecondForm.Show()
End If
' Copy the file contents to a string called sfcontents (Strip Format Contents)
sfcontents = My.Computer.FileSystem.ReadAllText(fd.FileName)
' Define some points in the string, starting at the beginning
pos1 = 1
pos2 = 1
' Loop from the start to the end of the string
For pos1 = 1 To Len(sfcontents)
' Look for FO, the strip name header, do the following if you find it
If Mid(sfcontents, pos1, 3) = "FO " Then
pos1 = pos1 + 3
pos2 = pos1 + 1
'Find the space after "FO " so we've captured the whole next word, that's the strip name
Do Until Mid(sfcontents, pos2, 1) = " "
pos2 = pos2 + 1
Loop
' Add that strip name to the combobox for selecting by user
SecondForm.ComboBox1.Items.Add(Mid(sfcontents, pos1, pos2 - pos1))
It is this line above which is populating the ComboBox, but that data is NOT being displayed on the form which is shown to the user after the first instance of the form is shown
End If
' Next step in the string
Next pos1
End Sub
Private Sub secondform_formclosing(sender As Object, e As FormClosingEventArgs) Handles SecondForm.FormClosing
There's a few hundred lines of code in here which then work with the data passed from the form closing, ie the selected value of the ComboBox. This all works fine for the first running of the code, but as the ComboBox is empty on subsequent runs, it doesn't work after that. Happy to post that code if anyone thinks it'll help, but I think it'll just muddy the issue at this stage as that code seems fine. However, see the bit below about event handlers...
End Sub
The code on Form2.vb is as follows:
Public Class Form2
Public selectedstrip As String '= ComboBox1.SelectedItem
Public stripfunction As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If RadioButton1.Checked Then stripfunction = 1
If RadioButton2.Checked Then stripfunction = 2
If RadioButton3.Checked Then stripfunction = 3
selectedstrip = ComboBox1.SelectedItem
Me.Close()
End Sub
End Class
I've read a bit online that says closing and reopening forms is not, excuse the pun, good form. However I'd then need event handlers for form.hide and I can't seem to work out how to use them or even what they are. If hiding the form is a better alternative solution, if someone could point me in the right direction for how to do that and what handlers to use instead, then I'd be grateful.
I'm probably doing something incredibly stupid, because everything I'm doing is self-taught from googling and I probably lack a greater understanding of WHY I need to do certain things, so apologies for any ignorance on my part. With that in mind, if I'm doing anything a completely silly way I'm open to rewriting it in a way that helps, but I may need some hand-holding to do that!
Thanks in advance for any help anyone can give.
The main problem appears to he here:
If SecondForm.IsDisposed Then
Dim secondform As New Form2
You are declaring a new local variable there and assigning the new Form2 object to that rather than the member variable, so when you later refer to the member variable to populate the ComboBox, you're not referring to the Form2 instance you just created.
Your code is rather weird anyway. Here's my advice.
Firstly, get rid of the code form Form1 that populates the ComboBox in Form2. Forms should populate their own controls. Put the code to populate the ComboBox in the Load event handler of Form2. You're then guaranteed that any time you call Show on a new instance of Form2, the code to populate the ComboBox will be executed. That's how a form should work.
As an alternative, given that you're reading from a file and that data probably won't change over the course of a session, read the data and put it into an array in Load event handler of Form1 and then pass that array to the constructor of Form2. You would have to write that constructor yourself and, in it, you would populate the ComboBox with the array data. That way, you're not reading and processing the same data file over and over but you're still populating Form2's controls in Form2.
Secondly, change this code:
If SecondForm.IsDisposed Then
Dim secondform As New Form2
secondform.Show()
InitializeComponent()
Else
SecondForm.Show()
End If
to this:
If SecondForm.IsDisposed Then
'Create and display a new instance.
Secondform = New Form2
Secondform.Show()
Else
'Focus the existing instance.
SecondForm.Activate()
End If
Note that there is no local variable, so the new instance is assigned to the member variable.
There is also no call to InitializeComponent. That method is what creates and configures the controls on a form based on the actions in the designer. The ONLY place that gets called is in a constructor.
Finally, if an instance of Form2 is already displayed, its Activate method is called to make sure that it has focus.
I have made a form in Vb.net in which the user can do several changes. I want the changes which the user makes should remain each time it is executed.
For Example, I have applied some effects on font while execution -
Private Sub Checkbox1_CheckedChanged ()
If CheckBox1.Checked = True then
Label1.Font = New Drawing Style ("Comic Sans Ms", 16, FontStyle.Bold)
End If
End Sub
The changes which the user makes should be visible next time the program is executed.
Is it possible?
Thanks in Advance!
You have a couple of options, The one that I would use if you are just going to have a few settings is the built in User Settings, you can create a setting in your Project Propertys. This is an example of your Font.
You would then use it like this.
Private Sub Checkbox1_CheckedChanged() Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
Label1.Font = New Font("Comic Sans Ms", 16, FontStyle.Bold)
My.Settings.MyNewFont = Label1.Font
My.Settings.Save()
End If
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
If Not IsNothing(My.Settings.MyNewFont) Then Label1.Font = My.Settings.MyNewFont
End Sub
Yes you can use external Css file for this purpose
1st you create a external Css file of your project and specify some default style
Like your Css file
.Fstyle {
font-family: Comic Sans Ms;
}
And your aspx
<asp:label id="lblname" runat="server" class="Fstyle">
2nd you can re-write to this file by the changes which the user makes
save the changes to the variables and override this file by using IO function of vb.net it remains the changes permanent.
Marks answer is how i would do it Except if you wish to store changes for a number of different controls you might be best to loop through them and check the settings on form closing and store them in a System.Collections.Specialized.StringCollection in My.Settings then you can loop through this collection on Form Load and set the settings as they should be.
Let me know if you want an example and i can provide it.
Example:
Note: I have always found when adding a System.Collections.Specialized.StringCollection you need to add an empty string manually (you can then delete it) to force it to create the .xml (probably a more technical solution to this but it works for me)
I created one called FormLoadData
To identify which controls to store data for I prefixed their names with "UC_", if it is all the controls on the form or all in a groupbox etc then you can skip this check.
then add the following form closing sub:
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'clear collection
My.Settings.FormLoadData.Clear()
'loop through each control on the form
For Each Con As Control In Me.Controls
'run check to see if it is a control to store changes for
If Con.Name.Substring(0, 3) = "UC_" Then
'load any settings you wish to store in the collection starting with the control ID
My.Settings.FormLoadData.Add(Con.Name & "," & Con.Font.ToString)
End If
Next
End Sub
and the following under New:
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'loop through controls
For Each Con As Control In Me.Controls
'loop through settings to compare
For Each item In My.Settings.FormLoadData
'check stored name against control name
If Con.Name = item.Split(",").ElementAt(0) Then
'update settings
Con.Font = New Font(item.Split(",").ElementAt(1), Con.Font.Size)
MsgBox(item)
Exit For
End If
Next item
Next Con
End Sub
There is certainly a better way of doing this rather then the loop through the collection created but unless you are storing the data for thousands of controls this will work OK.
My browser is playing up so i cannot check what this looks like but hopefully it is clear!
I know this has probably been asked 1000 times but I can't get my head around it
I have a text box on a form called 'Settings' that stores a file path and I need to reference that file path in a form called 'Main
I know this should be simple but just cannot get it to work!
Any simple advice
Thanks
As below i need the Dim zMailbox to refer to a textbox value on a separate form (Settings)
Public Class Main
Dim zMailbox As String = "C:\Dropbox\User\Lynx\In\"
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim lynxin As New IO.DirectoryInfo(zMailbox)
lstPlanned.Items.Clear()
For Each txtfi In lynxin.GetFiles("*.txt")
lstPlanned.Items.Add(IO.Path.GetFileNameWithoutExtension(txtfi.Name))
Next
End Sub
You should be using something like My.Settings
To do so, you right-click on your project and then click Properties. On the left side, you have a tab called "Settings". You can create a setting there and give it a default value. Ex : MyPath.
Then on your Settings form, you set your value into My.Settings.MyPath.
My.Settings.MyPath = TextboxPath.Text.Trim()
So when you want to access it anywhere in your application after, you can just use :
My.Settings.MyPath
I want to be able to edit/delete/insert lines of text from a textfile by using listbox and textbox.
I want to display all the contents of the textfile per line in a listbox and when I click a line of text it will then display it in the textbox giving me the option to either edit or delete that line of text.
My insertion of text would be inserted after the last line of text being displayed in the listbox. Is this possible? I just want a starting point and I will continue it from there. Thanks in advance.
Here is an answer with a listview , button 9 is to fill Listview, listview click sends the text to textbox and button 10 saves it back to the listview
This is probably the simplest way to do what you want to achieve.
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
ListView1.HeaderStyle = ColumnHeaderStyle.None
ListView1.HideSelection = False
For i As Integer = 0 To 50
ListView1.Items.Add("Line number" & i.ToString)
Next
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.Click
TextBox8.Text = ListView1.SelectedItems(0).Text
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
ListView1.SelectedItems(0).Text = TextBox8.Text
End Sub
Probably a good starting point for you anyway and expand this code from here on.
Yes. It is possible. I would suggest to use already existing text editors rather than re-inventing the wheel. If you still want to go ahead and create a new one from start, then you can try the following.
Create a window form application in vb.net with ListBox control for showing the lines, a textbox control for entering the file name, a button to browse to the given file, a button on clicking on which the file content should get loaded. Refer file objects for this.
Utilise ContextMenu class in vb.net to allow right click to read selected listbox line and accordingly perform add/delete/edit operation by modifying the selected listitem value.