1,080,668 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Posts by kimbokasteniv Contributing to Articles being Marked Solved

Well what you have looks fine, I just don't see any code which would take the information you got from the database and make it visible to the user... at least not in your btnGetAScheduledEvent_Click method.

Once you call the GetEventByID method of currentEvent, currentEvent should have data in its properties (assuming your database was there, and the event id matched an event).
So, you can get the data from currentEvent, by calling each property. For instance:

myOutputTextBox.Text = currentEvent.EventName

I think that was part of your problem? You were asking how to present the data back to the user?

As for getting data into the listBox, that's going to be different. I never done it in c#, but if you look at the MSDN page for ListBox, you should see all the methods and what type of data needs to be given to each method. Or someone here can give you a quick example...

kimbokasteniv
Junior Poster in Training
50 posts since Nov 2006
Reputation Points: 13
Solved Threads: 4
Skill Endorsements: 0

I don't think StringBuilder is appropriate here, as we need to treat each line as a separate IP endpoint... all this does is cram the contents of the file into a string without separating them. A List<string> would be much more useful. But you're going to want to build IPEndpoints anyway, so why not convert inline? Also, StreamReader.ReadLine will actually tell you when the file's done; no need to peek.

Improved code:

List<IPEndPoint> endpoints = new List<IPEndPoint>();
StreamReader reader = new StreamReader("filename.txt");

string line;
while((line = reader.ReadLine()) != null)
{
    string[] parts = line.Split(':');
    
    if(parts.Length == 2)
    {
        // We have two parts, which are hopefully an IP
        // address and a port number. Let's see...
        
        IPAddress address;
        int port;
        
        if(IPAddress.TryParse(parts[0], out address) &&
           Int32.TryParse(parts[1], out port))
        {
            // If both 'TryParse' calls succeeded, it's
            // a real endpoint, so add it to the list.
            
            endpoints.Add(new IPEndPoint(address, port));
        }
    }
}

// Now you have all of the valid IP endpoints from the file
// loaded into 'endpoints', ready to use to create connections.

Yup your solution is better. The bit of code I posted is something I just knew I already had.

kimbokasteniv
Junior Poster in Training
50 posts since Nov 2006
Reputation Points: 13
Solved Threads: 4
Skill Endorsements: 0

You can read a file with code that looks like this:

String infile1 = @"c:\ipAddresses.txt";
                StringBuilder currentWords = new StringBuilder();

                StreamReader sr = new StreamReader(infile1);

                try //read the file into lines of data
                {
                    do
                    {
                        currentWords.AppendLine(sr.ReadLine());
                    } while (sr.Peek() != -1);
                }
                catch
                {
                    MessageBox.Show("File [" + infile1 + "] is empty");
                }
                sr.Close(); //close the file
                String final = currentWords.ToString()

infile1 is the directory of your text file. StringBuilder is a special class designed for doing a lot of string concatenations. If you were to concatenate just a normal string many times, it would be very slow.
Once you have your final string, you can use the Split method to split the string into two strings around the colon.

kimbokasteniv
Junior Poster in Training
50 posts since Nov 2006
Reputation Points: 13
Solved Threads: 4
Skill Endorsements: 0

Your form class should look more like this:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        ScheduledEvent currentEvent;
        public Form1()
        {
            InitializeComponent();
            //currentEvent is going to be our scheduledEvent object which will store whatever scheduledEvent the user is currently looking at.
            //When you first instantiate currentEvent, there is nothing in it. You have to use GetEventByID to fill it.
            currentEvent = new ScheduledEvent();
        }

        private void getaScheduledEventButton_Click(object sender, EventArgs e)
        {
            //The user has pressed a button labelled "Find event". The event id typed in by the user into textBox1 is cast into an int and sent to the GetEventByID method.
            //IF the number corresponds to a real event id, then the fields of currentEvent will be updated.
            currentEvent.GetEventByID(int.Parse(textBox1.Text));
            //Assuming the fields of currentEvent have updated, output the fields of currentEvent to whatever other textboxes you have, so the user can see the information
        }

        private void getAllEventsButton_Click(object sender, EventArgs e)
        {
            //If the user presses a button named "Show all events on Date", use the ListEventsByDateOfEvent method. This should return all ScheduledEvent objects contained in
            //the database which have the same date as DateTime.Now.
            SortedList allEvents = currentEvent.ListEventsByDateOfEvent(DateTime.Now);
            
            //Once allEvents gets all of the events on a certain date, you need to show them to the user. Something like below will do that for you. You may have to put it in a loop
            //and change the '0' to an index which increments.
            listBox1.Items.Add(allEvents.GetByIndex(0));
        }
    }
}
kimbokasteniv
Junior Poster in Training
50 posts since Nov 2006
Reputation Points: 13
Solved Threads: 4
Skill Endorsements: 0
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Child c = new Child();
            c.MyEventHappened += new Child.MyEvent(c_MyEventHappened);
        }

        void c_MyEventHappened(int i)
        {
            textBox1.Text = "My event happened";
        }
    }

    class Child
    {
        public delegate void MyEvent(int i);
        public event MyEvent MyEventHappened;

        public Child()
        {
        }

        public void DoSomething()
        {
            //I've finished doing something I should let anyone who cares know
            MyEventHappened(1);
        }

    }

That should be basically what you need to do. The Child class has a delegate called 'MyEvent' with a specific signature. Then the Child class has an event called 'MyEventHappened" which uses MyEvent. So, when 'MyEventHappened' is called in 'DoSomething' anything attached to 'MyEventHappened" will also be called. And if you look in the 'Form1' class in the constructor 'c_MyEventHappened" is wrapped in the 'MyEvent' delegate and added to the 'MyEventHappened' event.
Make all the stuff in your child class first. Then go back to your form and attach to the event. Just a heads up, in Visual Studio when you type '+=' after referencing an event, it will automatically offer to write the code for you. Keep in mind that the signature of the delegate is up to you.

kimbokasteniv
Junior Poster in Training
50 posts since Nov 2006
Reputation Points: 13
Solved Threads: 4
Skill Endorsements: 0
 
© 2013 DaniWeb® LLC
Page generated in 0.0848 seconds using 2.52MB