Sending values to text box in form

This is a discussion on Sending values to text box in form within the C# Programming forums, part of the General Programming Boards category; Hello, I have a C# program that reads the ROM from a 1-wire device. This part is working fine so ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    52

    Sending values to text box in form

    Hello,

    I have a C# program that reads the ROM from a 1-wire device. This part is working fine so far. The issue I'm having is sending the data read from the device into a textbox on my form.

    Code:
    This is the Program.cs file that gets the data from the 1-wire chip.
    I can display the ROM data in the console when calling the function readDevice();, but I want to be able to send it to a textbox on my Form1.cs.
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Text;
    
    namespace uni_1_Wire_Programmer_Candela
    {
        static class Program
        {
            //This file must be located in the bin folder of the C# GUI program
            [DllImport("uni_1-Wire_ProgrammerDLL.dll")]
            public static extern int ComPortInit();
             [DllImport("uni_1-Wire_ProgrammerDLL.dll")]
            public static extern int readDevice();
    
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            public static void Main(string[] args)
            {
                ComPortInit();
                readDevice();
       
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
    This is the Form1.cs file and I want to be able to display the data acquired from the readDevice(); function on textBox4.

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



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



    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void label6_Click(object sender, EventArgs e)
    {

    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {

    }

    private void label5_Click(object sender, EventArgs e)
    {

    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

    }

    }
    }
    Any help will be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    Create a new constructor for Form1 that takes a string as an argument. In this constructor call InitializeComponent() just like the default constructor and after that set the text using textBox4.Text = theStringYouSentToConstructor

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    52
    This is the fuction. I'm not sure how to create the constructor, I'm new to C++/C#

    Code:
    int readDevice()
    {
    int rslt,i,cnt;
       int sendlen=0;
    
    	cnt = 0;
      
    	rslt = OWFirst();
    	//return(rslt);
       while (rslt)
       {
    	   cnt++;
           //print device found
    		//for (i = 7; i >= 0; i--)//use this to print out correctly
    		for (i = 0; i < 8; i++)
    		{
    			printf("%02X", ROM_NO[i]);
    			//ROM_NO_array_by_dev_found[cnt][i]=ROM_NO[i];
    		}	   
        // printf("  %d\n",cnt);
    	  rslt = OWNext();
       }   	
       return(cnt);
    }

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Code:
    public Form1()
    {
    InitializeComponent();
    }
    this is the default countructor of the Form1

    Code:
    Application.Run(new Form1());
    and this is the place there it is used


    you can add another constructor to the class

    Code:
    public Form1( string param)
    {
    InitializeComponent();
    
    //do something with the param here
    }

    and create the form using this constructor

    Code:
    string param = value read from device;
    Application.Run(new Form1(param));
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    52
    I have not been able to get this thing working.
    The readDevice() function is being called from a DLL file using the

    DllImport.

    Is there anyway that I could import just the ROM_NO[] array, this is were the data resides in the dll file once the function is called.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Is that your readDevice function? Is it changeable? If so, why not have it do something useful, like give you back the data instead of just printing it out? (As in pass some sort of array-like data structure that can get filled in?)

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    to get string from c-code to c# code you'll need something like this:
    Code:
    //c-code
    int test(char* sbuf, int bufSize)
    {
    	const char* data = "string to be returned";
    	if(strlen(data) < bufSize)
    	{
    		strcpy(sbuf,data);
    		return 0;
    	}
    	
    	return -1; //not enough buffer
    }
    
    //c# code
            [DllImport("test.dll")]
            private static extern int test(
               StringBuilder s,
               int bufSize);
    
            public int getstringfromdll(ref string result)
            {
                StringBuilder s = new StringBuilder(2048);
    
                int res = test(s, s.Capacity + 1);
                if(res == 0)
    				result = s.ToString();
                return res;
            }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    52
    Ok, so I finally got it working so far, it displays the string on the text box.
    The only issue left now is that I get characters in the text box and I want to see the hex values instead. The below code is what I use is C# to import the ROM ID from the 1-wire and display it in the text box.

    Code:
            [DllImport("uni_1-Wire_ProgrammerDLL.dll")]
            public static extern string Read_1WireString(int datasel);
    
            public Form1()
            {
                InitializeComponent();
              textBox4.Text = Read_1WireString(1);
                    }
    The above gives an output of -«Ūg in textBox4 which is the correct hex value for 2D AB DB 67 02 00 00 04 and the hex value is what I want dispalyed.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    You convert a string output such as yours to hex quite easily.

    Code:
    private String StringToHex(String str)
    {
        String result = String.Empty;
    
        foreach (byte b in Encoding.Default.GetBytes(str))
            result += String.Format("{0:X2}", b) + " ";
    
        return result;
    }
    So you'd use it like this...

    Code:
    textBox4.Text = StringToHex(Read_1WireString(1));
    Hope this helps.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    52
    Thanks, that worked, but it's only displaying five bytes 2D AB DB 67 02 and missing the rest of it 00 00 04

  11. #11
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Sounds like an encoding issue. Try Encoding.UTF8.GetBytes rather than Encoding.Default.GetBytes.

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    52
    It doesn't help with UTF8.

    Below is the function in the DLL that sends the string to the C# form.

    Code:
    unsigned char *_stdcall Read_1WireString(int datasel)
    {
    	switch(datasel)
    	{
    	case 1:
    		return ROM_NO; //ROM_NO is: [unsigned char ROM_NO[8]] and can't be changed.
    		break;
    
    			}
    }
    Also the C++ project to build the DLL file is set to Use Multi-Byte Character Set , don't know if that makes a difference.
    Last edited by drkidd22; 01-05-2011 at 02:12 PM.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Hmm, looking at your function, you're returning an unsigned char array which is not strictly the same as a string. The missing 4 bytes problem you're having exists in your function's return value's representation as a c# String object. It seems to be terminating when it reaches the first null.

    Perhaps it would be worth playing around with your p/invoke signature because I'm not convinced that a c# String is the best representation of an unsigned char array. I think a byte array (byte[]) would be more suitable than a String object. Plus you'd be able to convert the byte array to a hex string without character encoding being an issue.

    Sorry that I couldn't have been more help.

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Just create the hex string in C
    and return it olready ready to be displayed without converting in c#

    Code:
    unsigned char data[] = {0x2D, 0xAB, 0xDB, 0x67, 0x02, 0x00, 0x00,  0x04};
    char buf[100];
    int len = 0;
    int i;
    for(i = 0; i < sizeof(data)/sizeof data[0];i++)
    {
        int written = sprintf(buf+len,"%02X, ", data[i]);
        len += written;
    }
    len -= 2;
    buf[len] = 0; /* to remove last , */
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 06:24 AM
  2. Obtaining a value from a text box
    By thetinman in forum Windows Programming
    Replies: 1
    Last Post: 11-23-2006, 04:50 PM
  3. Limiting amount of text shown in console box
    By FingerPrint in forum C++ Programming
    Replies: 15
    Last Post: 03-30-2006, 12:39 AM
  4. Sending values to a control
    By Zyk0tiK in forum C Programming
    Replies: 6
    Last Post: 12-02-2005, 05:29 PM
  5. String from a text box
    By Garfield in forum Windows Programming
    Replies: 4
    Last Post: 09-16-2001, 03:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21