Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi :) I'm making a simple alt client for a game, I'm almost done and most of what I've done has been within my ability. However I need to add an item price lookup. How can I link this to a GUI, I want the price to appear in a textbox. Thanks so much in advance.
C#
<pre>using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace GELookup
{
    public class GELookup
    {
        private ArrayList id;
        private ArrayList name;
        public List<string> Result;

        public GELookup(string Item, bool MultiPage, int Page)
        {
            try
            {
                Result = new List<string>();
                id = new ArrayList();
                name = new ArrayList();

                //Get GE HTML.
                string strPage = GetFile("http://itemdb-rs.runescape.com/results.ws?query=" + Item + "&sup=All&cat=All&sub=All&page=" + Page + "&vis=1&order=1&sortby=name&price=all&members=");
                string[] splitter = new string[] { "./viewitem.ws?obj=" };
                string[] array;

                //Split the HTML at the Items.
                array = strPage.Split(splitter, StringSplitOptions.None);
                splitter = new string[] { "\"> " };

                //Add the Items while cleaning up the names/ID's
                for (int i = 1; i < array.Length; i++)
                {
                    id.Add(array[i].Split(splitter, StringSplitOptions.None)[0]);
                    name.Add(array[i].Split(splitter, StringSplitOptions.None)[1].Split(new string[] { "</a>" }, StringSplitOptions.None)[0]);
                }

                //Add the Items to the Result List
                for (int i = 0; i < id.Count; i++)
                {
                    Result.Add(name[i].ToString()); ;
                }

                //If MultiPage is true find the last page number and repeat
                //the search with ParsePage() method.
                if (MultiPage)
                {
                    int Start = strPage.IndexOf("<td class=\"navmid\">\n1");
                    int Finish = strPage.IndexOf("</td>\n<td class=\"navright\">");
                    string Data = "blank";
                    if (Start > 0 && Finish > 0)
                    {
                        Data = strPage.Substring(Start);
                        Data = Data.Substring(0, Data.IndexOf("</td>\n"));
                        Data = Data.Substring(Data.LastIndexOf("bers=\">") + 7, 1);
                        string[] Data2 = Data.Split(new string[] { "\n<a href=\"./results.ws?query=" + Item + "&sup=All&cat=All&sub=All&page=2&vis=1&order=1&sortby=name&price=all&members=\">", "</a>", "<td>" }, StringSplitOptions.None);

                        List<object> _Results = new List<object>();

                        char[] number = Data.ToCharArray();
                        if (Char.IsNumber(number[0]))
                        {
                            int Count = Convert.ToInt32(Data);
                            for (int X = 2; X <= Count; X++)
                            {
                                _Results.Add(ParsePage(Item, X));
                            }

                            //All of the pages have been parsed so add it all to the Result List.
                            for (int i = 0; i < _Results.Count; i++)
                            {
                                Result.AddRange((List<string>)_Results[i]);
                            }
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("Error Code: 010.\r\nPlease post this Error Code in the Bug's\r\nSection of the forum.\r\nThank you.");
            }
        }

        public GELookup()
        { }

        //Self Explanitory.
        private string GetFile(string strURL)
        {
            try
            {
                WebRequest myWebRequest = WebRequest.Create(strURL);

                WebResponse myWebResponse = myWebRequest.GetResponse();

                Stream ReceiveStream = myWebResponse.GetResponseStream();

                Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

                StreamReader readStream = new StreamReader(ReceiveStream, encode);

                string strResponse = readStream.ReadToEnd();

                readStream.Close();

                myWebResponse.Close();
                return strResponse;
            }
           catch(Execption e)
            {
                MessageBox.Show(e.ToString());
                return null;
            }
        }
        
        //Basicly a copy of the Constructor only this only parses one page...
        private List<string> ParsePage(string Item, int Page)
        {
            try
            {
                ArrayList id1 = new ArrayList();
                ArrayList name1 = new ArrayList();
                List<string> Result1 = new List<string>();

                string strPage = GetFile("http://itemdb-rs.runescape.com/results.ws?query=" + Item + "&sup=All&cat=All&sub=All&page=" + Page + "&vis=1&order=1&sortby=name&price=all&members=");
                string[] splitter = new string[] { "./viewitem.ws?obj=" };
                string[] array;
                array = strPage.Split(splitter, StringSplitOptions.None);
                splitter = new string[] { "\"> " };
                for (int i = 1; i < array.Length; i++)
                {
                    id.Add(array[i].Split(splitter, StringSplitOptions.None)[0]);
                    name1.Add(array[i].Split(splitter, StringSplitOptions.None)[1].Split(new string[] { "</a>" }, StringSplitOptions.None)[0]);
                }

                for (int i = 0; i < name1.Count; i++)
                {
                    Result1.Add(name1[i].ToString()); ;
                }
                return Result1;
            }
            catch(Execption e)
            {
                MessageBox.Show(e.ToString());
                return null;
            }       
        }
        
        //Takes the Index of the Item in the ListBox and returns
        //A string array of prices and the picture.
        public string[] GetPrice(int ItemIndex)
        {
            try
            {
                string[] ReturnPrices = new string[6];
                int index = ItemIndex;
                string[] price = new string[3];
                string[] array;
                string[] splitter = new string[] { "price: " };
                if (index != -1)
                {
                    string page = GetFile("http://itemdb-rs.runescape.com/viewitem.ws?obj=" + id[index]);
                    array = page.Split(splitter, StringSplitOptions.None);
                    splitter = new string[] { "</span>" };
                    for (int i = 0; i < 3; i++)
                    {
                        price[i] = array[i + 1].Split(splitter, StringSplitOptions.None)[0];
                    }

                    Regex Regex = new Regex("\\d{4}_obj_big.gif?");
                    Match match = Regex.Match(page);
                    string matchResult = match.Value;
                    string DynamicId = matchResult.Remove(4);

                    ReturnPrices[0] = "Name: " + Result[index];
                    ReturnPrices[1] = "ID: " + id[index];
                    ReturnPrices[2] = "Minimum Price: " + price[0];
                    ReturnPrices[3] = "Market Price: " + price[1];
                    ReturnPrices[4] = "Maximum Price: " + price[2];
                    ReturnPrices[5] = GetImage(Convert.ToString(id[index]), DynamicId);
                }

                return ReturnPrices;
            }
            catch(Execption e)
            {
                MessageBox.Show(e.ToString());
                return null;
            }
        }
        
        //Gets the Image of Item with the ItemID and returns the path to the saved image.
        private string GetImage(string ItemID,string imageDynId)
        {
            try
            {
                DirectoryInfo Folder = new DirectoryInfo(Directory.GetCurrentDirectory() + @"\GeImages\");

                //Check to see if that folder is there if not
                //create it.
                if (!Folder.Exists)
                {
                    Folder.Create();
                }

                //Check to see if the file has been downloaded before
                //if not download it, if so return the path.
                if (!File.Exists(Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + ".gif"))
                {
                    WebClient objClient = new WebClient();
                    objClient.DownloadFile(@"http://itemdb-rs.runescape.com/" + imageDynId + "_obj_big.gif?id=" + ItemID, Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + ".gif");
                }


                return Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + ".gif";
            }
            catch(Execption e)
            {
                MessageBox.Show(e.ToString());
                return null;
            }
        }

        public string[] GetGraph(string ItemID)
        {
            try
            {
                DirectoryInfo Folder = new DirectoryInfo(Directory.GetCurrentDirectory() + @"\GeImages\");

                //Check to see if that folder is there if not
                //create it.
                if (!Folder.Exists)
                {
                    Folder.Create();
                }

                string page = GetFile("http://itemdb-rs.runescape.com/viewitem.ws?obj=" + ItemID);
                Regex Regex = new Regex("\\d{4}_graphimg.gif?");
                Match match = Regex.Match(page);
                string matchResult = match.Value;
                string graphDynId = matchResult.Remove(4);

                Uri File1 = new Uri("http://itemdb-rs.runescape.com/" + graphDynId + "_scaleimg.gif?id=" + ItemID + "&scale=0&axis=0");
                Uri File2 = new Uri("http://itemdb-rs.runescape.com/" + graphDynId + "_graphimg.gif?id=" + ItemID + "&scale=0");
                Uri File3 = new Uri("http://itemdb-rs.runescape.com/" + graphDynId + "_scaleimg.gif?id=" + ItemID + "&scale=0&axis=2");

                WebClient objClient = new WebClient();
                objClient.DownloadFile(File1, Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + "Graph_Scale1.gif");
                objClient.DownloadFile(File2, Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + "Graph_Graph.gif");
                objClient.DownloadFile(File3, Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + "Graph_Scale2.gif");

                return new string[3] {Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + "Graph_Scale1.gif", Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + "Graph_Graph.gif", Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + "Graph_Scale2.gif"};
            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString());
                return null;
            }
        }        
    }
}


What I have tried:

I've spent the past 2 days searching for answers.
Posted
Updated 27-Oct-17 11:16am
Comments
j snooze 27-Oct-17 17:10pm    
Can't you just drag and drop a textbox onto your form and set its .text property to whatever the text is that you want?

1 solution

You don't - your class is not derived from a Control, so it can't display anything directly. I wouldn't even be happy about it using MessageBox, myself...
And it has nothing to do with namespaces either...

The idea is that your UI uses your non-UI classes to assemble data for it to display; the class should not even try to display data because it should have no idea what environment it should find it self in: a website could use the same classes as a WinForms app, as a WPF app, as a Xamarin iOS app without your classes changing.

So don't look here, look at your UI to use your classes to process the data for your UI to display for itself.
 
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