Button not performing action

This is a discussion on Button not performing action within the C# Programming forums, part of the General Programming Boards category; OK, I have a button that when pushed, performs various tasks (gets info from an XML file on an http ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    16

    Button not performing action

    OK, I have a button that when pushed, performs various tasks (gets info from an XML file on an http server actually.) Before it does that I want it to show some output (for example, "please wait, getting file"). However... it's not doing that.

    Here's the button:
    Code:
    // 
    // buttonGetName
    // 
                this.buttonGetName.Location = new System.Drawing.Point(41, 38);
                this.buttonGetName.Name = "buttonGetName";
                this.buttonGetName.Size = new System.Drawing.Size(128, 23);
                this.buttonGetName.TabIndex = 0;
                this.buttonGetName.Text = "Get line-in name";
                this.buttonGetName.Click += new System.EventHandler(this.buttonGetName_Click);
    And here's the onclick handling code:
    Code:
            // if get name is clicked...
            private void buttonGetName_Click(object sender, EventArgs e)
            {
                labelInfo.Text = "Getting name..."; // WHY DOES THIS NOT HAPPEN?!
                labelHTML.Text = ""; // or this actually.
                string SoapAction = "SOAPACTION: \"urn:schemas-upnp-org:service:AudioIn:1#GetAudioInputName\"";
                string Data =
                    "<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                    +"    <s:Body>"
                    +"        <u:GetAudioInputName xmlns:u=\"urn:schemas-upnp-org:service:AudioIn:1\" />"
                    +"    </s:Body>"
                    +"</s:Envelope>";
                string name = LineInName(SoapAction, Data);
    
                // if error, display, otherwise show line-in name
                if (name.Substring(0, 6) == "ERROR:")
                {
                    labelHTML.Text = name;
                    labelInfo.Text = "Error.";
                } else {
                    labelInfo.Text = "Got name.";
                    textBoxName.Text = name;
                }
            }
    LineInName() is a function that actually connects to the HTTP server, sends a request, and recieves a request (which then gets output to textBoxName) - unless it's an error, which is then handled.

    Basically, I want to click buttonGetName, then I want labelInfo to show "Getting name..." (and clear labelHTML too) - and then access my LineInName() function. However, it's taking a while to get the file and giving no feedback while it does this.

  2. #2
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Please post the full code.
    To code is divine

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    16
    Sure thing. I'm pretty new to C#, so I'm not sure how much of my code you want. Here's everything I think you need:
    Code:
    #region Using directives
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Xml;
    using System.IO;
    
    #endregion
    
    namespace SLIRN
    {
        partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            // if get name is clicked...
            private void buttonGetName_Click(object sender, EventArgs e)
            {
                labelInfo.Text = "Getting name..."; // WHY DOES THIS NOT HAPPEN?!
                labelHTML.Text = "";
                string SoapAction = "SOAPACTION: \"urn:schemas-upnp-org:service:AudioIn:1#GetAudioInputName\"";
                string Data =
                    "<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                    +"    <s:Body>"
                    +"        <u:GetAudioInputName xmlns:u=\"urn:schemas-upnp-org:service:AudioIn:1\" />"
                    +"    </s:Body>"
                    +"</s:Envelope>";
                string name = LineInName(SoapAction, Data);
    
                // if error, display, otherwise show line-in name
                if (name.Substring(0, 6) == "ERROR:")
                {
                    labelHTML.Text = name;
                    labelInfo.Text = "Error.";
                } else {
                    labelInfo.Text = "Got name.";
                    textBoxName.Text = name;
                }
            }
    
            // if set name is clicked...
            private void buttonSetName_Click(object sender, EventArgs e)
            {
                labelInfo.Text = "Setting name...";
                labelHTML.Text = "";
                string SoapAction = "SOAPACTION: \"urn:schemas-upnp-org:service:AudioIn:1#SetAudioInputName\"";
                string Data =
                    "<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                    +"    <s:Body>"
                    +"        <u:SetAudioInputName xmlns:u=\"urn:schemas-upnp-org:service:AudioIn:1\">"
                    +"            <DesiredName>" + textBoxName.Text + "</DesiredName>"
                    +"        </u:SetAudioInputName>"
                    +"    </s:Body>"
                    +"</s:Envelope>";
                string name = LineInName(SoapAction, Data);
    
                // if error, display, otherwise show set name
                if (name.Substring(0, 6) == "ERROR: ")
                {
                    labelHTML.Text = name;
                    labelInfo.Text = "Error.";
                } else {
                    labelInfo.Text = "Set name.";
                }
            }
    
            // this object does the hard work. talking to ZP, performing action.
            public string LineInName(string SoapAction, string Data)
            {
                //why is this code here? i do not know. it was in the example.
                //probably resets the connection or something.
                System.Net.WebResponse response = null;
    
                try
                {
                    //get zp ip... or use my default
                    // TODO: check for correct IP format
                    string ip = textBoxIP.Text;
                    if (ip == "") ip = "10.0.1.136";
    
                    //connect to zoneplayer
                    System.Net.WebRequest request = System.Net.WebRequest.Create("http://" + ip + ":1400/AudioIn/Control");
                    request.Method = "POST";
                    request.ContentType = "text/xml ; charset=\"utf-8\"";
                    request.Timeout = 90000; //timeout. make larger if you don't go through.
                    request.Headers.Add(SoapAction); //tells the ZP whether we're setting or getting a name.
                    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Data);
                    request.ContentLength = bytes.Length;
                    Stream outputStream = request.GetRequestStream();
                    outputStream.Write(bytes, 0, bytes.Length);
    
                    //get response from http server (aka zoneplayer). dunno what most of this does.
                    response = request.GetResponse();
                    System.IO.Stream streamReceive = response.GetResponseStream();
                    System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("utf-8");
                    System.IO.StreamReader streamRead = new System.IO.StreamReader(streamReceive, encoding);
    
                    //return xml response to a nice variable
                    string returnedXml = streamRead.ReadToEnd();
                    
                    //find "currentname" in returned xml
                    int CurrentNameStart = returnedXml.IndexOf("<CurrentName>") + 13;
                    int CurrentNameEnd = returnedXml.IndexOf("</CurrentName>");
                    string name;
                    if (CurrentNameEnd != 0 && CurrentNameStart != 0) //should check for both variables. get an error when i do "var1 && var2" :(
                    {
                        name = returnedXml.Substring(CurrentNameStart, (CurrentNameEnd - CurrentNameStart));
                    } else {
                        name = "ERROR: Could not get CurrentName.";
                    }
    
                    //return xml for debug info. probably get rid of this or hide it later.
                    labelHTML.Text = returnedXml;
    
                    return name;
                }
                catch (Exception ex)
                {
                    //if error, give feedback. 
                    return "ERROR: " + ex.Message;
                }
                finally
                {
                    //not sure what this does either. closes connection to http server probably.
                    if (response != null)
                    {
                        response.Close();
                    }
                }
            }
        }
    }
    Code:
    namespace SLIRN
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.buttonGetName = new System.Windows.Forms.Button();
                this.buttonSetName = new System.Windows.Forms.Button();
                this.label1 = new System.Windows.Forms.Label();
                this.textBoxName = new System.Windows.Forms.TextBox();
                this.labelInfo = new System.Windows.Forms.Label();
                this.labelStatus = new System.Windows.Forms.Label();
                this.labelHTML = new System.Windows.Forms.Label();
                this.labelIP = new System.Windows.Forms.Label();
                this.textBoxIP = new System.Windows.Forms.TextBox();
                this.labelResponse = new System.Windows.Forms.Label();
                this.SuspendLayout();
    // 
    // buttonGetName
    // 
                this.buttonGetName.Location = new System.Drawing.Point(41, 38);
                this.buttonGetName.Name = "buttonGetName";
                this.buttonGetName.Size = new System.Drawing.Size(128, 23);
                this.buttonGetName.TabIndex = 0;
                this.buttonGetName.Text = "Get line-in name";
                this.buttonGetName.Click += new System.EventHandler(this.buttonGetName_Click);
    // 
    // buttonSetName
    // 
                this.buttonSetName.Location = new System.Drawing.Point(41, 68);
                this.buttonSetName.Name = "buttonSetName";
                this.buttonSetName.Size = new System.Drawing.Size(128, 23);
                this.buttonSetName.TabIndex = 1;
                this.buttonSetName.Text = "Set line-in name";
                this.buttonSetName.Click += new System.EventHandler(this.buttonSetName_Click);
    // 
    // label1
    // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(15, 122);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(73, 14);
                this.label1.TabIndex = 2;
                this.label1.Text = "Line-in name:";
    // 
    // textBoxName
    // 
                this.textBoxName.Location = new System.Drawing.Point(95, 119);
                this.textBoxName.Name = "textBoxName";
                this.textBoxName.TabIndex = 3;
    // 
    // labelInfo
    // 
                this.labelInfo.Location = new System.Drawing.Point(87, 98);
                this.labelInfo.Margin = new System.Windows.Forms.Padding(3, 3, 2, 3);
                this.labelInfo.Name = "labelInfo";
                this.labelInfo.Size = new System.Drawing.Size(120, 14);
                this.labelInfo.TabIndex = 4;
                this.labelInfo.Text = "Waiting for input";
    // 
    // labelStatus
    // 
                this.labelStatus.AutoSize = true;
                this.labelStatus.Location = new System.Drawing.Point(41, 98);
                this.labelStatus.Name = "labelStatus";
                this.labelStatus.Size = new System.Drawing.Size(39, 14);
                this.labelStatus.TabIndex = 5;
                this.labelStatus.Text = "Status:";
    // 
    // labelHTML
    // 
                this.labelHTML.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                this.labelHTML.Location = new System.Drawing.Point(211, 28);
                this.labelHTML.Margin = new System.Windows.Forms.Padding(1, 3, 3, 3);
                this.labelHTML.Name = "labelHTML";
                this.labelHTML.Size = new System.Drawing.Size(223, 140);
                this.labelHTML.TabIndex = 6;
    // 
    // labelIP
    // 
                this.labelIP.AutoSize = true;
                this.labelIP.Location = new System.Drawing.Point(9, 13);
                this.labelIP.Name = "labelIP";
                this.labelIP.Size = new System.Drawing.Size(79, 14);
                this.labelIP.TabIndex = 7;
                this.labelIP.Text = "ZonePlayer IP:";
    // 
    // textBoxIP
    // 
                this.textBoxIP.Location = new System.Drawing.Point(95, 10);
                this.textBoxIP.Name = "textBoxIP";
                this.textBoxIP.TabIndex = 8;
    // 
    // labelResponse
    // 
                this.labelResponse.AutoSize = true;
                this.labelResponse.Location = new System.Drawing.Point(211, 7);
                this.labelResponse.Name = "labelResponse";
                this.labelResponse.Size = new System.Drawing.Size(55, 14);
                this.labelResponse.TabIndex = 9;
                this.labelResponse.Text = "Response";
    // 
    // Form1
    // 
                this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                this.ClientSize = new System.Drawing.Size(442, 180);
                this.Controls.Add(this.labelResponse);
                this.Controls.Add(this.textBoxIP);
                this.Controls.Add(this.labelIP);
                this.Controls.Add(this.labelHTML);
                this.Controls.Add(this.labelStatus);
                this.Controls.Add(this.labelInfo);
                this.Controls.Add(this.textBoxName);
                this.Controls.Add(this.label1);
                this.Controls.Add(this.buttonSetName);
                this.Controls.Add(this.buttonGetName);
                this.Name = "Form1";
                this.Text = "Sonos Line-In ReNamer";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Button buttonGetName;
            private System.Windows.Forms.Button buttonSetName;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.TextBox textBoxName;
            private System.Windows.Forms.Label labelInfo;
            private System.Windows.Forms.Label labelStatus;
            private System.Windows.Forms.Label labelHTML;
            private System.Windows.Forms.Label labelIP;
            private System.Windows.Forms.TextBox textBoxIP;
            private System.Windows.Forms.Label labelResponse;
        }
    }
    Pity I can't attach the whole project as a zip, it'd save a lot of space I think. Hope that helps!

  4. #4
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    If they are in seperate files, try declaring the variables as static.
    To code is divine

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    16
    Everything (I think) is in one file. Visual C# 2005 Express Edition Beta just seems to not put everything on one page.

    What do you mean by declaring the variables as static? Should I set the button itself to static, or the command to change it?

  6. #6
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    I meant the button and label. However, I don't know why it isn't working. I made a test project that worked just fine using a button and label...hmm
    To code is divine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Retail Outlet Managment System - the 4th
    By Presidentofusa in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 10:44 PM
  2. need help on Calculator program in C
    By deathscyth129 in forum C Programming
    Replies: 8
    Last Post: 01-08-2005, 07:50 PM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 05:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 05:42 PM

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