Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
i write code for reading data from serial port in smart device using C#(mobile application) in that i get the available COM Ports, i can also connecting and disconnecting the ports when sending data to com ports in that i can not transfer the data from sending text Box To Receiving text Box. write code for send button i am pasting here in that i can,t appending the data in smart device same code working C# window forms pl send code for append, text sending text Box To Receiving text Box*

in my code having
i used Two Labels in that
one Label for Available COM Ports
second Label for connect and disconnect show on this label
one combox
three buttons (connect,disconnect,send)
one Rich text box
one text box



C#
        //THSI IS SMART DEVICE PROJECT USING c#
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SerialPort port = new SerialPort();
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                #region Display all available COM Ports
                string[] ports = SerialPort.GetPortNames();
                // Add all port names to the combo box:
                foreach (string port in ports)
                {
                    //this.cboPortName.Items.Add(port);
                    this.cbbCOMPorts.Items.Add(port);
                }
                #endregion
                this.btnDisconnect.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnConnect_Click_1(object sender, EventArgs e)
        {
            if (port.IsOpen)
            {
                port.Close();
            }
            try
            {
                {
                    port.PortName = cbbCOMPorts.Text;
                    port.BaudRate = 96000;
                    port.Parity = Parity.None;
                    port.DataBits = 8;
                    port.StopBits = StopBits.One;
                }
                //.Encoding = System.Text.Encoding.Unicode 
                port.Open();
                lblMessage.Text = cbbCOMPorts.Text + " Connected. ";
                btnConnect.Enabled = false;
                btnDisconnect.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            txtDataToSend.Focus();
        }
        private void btnDisconnect_Click_1(object sender, EventArgs e)
        {
            try
            {
                port.Close();
                lblMessage.Text = port.PortName + " disconnected.";
                btnConnect.Enabled = true;
                btnDisconnect.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                port.Write(txtDataToSend.Text);
                //txtDataReceived->IS a data Received Text Box name 
               //txtDataToSend->is a data send Text Box name

                txtDataReceived.AppendText (txtDataToSend.Text ); // <<---- error here
                txtDataReceived.Text = txtDataReceived.Text + txtDataToSend.Text;//<--- i Tried but it not error but data cannot be appendtext in text box(txtDataReceived)
                txtDataReceived.ScrollToCaret();
               }
               txtDataToSend.Text = string.Empty;
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

       }


<-----------------------ERROR MESSAGE ----------------------->
-----WHEN I COMPILE OR RUN THE PROJECT IT SHOWING THE BELOW ERROR------
'System.Windows.Forms.TextBox' does not contain a definition for 'AppendText' and no extension method 'AppendText' accepting a first argument of type 'System.Windows.Forms.TextBox' could be found (are you missing a using directive or an assembly reference?)
Posted
Updated 12-Feb-10 3:37am
v23

Not that you've posted the error message you're getting, I googled "C# TextBox object", and on the MSDN site, I found that TextBox.AppendText() is not available until you use .Net 3.5 or higher.

You're using .Net 3.0...

The error message is telling you that the method simply isn't in the TextBox class.
 
Share this answer
 
JSOP, AppendText actually goes all the way back to .Net 1.0. It is just not available in .Net CF, which this person is likely using. Like I said before, the solution is to do:
C#
txtDataReceived.Text = txtDataReceived.Text + txtDataToSend.Text;

That will not cause a compile time error. If it does, then post what that error is and we can assist you further. You have only posted the error for AppendText, not for this line of code.
 
Share this answer
 
The following line is what is causing the error:
C#
txtDataReceived.AppendText (txtDataToSend.Text );

The following line is NOT causing the error:
C#
txtDataReceived.Text = txtDataReceived.Text + txtDataToSend.Text;

REMOVE the first line and replace it with the second line. Do not just add the second line. You need to REMOVE THE FIRST LINE.
 
Share this answer
 
You voted my answer a 1 because I pointed out a spelling mistake and your lack of information required to help you solve the problem?

Oh yeah, you still haven't mentioned WHAT the error is (and the appropriate term is "exception").

It'll probably throw an exception if the Text property you're using in AppendText is null.
 
Share this answer
 
v2
Right off the bat, I've noticed that you spelled the name of the method wrong.

It should be:

txtDataReceived.AppendText(txtDataToSend.Text);


Of course, you didn't say what object txtDataReceived is, so I'm just guessing here.
 
Share this answer
 
C#
txtDataReceived.Text = txtDataReceived.Text + txtDataToSend.Text;
 
Share this answer
 
You say you get an error with my suggestions? What is the new error message?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900