VB.NET Set Default Text Editor - vb.net

I have created my own text editor in Visual Basic 2013. I want to open text files with it from outside the application: to open them from the Desktop with double click or right click and open with.
I tried to use right click and open with but it doesn't work, it just opens up my application.
How I make my text editor the one that I open text files with?

You would have to use something like the Environment.GetCommandLineArgs method.
Put this in your form load event:
Dim CommandLineArguments() As String = Environment.GetCommandLineArgs()
If CommandLineArguments.Length >= 2 AndAlso String.IsNullOrEmpty(CommandLineArguments(1)) = False AndAlso IO.File.Exists(CommandLineArguments(1)) Then
Me.TextBox1.Text = IO.File.ReadAllText(CommandLineArguments(1))
End If
This will get the command line arguments sent to your application (which is the path to the file you are trying to open with your app) and check if the argument is an existing file. If so, it will read all the file's text into your TextBox.

Write this code in the form load event.
Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim fname As String = Command$()
If Not fname = "" Then
fname = Replace(fname, Chr(34), "")
Dim obj As New System.IO.StreamReader(fname.ToString)
RichTextBox1.Rtf = obj.ReadToEnd
obj.Close()
Me.Text = "Your Application Name " & fname
End If
End Sub

Related

Asp.Net, VB, SQL Server Reporting Services..dynamically generated reports from directory to be viewed on report viewer on click?

I have an aspx webform that in it's vb code reads files that are .rdl from a directory and then lists them as say a button or hyper link etc. "reports are on local host report server"
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim FileLocation As DirectoryInfo = _
New DirectoryInfo("C:\inetpub\wwwroot\Reports")
Dim fi As FileInfo() = FileLocation.GetFiles("*.rdl")
For Each name As FileInfo In fi
' Dim i As Integer
Dim listed As New LinkButton
' listed.Attributes("CssClass") = "a"
' listed.Attributes("Class") = "a"
listed.Attributes("id") = "listed"
listed.Text = (name.Name)
mine.Controls.Add(listed)
'mine.InnerHtml = ""
'i = i + 1
Next
End Sub
and another that has a reportviewer. When a report was clicked, how can I send the name/value to the viewer and redirect to it?
Thank you very much
EDIT: i got to make the click redirect to the viewer with a string and have the viewer use that string,but with how much ever variation ive tried it still errors that
The path of the item 'salesreport.rdl' is not valid. The full path must be less than 260 characters long; other restrictions apply. If the report server is in native mode, the path must start with slash. (rsInvalidItemPath)
this is the viewer code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
' Retrieve parameter from Route "Reports/{reportName}"
Dim reportName = Picks.Selecty
'Page.Title = reportName
ReportViewer1.ProcessingMode = ProcessingMode.Remote
Dim serverReport As ServerReport
ServerReport = ReportViewer1.ServerReport
serverReport.ReportServerUrl = New Uri("http://localhost/reportserver/")
serverReport.ReportPath = reportName
' reportName
'
' add parameters here
'Dim param As New ReportParameter("name", "value")
'serverReport.SetParameters(param)
serverReport.Refresh()
End If
End Sub
You could try the following.
Create a new page say ReportViewer.aspx which hosts your ReportViewer Control that recieves the name of the .RDL file as a query string.
In the First .aspx page, for every link you display, point the NavigateURL property to ReportViewer.aspx page and pass the name of the .RDL file as a QueryString.
Hope that helps
Thanks,
Prawin

Update one form based on selections from another form

I apologise if the title is a bit vague, i've only been on here a day.
So my problem is I have a menu form in which I input the options from the comboboxes. And then I go to the next form which shows the relevant imported text file info.
However when I click the 'back' button to return to the menu and input different information in the comboboxes, it doesn't take me to the correct text file info, it just shows the info from the previous selection.
here is the student menu pic
here is the text file form
below is the code for the student menu next button:
If OptionBox.Text = "Introduction" Then
Introduction.Show()
Else
If OptionBox.Text = "Explanation" Then
Explanation.Show()
End If
End If
End Sub
below is the code for the text file form load page and the back button
Private Sub Introduction_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Student_Menu.Hide()
Dim font As New System.Drawing.Font("Calibri", 11)
If Student_Menu.TopicSelect.Text = "Computer Systems" Then
Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\ComputerSystems.txt"
Dim sr As New IO.StreamReader(strFile)
IntroductionLabel.Text = sr.ReadToEnd()
sr.Close()
Else
If Student_Menu.TopicSelect.Text = "Hardware" Then
Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\Hardware.txt"
Dim sr As New IO.StreamReader(strFile)
IntroductionLabel.Text = sr.ReadToEnd()
sr.Close()
Else
If Student_Menu.TopicSelect.Text = "Software" Then
Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\Software.txt"
Dim sr As New IO.StreamReader(strFile)
IntroductionLabel.Text = sr.ReadToEnd()
Else
If Student_Menu.TopicSelect.Text = "Representation of Data" Then
Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\RepresentationOfData.txt"
Dim sr As New IO.StreamReader(strFile)
IntroductionLabel.Text = sr.ReadToEnd()
Else
If Student_Menu.TopicSelect.Text = "Databases" Then
Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\Databases.txt"
Dim sr As New IO.StreamReader(strFile)
IntroductionLabel.Text = sr.ReadToEnd()
Else
If Student_Menu.TopicSelect.Text = "Communications & Networks" Then
Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\Hardware.txt"
Dim sr As New IO.StreamReader(strFile)
IntroductionLabel.Text = sr.ReadToEnd()
End If
End If
End If
End If
End If
End If
IntroductionLabel.Font = font
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
Me.Hide()
Student_Menu.Show()
Student_Menu.TopicSelect.ResetText()
Student_Menu.OptionBox.ResetText()
End Sub
what do i need to do in order to update this information so that the program doesn't skip going through the form again.
There is a lot of repeated code there. Here is a way to reduce it (see DRY) and expose a method to change the topic. In a Module:
Public Enum Topics
ComputerSystems
Hardware
Software
Data
Database
Networks
End Enum
Then in the form that shows the text:
Friend Sub DisplayTopic(topic As Topics)
Dim text As String
Dim filname As String = ""
Select Case topic
Case Topics.ComputerSystems
filname = "ComputerSystems.txt"
Case Topics.Database
filname = "Databases.txt"
'... etc
End Select
' attach path
filname = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"gcsecomputingtask", filname)
text = File.ReadAllText(filname)
IntroductionLabel.Text = text
End Sub
By the way, VB does has an ElseIf which can avoid the "arrow" anti pattern you can see in your code. At the very least, the excessive indentation is annoying.
If topic = Topics.ComputerSystems Then
'...
ElseIf topic = Topics.Software Then
'...
End If
Show that form normally using an instance of the form class:
Public Class MenuForm ' a form is just a class
' declare an object variable to use
Private info As Form2 ' whatever its name is (Explanation???)
....
Private Sub MenuForm_Load(...)
' create an instance to be used later:
info = New Form2
Then invoke the method to tell it which topic to display. Displaying topic info is a separate method from loading a form first because the form load event happens only once. A specialized method to do what we want makes more sense since they really have nothing to do with one another, and makes it easier to see how the code works:
info.DisplayTopic(Topics.ComputerSystems)
info.Show
This way, you dont have one form fiddling with the controls on another, but still have a clear way of communicating which topic to display.
Note that the location of the topics file(s) is a bit different. You'd want a "gcsecomputingtask" folder in MyDocuments for the files. The VS project folder is not a good place for it, the folder location could change depending on which machine you are running on (yours or computer lab etc). They could also be stored as a resource to skip that part too.

How to Make a File Name appear as Form Text

I am creating a text editor in VB.net with tabs and I am done so far. All I need is when the user saves the document or opens a document the name of the document shows up on the tab. I am using a separate tab control. I have it done but it shows the whole directory of the file. The only way to change the text on the tab is to change the text of the form I am using for the tab control to duplicate. So my code for when the user opens a file is:
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim alltext As String
filename = OpenFileDialog1.FileName
alltext = File.ReadAllText(filename)
FastColoredTextBox1.Text = alltext
Me.Text = filename
End Sub
But like I said it shows the whole directory of the file. Is there any way to make it just show the file name. The code for when the user saves the file has the same thing.
Try this:
Me.Text = System.IO.Path.GetFileName(filename)

Issue Creating Text File Via Visual Basic (Not writing text)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "C:\KVRequest.txt"
Dim aryText(4) As String
aryText(0) = "TextBox4.Text"
aryText(1) = "TextBox5.Text"
aryText(2) = "TextBox6.Text"
aryText(3) = "TextBox7.Text"
aryText(4) = "TextBox8.Text"
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
objWriter.Close()
MsgBox("Text file created in your C drive, attach this file in an email to someone#gmail.com Please check that all of the details are correct before sending.")
End Sub
What I am trying to do is get the text from the text boxes (4 5 6 7 8) to write into a text file. The code I have creates the file, but does not write text into it, can anyone give me a tip on how to get this working?
Thanks!
Edit: Also while I am here, I was trying to get it so button_1.enabled was only true if all of the text boxes had been edited, but I could not think of a practical way to do this, if you could help me with this I would also be very grateful!
Given the code posted above, the reason nothing is being written to the file is because you're not telling it to write anything to the file. You would need to add something like this between the creation of your StreamWriter and where you close the close method on it:
objWriter.WriteLine(TextBox4.Text)
objWriter.WtiteLine(TextBox5.Text)
etc...
Also, the simplest option for only enabling the save button is to create a Control.TextChanged handler for each of your text boxes (or use the one Sub to do it for all of them by adding all their events to the one handler method) and have it do something similar to:
If TextBox4.Text <> "" And TextBox5.Text <> "" And TextBox6.Text <> "" Then
Button1.Enabled = True
Else
Button1.Enabled = False
End If

Can't get VBA to write "http://"as text?

I wrote the code bellow and need the asociated .PGP file to have the text http:// included. the PGP file is namely read by Autocad which requers the "http://" in its text to be able to launch the desierd webpage. problem is , is that VBA is Auto formating the http:// as a code entatie and not writting it to the text based PGP file.
Can any one tell me how to achive what im after?
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim FILE_NAME As String = "C:\test.pgp"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
objWriter.WriteLine((TextBox5.Text) + "," + " " + "START http://" + (TextBox6.Text) + ", 1,,")
objWriter.Close()
MsgBox("The acad.pgp file was successfully appended…")
Else
MsgBox("File missing reinstall or contact vendor…")
End If
End Sub
Hm, I'tried your code above in VisualStudio 2010. An empty file named C:\test.pgp was appended with the following text:
textbox5, START http://textbox6, 1,,
The text http... is right there. Sometimes, when I open the file in a viewer, this viewer automatically detects the http string and marks it as a hyperlink. But only in this viewer!
So the error seems to be somewhere else, not in the code?