Results 1 to 17 of 17

Thread: [RESOLVED] Capturing confirmation from a splash screen

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Location
    UK - OK
    Posts
    72

    Resolved [RESOLVED] Capturing confirmation from a splash screen

    Hi guys,

    Im trying to create a splash screen within Visual Studio Express 2017. The desired outcome is that a notification form pops up with a message and the user clicks "I accept". What I would like to do is capture that confirmation (Im thinking either write a text file to a network share or write to a file which stamps the date and user who has accepted).

    I was wondering if anyone has any thoughts\suggestions regarding this. I have very little C# knowledge but am a keen learner (I have written in other programming languages and scripting languages before).

    Thanks

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Capturing confirmation from a splash screen

    Quote Originally Posted by shabbaranks View Post
    What I would like to do is capture that confirmation (Im thinking either write a text file to a network share or write to a file which stamps the date and user who has accepted).
    There are a variety of things you could do, but which one(s) are appropriate depends on details that are currently unclear.

    I get the impression that what you are looking to achieve is a list of users who have clicked "I accept" (and when), which can be checked by an admin person without going to each computer.

    If that is the case then writing to a file on a network share would be one way to go, but having multiple users attempting to write to the same file could be problematic (the users could get exceptions, or overwrite the data written by somebody else). Writing separate files to a network folder (which is only used for this purpose) would overcome those problems, but arguably make checking harder (as you would need to view the folder, and look at the files).

    An alternative is to use a server-based database (such as SQL Server or MySQL), which wouldn't have those issues, but could take a lot more work if you aren't already using that kind of database.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Location
    UK - OK
    Posts
    72

    Re: Capturing confirmation from a splash screen

    Thanks Si - I did think about the SQL route which wouldn't be too difficult but I didn't want to over engineer the requirement.

    "I get the impression that what you are looking to achieve is a list of users who have clicked "I accept", which can be checked by an admin person (without going to each computer)."

    This is correct, a daily message will be scheduled (the form in question) to which users will need to confirm they have received and read the notification. To start with (and to get me learning about programming) Im thinking the click function could create a text file on a network share naming it in this format username_date_confirmed.txt

    Ive created a form and have got the accept button closing the form upon pressing - all I need to do now is create the code which does the text file stuff - is this relatively simple?

    Thanks again

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Location
    UK - OK
    Posts
    72

    Re: Capturing confirmation from a splash screen

    Slowly making progress... I managed to get my splash screen to create a text file in the desired location, but what I am trying to do now is to name the text file based on the current logged in user - any ideas please?

    Current code
    Code:
    using System;
    using System.Windows.Forms;
    using System.Security.Principal;
    
    namespace WindowsFormsApp1
    
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            string FilePath = @"C:\Temp\to be deleted\Notification\";
            string UserName = Environment.UserName;
            
           
            
            private void Accept_btn_Click(object sender, EventArgs e)
    
            {
                System.IO.StreamWriter writer = new System.IO.StreamWriter(File);
                writer.Write(File);
             }
        }
    }

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Capturing confirmation from a splash screen

    From the admin side of things I don't think using files or a database makes too much difference, but in terms of the code it will be a bit easier to create files rather than using a database.

    What you've got seems to be on the right lines

    You can use the UserName like this:
    Code:
            string SharedFolder = @"C:\Temp\to be deleted\Notification\";
    
            string UserName = Environment.UserName;
            string DateValue = ????
            string FileName = string.Format("{0}_{1}.txt", UserName, DateValue);
    
            string FilePath = Path.Combine(SharedFolder, FileName);
    Note that filenames shouldn't contain certain characters (such as / ), so you probably want to format the date as YYYYMMDD (which not only avoids this issue, but also allows sorting the files nicely in Explorer).

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Location
    UK - OK
    Posts
    72

    Re: Capturing confirmation from a splash screen

    Thanks, is this piece of code
    Code:
    string FileName = string.Format("{0}_{1}.txt",
    telling the order of the file name based on the strings? So effectively {0} = Username_{1)=Date?

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Capturing confirmation from a splash screen

    That's right

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Location
    UK - OK
    Posts
    72

    Re: Capturing confirmation from a splash screen

    Just trying to piece together this code - but I cant work out why my strings have red marks against them "a field initializer cannot reference the non-static field, method or property 'Form1.SharedFolder'.

    In layman's terms - what does this mean?

    Code:
    using System;
    using System.Windows.Forms;
    using System.Security.Principal;
    using System.IO;
    
    namespace WindowsFormsApp1
    
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            string SharedFolder = @"C:\Temp\to be deleted\Notification\";
            string UserName = Environment.UserName;
            DateTime dt = DateTime.Now;
            string DateValue = dt.ToString("yyyyMMddHHmmss");
            string FileName = string.Format("{0}_{1}.txt", UserName, DateValue);
    
            string FilePath = Path.Combine(SharedFolder, FileName);
    
            private void Accept_btn_Click(object sender, EventArgs e)
    
            {
    
            }
        }
    }

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Capturing confirmation from a splash screen

    If you simply move those variables inside Accept_btn_Click it should work.

    I'm not sure of the reason it is a problem at class level, but I would assume the compiler sometimes re-arranges the order that the variables are created (in order to optimise), which means it cannot be guaranteed that they will be created in the order they are needed to give them the correct values.

    Quote Originally Posted by shabbaranks View Post
    "a field initializer cannot reference the non-static field, method or property 'Form1.SharedFolder'.

    In layman's terms - what does this mean?
    "a field initializer" is setting the value of a variable on the same line you declare it.

    "non-static field" means a variable which is instance specific (so if you open two copies of the form, they might have different values).
    Last edited by si_the_geek; Oct 12th, 2017 at 06:44 AM.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Capturing confirmation from a splash screen

    Quote Originally Posted by si_the_geek View Post
    I'm not sure of the reason it is a problem at class level, but I would assume the compiler sometimes re-arranges the order that the variables are created (in order to optimise), which means it cannot be guaranteed that they will be created in the order they are needed to give them the correct values.
    Interestingly, that is allowed in VB. I suspect that your assumption is true but the VB compiler is written differently.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Location
    UK - OK
    Posts
    72

    Re: Capturing confirmation from a splash screen

    Again thanks, Im getting there (even added a cheeky little close form at the end). No errors this time but no text file either

    Code:
    using System;
    using System.Windows.Forms;
    using System.Security.Principal;
    using System.IO;
    
    namespace WindowsFormsApp1
    
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Accept_btn_Click(object sender, EventArgs e)
    
            {
                string SharedFolder = @"C:\Temp\to be deleted\Notification\";
                string UserName = Environment.UserName;
                DateTime dt = DateTime.Now;
                string DateValue = dt.ToString("yyyyMMddHHmmss");
                string FileName = string.Format("{0}_{1}.txt", UserName, DateValue);
    
                string FilePath = Path.Combine(SharedFolder, FileName);
                this.Close();
            }
        }
    }
    Is there a best practice "method" in diagnosing these kinds of issues where you don't get an error but you don't get the output either? I guess you could output to a message box or something?

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Capturing confirmation from a splash screen

    You're creating the file path but that's just a string containing the path of the file. You haven't opened/created the file at that location and written anything to it. You might consider call File.WriteAllText to open the file, write text to it and then close it again. There's also AppendAllText to do exactly what you'd expect. If you need to make use of the existing text rather than just overwrite it or append to it then you'll need to do a bit more, probably starting with a StreamReader.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Location
    UK - OK
    Posts
    72

    Re: Capturing confirmation from a splash screen

    Ah ha - gotcha thanks.... Will do some digging thanks

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Capturing confirmation from a splash screen

    The best way of diagnosing issues like that is done with debugging, here is a tutorial: https://msdn.microsoft.com/en-us/library/y740d9d3.aspx (you may not need it now, but I would strongly recommend at least keeping the link, because debugging is very important)

    However, in this case the issue is rather simple... you are setting up the variables (ending with FilePath having the full path to the file to save), but aren't saving it. Your code in post #4 saved a file, so you just need a modified version of that to use the FilePath variable.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Capturing confirmation from a splash screen

    @si_the_geek, too thlow, thucker!

  16. #16

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Location
    UK - OK
    Posts
    72

    Re: Capturing confirmation from a splash screen

    Guys,

    Just wanted to say a massive THANK YOU for your help with this. You didn't dismiss the fact my knowledge was a bit thin on the ground and tell me to Google - you helped me understand what was going on and come up with a working solution - thanks again.

    Added
    Code:
     using (StreamWriter writer = new StreamWriter(FilePath));
    and jobs a gooden...

  17. #17
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Capturing confirmation from a splash screen

    No problem, we're happy to help


    If it is all now sorted out, could you please do us a little favour, and mark the thread as Resolved?
    (this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)

    You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width