locked
How to retrieve simple output from a URL? RRS feed

  • Question

  • Hi Developers,

    I am trying to write a simple program to retrieve the ouput when calling a URL and to present it when a button is clicked.

    This seems to be a rather simple topic, however, I am stuck und frustrated :) 

    I have written this as a console application without issues but I seem to have problem with developing a WPF application.

    The compiler throws the following error (I could not find any suitable solution for my code although a lot of information are available)


    Error CS1061 'RichTextBox' does not contain a definition for 'Text' and no accessible extension method 'Text' accepting a first argument of type 'RichTextBox' could be found (are you missing a using directive or an assembly reference?) GetWebsite URL C:\Users\c5219207\source\repos\GetWebsite URL\GetWebsite URL\MainWindow.xaml.cs 38 Active

    I have the following code:

    ###########################################        

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Net;
    using System.IO;

    namespace GetWebsite_URL
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }


            private void getMetar_Click_1(object sender, RoutedEventArgs e)
                    {
                        string url = metarUrl.Text;
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                        StreamReader sr = new StreamReader(response.GetResponseStream());
                        metarOutput.Text = sr.ReadToEnd();
                        sr.Close();

                    }



        }
    }

    ###########################################

    And the follwing for the xaml

    <Window x:Class="GetWebsite_URL.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:GetWebsite_URL"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <Button x:Name="getMetar" Content="Button" HorizontalAlignment="Left" Margin="56,84,0,0" VerticalAlignment="Top" Width="94" Height="33" Click="getMetar_Click_1"/>
            <TextBox x:Name="metarUrl" HorizontalAlignment="Left" Margin="191,84,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="237" Height="34"/>
            <RichTextBox x:Name="metarOutput" Margin="56,161,251,65"/>

        </Grid>
    </Window>

    Monday, January 27, 2020 9:51 AM

All replies

  • Try one of methods:

    <RichTextBox x:Name="metarOutput" Margin="56,161,251,65">

        <FlowDocument>

            <Paragraph>

                <Run x:Name="run1">

                </Run>

            </Paragraph>

        </FlowDocument>   

    </RichTextBox>

     

    . . .

    run1.Text = "some text";

     

    Or use a simpler TextBox if you do not need formatting.

    Monday, January 27, 2020 11:24 AM
  • Hi,

    You can use the folloiwng code-behind:

                Run r = new Run(sr.ReadToEnd());
                Paragraph para = new Paragraph();
                para.Inlines.Add(r);
                metarOutput.Document.Blocks.Clear();
                metarOutput.Document.Blocks.Add(para);

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 31, 2020 6:58 AM