Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 4th, 2008, 09:50 PM
Friend of Wrox
 
Join Date: Mar 2008
Location: So Cal is good enough for you, California, USA.
Posts: 133
Thanks: 15
Thanked 1 Time in 1 Post
Send a message via ICQ to iceman90289 Send a message via AIM to iceman90289
Default Scope Issue

Ok, i am going to try and explain this as accurately as i can, i have tried but i know i am not all that easy to understand.

This is the method that adds a tab to a tab control that is supposed to hold all the tabs representing an IM session. in my myMSN object, i have methods to sign me in, sign out, send messages, shake buddy, all the msn features.

private void tvMSNContacts_DoubleClick(object sender, EventArgs e)
        {

            Email = tvMSNContacts.SelectedNode.ToString();
            TabPage tcContact = new TabPage(Email);

            // Make a textbox in the tab to hold incomming and outgoing messages.
            TextBox tbConversation = new TextBox();
            tbConversation.Multiline = true;
            tbConversation.Dock = DockStyle.Top;
            tbConversation.Height += 200;
            tcContact.Controls.Add(tbConversation);

            TextBox tbMessageToSend = new TextBox();
            tbMessageToSend.Multiline = true;
            tbMessageToSend.Size = new Size(332, 91);
            tbMessageToSend.Location = new Point(6, 232);
            tbMessageToSend.TextChanged +=new EventHandler(tbMessageToSend_TextChanged);

            // Make a button to send the data to your buddy
            Button btnSendData = new Button();
            btnSendData.Text = "Send";
            btnSendData.Location = new Point(263, 329);
            btnSendData.Click +=new EventHandler(btnSendData_Click);

            // Make a button to close this tab.
            Button btnCloseTab = new Button();
            btnCloseTab.Text = "Close";
            btnCloseTab.Location = new Point(6, 329);
            btnCloseTab.Click += new EventHandler(btnCloseTab_Click);


            tcContact.Controls.Add(btnSendData);
            tcContact.Controls.Add(btnCloseTab);
            tcContact.Controls.Add(tbConversation);
            tcContact.Controls.Add(tbMessageToSend);

            tcMSNConversations.Controls.Add(tcContact);

            tcContact.Show();

            btnCloseTab.Show();
            btnSendData.Show();
            tbMessageToSend.Show();
            tbConversation.Show();
        }

  Here is the problem. My variables(they are private vars i made at the begining of my form1 class. Email is a string and MSNMessageToContact is a string as well. MSNMessageToContact is the text from the textbox you use to type messages. Look at the method: tbMessageToSend_EventHandler below

        private void btnSendData_Click(object sender, EventArgs e)
        {
            myMSN.SendMessage(Email, MSNMessageToContact);
        }

        private void btnCloseTab_Click(object sender, EventArgs e)
        {
            // Waiting for help...
        }
I figured it could update the string every time a character is entered but it complains at me. it says tbMessageToSend is a null reference. I have made a private Member var "private TextBox tbMessageToSend = null" thats where my problem is. but if you look at the first method i made, the tvMSNContacts_DoubleClick, tv is treeview, you will see i set tbMessageToSend = new TextBox. how do i get it to not disinitialize when it leaves that block? i need to
get tbMessageToSend to give me its text somehow, if you have a different design idea i am open to suggestions.
        private void tbMessageToSend_TextChanged(object sender, EventArgs e)
        {
            MSNMessageToContact = tbMessageToSend.Text;
        }

  #2 (permalink)  
Old April 5th, 2008, 02:27 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Code:
TextBox tbMessageToSend = new TextBox();
The above create a new variable, and assigns it a new TextBox - even if a variable of the same name already exists.

Code:
tbMessageToSend = new TextBox();
This sets an existing variable to a new TextBox.

Another alternative would be to get rid of the private member variable and change your event handler to this:

Code:
private void tbMessageToSend_TextChanged(object sender, EventArgs e)
{
    MSNMessageToContact = ((TextBox)sender).Text;
}
/- Sam Judson : Wrox Technical Editor -/
  #3 (permalink)  
Old April 5th, 2008, 12:33 PM
Friend of Wrox
 
Join Date: Mar 2008
Location: So Cal is good enough for you, California, USA.
Posts: 133
Thanks: 15
Thanked 1 Time in 1 Post
Send a message via ICQ to iceman90289 Send a message via AIM to iceman90289
Default

thanks that works

can u explain the code to me a little. it just looks strange to me.

also, i get a string from the name of a treenode(its the contacts email) and the string comes in the format "treenode <contact"
how do i make it just print the string without adding treenode?

  #4 (permalink)  
Old April 5th, 2008, 01:03 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

The revised event handler simply gets the textbox value.
Code:
private void tbMessageToSend_TextChanged(object sender, EventArgs e)
{
    MSNMessageToContact = ((TextBox)sender).Text;
}
Since TextChanged sends the control as an Object, you need to cast it to a TextBox to be able to access its TextBox properties. It's OK to go ahead and do this cast, because we know tbMessageToSend is a TextBox :)

Rob
The Developing Developer
Currently Working Towards: MCAD C#
My Blog: http://www.robzyc.spaces.live.com
<center>"Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".

Thomas Jefferson</center>
  #5 (permalink)  
Old April 5th, 2008, 01:17 PM
Friend of Wrox
 
Join Date: Mar 2008
Location: So Cal is good enough for you, California, USA.
Posts: 133
Thanks: 15
Thanked 1 Time in 1 Post
Send a message via ICQ to iceman90289 Send a message via AIM to iceman90289
Default

my textboxes work!!... mostly. i can send the message, it shows up in the conversationbox but my problem is in adds the strings together, i dont want it to look like this:
You said: HiContact said:sup

it puts the strings together,

i want
You said: Hi

Contact said: Sup

how do i make it look like that, the "\n" didnt work.

  #6 (permalink)  
Old April 5th, 2008, 01:34 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

How are you concatenating the strings and what are they being output to? (standard text box, etc.)

Remember, you need to demonstrate to us what you are doing so we can help you.

Rob
The Developing Developer
Currently Working Towards: MCAD C#
My Blog: http://www.robzyc.spaces.live.com
<center>"Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".

Thomas Jefferson</center>
  #7 (permalink)  
Old April 5th, 2008, 02:02 PM
Friend of Wrox
 
Join Date: Mar 2008
Location: So Cal is good enough for you, California, USA.
Posts: 133
Thanks: 15
Thanked 1 Time in 1 Post
Send a message via ICQ to iceman90289 Send a message via AIM to iceman90289
Default

here is the event handler for the SendData button. it takes the string out of my text box and puts in it the conversation box and also sends the string as a message to my contact. i am using the + symbol to add the strings together at the moment. i also added + "\n " to the end but it didnt work. it keeps adding, how do i programmatically hit the "enter button" on the control or something, so that it sets the '|' cursor to a new line so the next string that prints goes under it instead of attatched to it.

private void btnSendData_Click(object sender, EventArgs e)
        {
            myMSN.SendMessage(Email, MSNMessageToContact);
            tbConversation.Text += "You said: " + MSNMessageToContact + "\n"; // the \n doesn not show in the string or
// make set the cursor to next line.

// i have to reset these two variables so i dont send same messages
            tbMessageToSend.Text = "";
            MSNMessageToContact = "";
            // i want to give focus to the text box so the user doesnt have to click
            // it every time.
            tbMessageToSend.Focus();
        }


  #8 (permalink)  
Old April 5th, 2008, 02:03 PM
Friend of Wrox
 
Join Date: Mar 2008
Location: So Cal is good enough for you, California, USA.
Posts: 133
Thanks: 15
Thanked 1 Time in 1 Post
Send a message via ICQ to iceman90289 Send a message via AIM to iceman90289
Default

in the last post, i forgot to menion, yes they are just standard textboxes, with multiline set to true.

  #9 (permalink)  
Old April 5th, 2008, 03:41 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

OK, this thread has now gone way off-topic, I recommend you create a new thread for this question.

Rob
The Developing Developer
Currently Working Towards: MCAD C#
My Blog: http://www.robzyc.spaces.live.com
<center>"Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".

Thomas Jefferson</center>


Similar Threads
Thread Thread Starter Forum Replies Last Post
Static Scope JaneDean Beginning PHP 0 November 25th, 2008 02:07 AM
the scope of variable ccj_999 C++ Programming 9 October 26th, 2006 10:35 AM
DataSet Scope jbenson001 ASP.NET 1.x and 2.0 Application Design 2 December 5th, 2003 02:56 PM
Scope of array jakeone Beginning PHP 1 November 4th, 2003 08:10 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.