Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,522 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,828 people online right now. Registration is fast and FREE... Join Now!




Creating log in form

 
Reply to this topicStart new topic

Creating log in form

nbarten
15 Aug, 2008 - 10:57 AM
Post #1

D.I.C Head
**

Joined: 30 Apr, 2007
Posts: 112



Thanked: 2 times
My Contributions
i'm a bit in trouble...

i have a form and another class. If i click on a button on the form, it creates an object of my own class, do some methods, etc.

I've also a multiline textbox on the form which is used for the logging of events. First i thought: i call a method from the object of my own class, and then after it has its work done, it gives back the result (log) so i can update it in the log textbox.

The problem is, i want to update the log already somewhere in the method of my own class. So not returning all stuff after all is done, but immediately updating the stuff.

Of course i can't access the textbox from the method in the class. So i wonder, how to do something like that? Which ways are there, and which one is the best?
User is offlineProfile CardPM
+Quote Post

djkitt
RE: Creating Log In Form
20 Aug, 2008 - 10:02 AM
Post #2

D.I.C Head
**

Joined: 22 May, 2008
Posts: 128



Thanked: 13 times
My Contributions
So, here are a couple of things you could do.

EASY:
You could make a public TextBox control in your class and set it equal to the forms textbox when you create an instance of your class. This way you can update the textbox whenever you want right in your class.

MORE INVOLVED:
You can add an event to your class and then subscribe to that event from your form.

This post has been edited by djkitt: 20 Aug, 2008 - 10:02 AM
User is offlineProfile CardPM
+Quote Post

nbarten
RE: Creating Log In Form
20 Aug, 2008 - 12:00 PM
Post #3

D.I.C Head
**

Joined: 30 Apr, 2007
Posts: 112



Thanked: 2 times
My Contributions
eh... i don't get you.

Can you explain the EASY part a bit more?

Let's say i have:

Form1.cs: frmMain class (automatically made by VS 2008)
FtpConnection.cs: FtpConnection class (selfmade)

and a method DoThis() in the FtpConnection class.

So in the frmMain class i make an instance (object) of FtpConnection, and then i call the DoThis() method, so like this:

FtpConnection con = new FtpConnection();
con.DoThis();

But in the method DoThis() i need to edit the Text property of the TextBox (which is only accessible in frmMain).

I can make the TextBox public, but that won't help me anything because i haven't an object in the DoThis() method to work with?


Hope you understand me, i'm not really good with english so it's difficult to explain. wink2.gif

Thanks anyway that someone replied - took a while.

This post has been edited by nbarten: 20 Aug, 2008 - 12:01 PM
User is offlineProfile CardPM
+Quote Post

djkitt
RE: Creating Log In Form
20 Aug, 2008 - 12:24 PM
Post #4

D.I.C Head
**

Joined: 22 May, 2008
Posts: 128



Thanked: 13 times
My Contributions
So, OK. Here are the details...

The class...
CODE

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class FtpConnection
    {
                                public TextBox TB;     // this is the public variable that will point to your form's textbox


        public void DoThis()
        {
            if (TB != null)
                TB.Text = "Some FTP Info...";
        }
    }
}


Form1...
CODE

            FtpConnection con = new FtpConnection();
            con.TB = this.txtFtpLog;
            con.DoThis();


Let me know if you have any more qustions. By the way, events are way more cooler...

Also, I understand your english fine.
Kitt

This post has been edited by djkitt: 20 Aug, 2008 - 12:27 PM
User is offlineProfile CardPM
+Quote Post

nbarten
RE: Creating Log In Form
20 Aug, 2008 - 12:50 PM
Post #5

D.I.C Head
**

Joined: 30 Apr, 2007
Posts: 112



Thanked: 2 times
My Contributions
Thanks++ smile.gif it works fine.

How works the other one? I know how to create events in the same class.
User is offlineProfile CardPM
+Quote Post

djkitt
RE: Creating Log In Form
20 Aug, 2008 - 02:32 PM
Post #6

D.I.C Head
**

Joined: 22 May, 2008
Posts: 128



Thanked: 13 times
My Contributions
QUOTE(nbarten @ 20 Aug, 2008 - 03:50 PM) *

Thanks++ smile.gif it works fine.

How works the other one? I know how to create events in the same class.


You're welcome.

The other way is a bit more convoluted, but there are benefits...

So, I just made a little sample project called DreamInCodeStuff and added a class called FtpConnection. I commented the code pretty heavilly, but if you have any questions feel free to ask...

First we set up the class...
CODE

using System;

namespace DreamInCodeStuff
{
    // First we create an EventArgs class defining the information we'll pass back to the subscribing form...
    public class FtpMessageEventArgs : EventArgs
    {
        // Here we are defining two strings to send, but you could send any data...
        // (I added the stamp just to illustrate that you can pass more than just the string for the textbox)
        private string dtTmStmp;
        private string msg;

        // This constructor fills the variables when the instance is created...
        public FtpMessageEventArgs(string dateTimeStamp, string message)
        {
            dtTmStmp = dateTimeStamp;
            msg = message;
        }

        // These readonly properties allow the subscribing form to get the info you are passing...
        public string DateTimeStamp
        {
            get { return dtTmStmp; }
        }
        public string Message
        {
            get { return msg; }
        }
    }

    // Next we have to create a delegate. This defines what our event method needs to look like.
    public delegate void FtpMessageEventHandler(object sender, FtpMessageEventArgs mArgs);


    class FtpConnection
    {
        // Here we declare our event based on the delegate we defined earlier.
        // We make the event public so that anyone using our class can subscribe
        // to it with their own handler routine...
        // Basically, this is just a pointer to a method on the subsrcibing form.
        // (Just like we made a TextBox TB to point to a textbox on a form
        //  when we did this the *EASY* way)
        public event FtpMessageEventHandler MessageReceivedEvent;

        // Here is your public method for testing the whole thing...
        public void DoThis()
        {
            string message = "Some FTP Info...";
            FtpMessageEventArgs tmArgs = new FtpMessageEventArgs(DateTime.Now.ToLocalTime().ToString(), message);
            // Make sure the event has been subscribed to (it actually points to a method.)
            if (MessageReceivedEvent != null)
                MessageReceivedEvent(this, tmArgs);
        }

    }
}



Then a form to test it from...
CODE

using System;
using System.Windows.Forms;

namespace DreamInCodeStuff
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // Here we define a method with the same return type and parameters as the FtpConnection classes event.
        // We will assign this method to the public event on the FtpConnection class.
        private void FtpMessageReceived(object sender, FtpMessageEventArgs args)
        {
            string timeStamp = args.DateTimeStamp.Trim();
            string message = args.Message.Trim();
            txtFtpInfo.Text = timeStamp + ": " + message;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FtpConnection con = new FtpConnection();
            // Point the event to our local method
            con.MessageReceivedEvent += new FtpMessageEventHandler(FtpMessageReceived);
            // test it..
            con.DoThis();
        }
    }
}


Hope this helps,

Kitt

User is offlineProfile CardPM
+Quote Post

nbarten
RE: Creating Log In Form
20 Aug, 2008 - 11:02 PM
Post #7

D.I.C Head
**

Joined: 30 Apr, 2007
Posts: 112



Thanked: 2 times
My Contributions
Well... i understand it, although i'm not that good with delegates wink2.gif

I'm going the easy part i think, thanx anyway.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 09:37PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month