如何让这个命名空间的东西在GUI C#中工作# [英] How can I get this namespace thing to work in GUI C#

查看:67
本文介绍了如何让这个命名空间的东西在GUI C#中工作#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:)我正在为游戏制作一个简单的alt客户端,我差不多完成了,我所做的大部分工作都在我的能力之内。但是我需要添加商品价格查询。如何将其链接到GUI,我希望价格出现在文本框中。非常感谢提前。

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.

<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;
            }
        }        
    }
}





我尝试了什么:



我过去2天都在寻找答案。



What I have tried:

I've spent the past 2 days searching for answers.

推荐答案

你没有 - 你的类不是从Control派生的,所以它不能直接显示任何东西。我甚至不高兴使用MessageBox,我自己...

它与名称空间无关......



我们的想法是你的UI使用非UI类来组合数据以供显示;该类甚至不应该尝试显示数据,因为它应该不知道应该在哪个环境中找到它自己:网站可以使用与WinForms应用程序相同的类,作为WPF应用程序,作为Xamarin iOS应用程序而不更改类。



所以不要看这里,看看你的UI使用你的类来处理你的UI显示的数据。
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.


这篇关于如何让这个命名空间的东西在GUI C#中工作#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆