|
-
Dec 12th, 2005, 07:36 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] First start windows
I am making an application that I am going to sell, so I want it to look good and have good features. One of the things I cant seem to make is a first boot thing. When the person boots the program the first time it displays a form. Then once he closes the app and turns it back on that form will not show up again. How do I do this?
Last edited by 3dmaker; Dec 13th, 2005 at 08:53 PM.
-
Dec 12th, 2005, 07:44 PM
#2
Re: First start windows
Would require the use of writing a value in either the registry or a file located in the application directory. On for load, it simply reads in the value, checks to see if it is a certain value, then runs the appropriate code. I tend to stay away from the registry and just use a simple text file, but not saying you should do the same, just a preference...
-
Dec 12th, 2005, 08:11 PM
#3
Thread Starter
Hyperactive Member
Re: First start windows
Do you have an example of the file type? Not the registy.
-
Dec 12th, 2005, 08:16 PM
#4
Junior Member
Re: First start windows
You could use StreamReader and StreamWriter
http://www.knowdotnet.com/articles/streams1_4.html
(This is pretty much repeating what gigemboy said)
With the StreamWriter, you could create/update an ini file.
When your program loads, you could use the StreamReader to read the ini file and respond appropriately.
Last edited by KevinWilliams; Dec 12th, 2005 at 08:22 PM.
-
Dec 12th, 2005, 08:23 PM
#5
Re: First start windows
I will say that you should avoid the registry, as .NET apps are supposed to avoid using the registry if at all possible. I'd suggest setting a value in the config file, which is exactly what it is for. You read the value using the System.Configuration namespace and write it using an XmlDocument object. Here's an example of what your config file might look like:
HTML Code:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configsections>
<section name="start" type="System.Configuration.SingleTagSectionHandler" />
</configsections>
<start showWelcomeScreen="True" />
</configuration>
and here's an example of how you might use it in your main form:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Get the value of the "showWelcomeScreen" attribute from the "start" node.
Dim startTable As IDictionary = CType(Configuration.ConfigurationSettings.GetConfig("start"), IDictionary)
Dim showWelcomeScreen As Boolean = CBool(startTable("showWelcomeScreen"))
If showWelcomeScreen Then
MessageBox.Show("Welcome")
Dim configDoc As New Xml.XmlDocument
Dim configPath As String = Application.ExecutablePath & ".config"
'Load the config file into the XmlDocument
configDoc.Load(configPath)
'Find the node that corresponds to the "start" section.
For Each node As Xml.XmlNode In configDoc("configuration")
If node.Name = "start" Then
'Set the "showWelcomeScreen" attribute to False for subsequent instances of the application.
node.Attributes.GetNamedItem("showWelcomeScreen").Value = False.ToString()
Exit For
End If
Next
'Save the updated config file.
configDoc.Save(configPath)
End If
End Sub
Note that while you are debugging your config file will get overwritten each time you build, so the flag will be True every time you debug your app. If you take a look at the config file in your bin folder after the app starts up though, you'll see that it has been set to False. When you release your app, obviously the config file won't be overwritten each time the app is run so the flag will only ever be True for the first execution.
Edit: This is a good opportunity to become familiar with the config file, which is something that should be used in most complex apps, and much more than it currently is. A lot of developers avoid it because they don't know how to use it. This is a good start. Using the config file in VB 2005 is even easier with the My.Settings property. The code above is for VB.NET 2003. If you're using VB 2005 it reduces to something much simpler, although I haven't really used My.Settings myself yet so I can't tell you exactly what.
Last edited by jmcilhinney; Dec 12th, 2005 at 08:27 PM.
-
Dec 12th, 2005, 08:33 PM
#6
Thread Starter
Hyperactive Member
Re: First start windows
Thanks, but one question. I have been reading that code over and over and cannot figure out where it loads the xml file. What should I call the XML file? It does not work for me yet because I dont know what I should call the config file. :-\
-
Dec 12th, 2005, 08:36 PM
#7
Junior Member
Re: First start windows
thanks for the config file snippet jmcilhinney.
i'm still young in VB programming, so i can only offer people what i've used.
-
Dec 12th, 2005, 08:40 PM
#8
Re: First start windows
A config file is a specific type of file that has inbuilt behaviour in .NET. To create one you right click on your project in the solution explorer and select "Add New Item". You then select "Application Configuration File" from the list of available options. This adds an item to your project named "App.config". Each time you build your app after that, this file will be copied to your bin folder with the same name as your executable but with the additional ".config" extension, e.g. if your project is compiled to MyApp.exe the config file is automatically named MyApp.exe.config. The System.Configuration namespace automatically looks for and uses this file at run time. When editing the file yourself, this line:
VB Code:
Dim configPath As String = Application.ExecutablePath & ".config"
gets the full path to your executable file and adds the ".config" extension, so you always get the correct file.
-
Dec 12th, 2005, 08:47 PM
#9
Thread Starter
Hyperactive Member
Re: First start windows
Ahh I see. Thanks! But I get this error when trying to use the code
An unhandled exception of type 'System.Configuration.ConfigurationException' occurred in system.dll
Additional information: Error loading XML file file:///C:/Documents and Settings/george/My Documents/Visual Studio Projects/Config/bin/Config.exe.config This is an unexpected token. The expected token is 'QUOTE'. Line 1, position 10.
I tried changing the .config thing but unfortunately it does not fix.
Ok I got it somehow working. Now I get this error
An unhandled exception of type 'System.NullReferenceException' occurred in Config.exe
Additional information: Object reference not set to an instance of an object.
On line
Dim showWelcomeScreen As Boolean = CBool(startTable("showWelcomeScreen"))
-
Dec 12th, 2005, 08:57 PM
#10
Re: First start windows
I'm confused why it's looking for Config.exe.config. Is your project being compiled to an executable named Config.exe? As for the second error, it would appear that you haven't successfully created the startTable object, or else perhaps that item is being returned as a null reference. How about you post the code from your config file and how you are using it, just as I did before?
-
Dec 12th, 2005, 09:04 PM
#11
Thread Starter
Hyperactive Member
Re: First start windows
I am using the exact same config file code that you have posted. I am not to good with XML files.
-
Dec 12th, 2005, 09:13 PM
#12
Re: First start windows
Well that very code works fine for me so you've messed something up. What is the name of your executable file? Also, have you definitely used the method I detailed above to add the config file in the first place? Have you edited the VB code I posted?
-
Dec 12th, 2005, 09:19 PM
#13
Thread Starter
Hyperactive Member
Re: First start windows
What I did was made a new project.
called it Config (because I want to test this)
copied that code. Put it in. right clicked> make the config file.
added the code.
And i get that last error :-\
-
Dec 12th, 2005, 09:25 PM
#14
Re: First start windows
Post The Code You Are Using.
-
Dec 12th, 2005, 11:17 PM
#15
Thread Starter
Hyperactive Member
Re: First start windows
VB Code:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Get the value of the "showWelcomeScreen" attribute from the "start" node.
Dim startTable As IDictionary = CType(Configuration.ConfigurationSettings.GetConfig("start"), IDictionary)
Dim showWelcomeScreen As Boolean = CBool(startTable("showWelcomeScreen"))
If showWelcomeScreen Then
MessageBox.Show("Welcome")
Dim configDoc As New Xml.XmlDocument
Dim configPath As String = Application.ExecutablePath & ".config"
'Load the config file into the XmlDocument
configDoc.Load(configPath)
'Find the node that corresponds to the "start" section.
For Each node As Xml.XmlNode In configDoc("configuration")
If node.Name = "start" Then
'Set the "showWelcomeScreen" attribute to False for subsequent instances of the application.
node.Attributes.GetNamedItem("showWelcomeScreen").Value = False.ToString()
Exit For
End If
Next
'Save the updated config file.
configDoc.Save(configPath)
End If
End Sub
End Class
-
Dec 12th, 2005, 11:32 PM
#16
Re: First start windows
The error you are getting is because the ConfigurationSettings class is not finding a configuration section named "start", hence the startTable is a null reference after that line. This means that either you have created your config file incorrectly or you have put the wrong code in it. The fact you said you had some other error previously suggests to me that it is the former. Does your project look something like the attached image? I'd suggest that you delete your existing config file and create a new one by following these steps:
1. Right-click your project in the Solution Explorer.
2. Select Add->Add new Item.
3. Select "Application Configuration File"
4. Paste the following code into the file:
Code:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="start" type="System.Configuration.SingleTagSectionHandler" />
</configSections>
<start showWelcomeScreen="True" />
</configuration>
-
Dec 12th, 2005, 11:36 PM
#17
Thread Starter
Hyperactive Member
Re: First start windows
Huh. It works now. Funny how I followed the exact steps and it did not work.
Now it works! Thanks!!
-
Dec 12th, 2005, 11:45 PM
#18
Thread Starter
Hyperactive Member
Re: [RESOLVED] First start windows
Hmm...
I made the welcome screen a form. Now when I launch the app it does show up once but it shows up behind the main app. Any idea on how to get in front?
-
Dec 12th, 2005, 11:55 PM
#19
Re: First start windows
Presumably you are calling Show on the welcome form. This means that it will be Activated but then as soon as the main form is displayed it gets Activated straight away. Do you want the main form to be displayed immediately or to wait until the welcome form is dismissed? If it is the latter then just call ShowDialog on the welcome form instead of Show. If it's the former then you can use the main form's Activated event instead of Load:
VB Code:
Private firstActivation As Boolean = True
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
If Me.firstActivation Then
Me.firstActivation = False
'Get the value of the "showWelcomeScreen" attribute from the "start" node.
Dim startTable As IDictionary = CType(Configuration.ConfigurationSettings.GetConfig("start"), IDictionary)
Dim showWelcomeScreen As Boolean = CBool(startTable("showWelcomeScreen"))
If showWelcomeScreen Then
MessageBox.Show("Welcome")
Dim configDoc As New Xml.XmlDocument
Dim configPath As String = Application.ExecutablePath & ".config"
'Load the config file into the XmlDocument
configDoc.Load(configPath)
'Find the node that corresponds to the "start" section.
For Each node As Xml.XmlNode In configDoc("configuration")
If node.Name = "start" Then
'Set the "showWelcomeScreen" attribute to False for subsequent instances of the application.
node.Attributes.GetNamedItem("showWelcomeScreen").Value = False.ToString()
Exit For
End If
Next
'Save the updated config file.
configDoc.Save(configPath)
End If
End If
End Sub
-
Dec 13th, 2005, 12:19 AM
#20
Thread Starter
Hyperactive Member
Re: First start windows
Well now these lines of code
VB Code:
Private firstActivation As Boolean = True
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
If Me.firstActivation Then
Me.firstActivation = False
the Private, Private Sub, Me.FirstActivation are all underlined in blue.
-
Dec 13th, 2005, 01:16 AM
#21
Re: First start windows
Please don't tell me they're underlined in blue without telling me what the error messages are. Where on earth did you put that code? You haven't put it within the Load event handler have you?
-
Dec 13th, 2005, 01:25 AM
#22
Thread Starter
Hyperactive Member
Re: First start windows
Well I fixed the issue now. I made it even easier! Instead of it opening a new form it just loads a sepperate application.
-
Dec 13th, 2005, 01:29 AM
#23
Re: First start windows
Obviously the choice is yours but it seems a very odd thing to start a seperate application just to show a welcome screen. I doubt you could find any other app that does that.
-
Dec 13th, 2005, 08:02 PM
#24
Thread Starter
Hyperactive Member
Re: First start windows
Thank you! I got it working with that code!!
-
Dec 13th, 2005, 08:07 PM
#25
Re: First start windows
Cool. Don't forget to resolve your thread from the Thread Tools menu.
-
Dec 13th, 2005, 08:09 PM
#26
Thread Starter
Hyperactive Member
Re: First start windows
Alright! I figured it out!
Last edited by 3dmaker; Dec 13th, 2005 at 08:53 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|