In a WPF application, I have a webbrowser called WebBrowser1. This refers to an HTML page which contains a TextArea to which users can input text.
<html>
<body>
<textarea class="myStudentInput" id="myStudentInput1">
Text to be copied
</textarea>
</body>
</html>
I wish to get this text and potentially also set this text.
I have tried something similar to the javascript way of writing it:
document.getElementById("myStudentOutput1").innerHTML;
such as
HtmlElement textArea = webBrowser1.Document.All["myStudentInput1"];
dynamic textArea = WebBrowser1.Document.GetElementsByID("myStudentInput1").InnerText;
but it doesn't work.
The following solution in Visual Studio 2015 WPF Application works for me.
First, add a reference to the Microsoft HTML COM Library. This is on the COM tab, when you do an "Add Reference" in your project.
Then add the code:
<Window x:Class="WpfApplication3.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:WpfApplication3"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800">
<Grid>
<WebBrowser x:Name="WebBrowser1" HorizontalAlignment="Left" Height="480" Margin="10,10,0,0" VerticalAlignment="Top" Width="770" Source="E:\Others\Dropbox\Programming\Questions.html"/>
<Button x:Name="mySetQuestionButton" Content="Set Question" HorizontalAlignment="Left" Margin="200,520,0,0" VerticalAlignment="Top" Width="75" Click="mySetQuestion"/>
<Button x:Name="myGetAnswerButton" Content="Get Answer" HorizontalAlignment="Left" Margin="350,520,0,0" VerticalAlignment="Top" Width="75" Click="myGetAnswer"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="600,520,0,0" TextWrapping="Wrap" Text="Hello2" VerticalAlignment="Top"/>
</Grid>
</Window>
and
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;
namespace WpfApplication3
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void mySetQuestion(object sender, EventArgs e)
{
mshtml.HTMLDocument document = (mshtml.HTMLDocument)WebBrowser1.Document;
mshtml.IHTMLElement textArea = document.getElementById("myQuestion1");
textArea.innerHTML = "What is 1+1?";
}
private void myGetAnswer(object sender, EventArgs e)
{
mshtml.HTMLDocument document = (mshtml.HTMLDocument)WebBrowser1.Document;
mshtml.IHTMLElement textArea = document.getElementById("myStudentInput1");
textBlock.Text = textArea.innerHTML;
}
}
}
but it doesn't work.
I have no idea what that could possibly mean. All you can get is a code snippet that does work:
public partial class Form1 : Form {
private WebBrowser webBrowser1;
private Button button1;
public Form1() {
button1 = new Button { Text = "Test" };
button1.Click += button1_Click;
this.Controls.Add(button1);
webBrowser1 = new WebBrowser { Dock = DockStyle.Fill };
webBrowser1.DocumentText = #"<html><body><textarea class=""myStudentInput"" id=""myStudentInput1"">Text to be copied</textarea></body></html>";
this.Controls.Add(webBrowser1);
}
private void button1_Click(object sender, EventArgs e) {
var elem = webBrowser1.Document.GetElementById("myStudentInput1");
MessageBox.Show(elem.InnerText);
}
}
Which produces:
Related
I'm using Material design. I created a color picker to choose the color the user wants, after the user chooses the color and theme.
I want to save these settings into a text file on the disk. I don't know how can I convert these types to a list for the string which can I use for reading theme that is saved :
private void MyColorPicker1_PreviewMouseMove(object sender, MouseEventArgs e)
{
string filepath = #"C:\Themses";
if (e.LeftButton == MouseButtonState.Pressed)
{
ITheme theme = _paletteHelper.GetTheme();
theme.SetPrimaryColor(Color.FromRgb(MyColorPicker1.Color.R, MyColorPicker1.Color.G, MyColorPicker1.Color.B)); //red
var Test = theme.GetBaseTheme();
// something here to write all setting inside of ITheme into the text file
//
_paletteHelper.SetTheme(theme);
}
}
How can I do that?
Full XAML:
<Window x:Class="WpfApp5.SettingThemsWins.MaterialThemSettingy"
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:WpfApp5.SettingThemsWins"
mc:Ignorable="d"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
Background="{DynamicResource MaterialDesignPaper}"
Title="Setting" Height="607" Width="1144" WindowStartupLocation="CenterScreen">
<Grid>
<materialDesign:ColorPicker x:Name="MyColorPicker1" HorizontalAlignment="Left" Margin="20,17,0,0" VerticalAlignment="Top" Height="353" Width="750" PreviewMouseMove="MyColorPicker1_PreviewMouseMove" />
<ToggleButton x:Name="ThemeActivationsBtn" Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Activation Of Dark Theme" IsChecked="False" Margin="110,380,0,0" Click="ThemeActivationsBtn_Click" HorizontalAlignment="Left" Width="63" Height="27" VerticalAlignment="Top" />
<Label Content="Dark Theme :" HorizontalAlignment="Left" Height="24" Margin="20,382,0,0" VerticalAlignment="Top" Width="85"/>
<Button x:Name="SaverThemy" Content="Save Theme" HorizontalAlignment="Left" Margin="200,375,0,0" VerticalAlignment="Top" Width="170" Click="SaverThemy_Click"/>
</Grid>
</Window>
Code behind:
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.IO;
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.Shapes;
namespace WpfApp5.SettingThemsWins
{
/// <summary>
/// Interaction logic for MaterialThemSettingy.xaml
/// </summary>
public partial class MaterialThemSettingy : Window
{
private readonly PaletteHelper _paletteHelper = new PaletteHelper();
bool isDark;
public MaterialThemSettingy()
{
InitializeComponent();
//EmptySampleWind.Window1 window1 = new EmptySampleWind.Window1();
//window1.Show();
}
public static IEnumerable<string> SortByLength(IEnumerable<string> e)
{
// Use LINQ to sort the array received and return a copy.
var sorted = from s in e
orderby s.Length ascending
select s;
return sorted;
}
private void MyColorPicker1_PreviewMouseMove(object sender, MouseEventArgs e)
{
string filepath = #"C:\Themses";
if (e.LeftButton == MouseButtonState.Pressed)
{
ITheme theme = _paletteHelper.GetTheme();
theme.SetPrimaryColor(Color.FromRgb(MyColorPicker1.Color.R, MyColorPicker1.Color.G, MyColorPicker1.Color.B)); //red
var Test = theme.GetBaseTheme();
// something here to write all setting inside of ITheme into the text file
//
_paletteHelper.SetTheme(theme);
}
}
private void ThemeActivationsBtn_Click(object sender, RoutedEventArgs e)
{
isDark = (bool)ThemeActivationsBtn.IsChecked;
if (isDark)
{
ITheme theme = _paletteHelper.GetTheme();
IBaseTheme baseTheme = isDark ? new MaterialDesignDarkTheme() : (IBaseTheme)new MaterialDesignLightTheme();
theme.SetBaseTheme(baseTheme);
_paletteHelper.SetTheme(theme);
}
else
{
ITheme theme = _paletteHelper.GetTheme();
IBaseTheme baseTheme = isDark ? new MaterialDesignDarkTheme() : (IBaseTheme)new MaterialDesignLightTheme();
theme.SetBaseTheme(baseTheme);
_paletteHelper.SetTheme(theme);
}
}
}
}
I want to play mp3 audio in my app but nothing is happening
my code is this
XAML:
<Page
x:Class="SunnahForKids.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SunnahForKids"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Black">
<StackPanel Width="900" Height="700" HorizontalAlignment="Center">
<MediaElement HorizontalAlignment="Left" Name="a" AutoPlay="False" Source="azan.mp3" Height="100" Margin="451,299,0,0" VerticalAlignment="Top" Width="100" Volume="100"/>
<Button Content="Button" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,676,0" Height="189" Click="Button_Click"/>
</StackPanel>
</Grid>
</Page>
And here is my .cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Data.Json;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Media.SpeechSynthesis;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace SunnahForKids
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
a.Play();
}
}
}
and I am struggling with it for 2 days need help . I also followed this link MediaElement in WinRT / Win8 does not work at all
to update my driver but it is up to date.Display driver in Intel R(Q35) express chipset family ( Microsoft Corporation WDDM-1.0) please get me out of here..
I tried your code and the code works, so something strange is going on. I would try to listen for the events mentioned in the link (example further down for devs looking for code example).
Check your event log on the computer (Windows => Applications), if you've missed updates for windows, and check that that mp3 will otherwise play.
Listen for the MediaFailed event (recommend that you do in the docs) and see if you can grab some information there.Code from msdn:
private void videoMediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
// Check the event arguments => e
// get HRESULT from event args
string hr = GetHresultFromErrorMessage(e);
// Handle media failed event appropriately
}
private string GetHresultFromErrorMessage(ExceptionRoutedEventArgs e)
{
String hr = String.Empty;
String token = "HRESULT - ";
const int hrLength = 10; // eg "0xFFFFFFFF"
int tokenPos = e.ErrorMessage.IndexOf(token, StringComparison.Ordinal);
if (tokenPos != -1)
{
hr = e.ErrorMessage.Substring(tokenPos + token.Length, hrLength);
}
return hr;
}
This question already has an answer here:
showing dynamic text in silverlight
(1 answer)
Closed 9 years ago.
I have a button which shows "click me!" on it. I want to see after clicking, it shows "Do not click on me!" permanently.
I get the below error message in buttonname_click() function and I do not know how to resolve it (I have spent so much time on it).
The name 'buttonname' does not exist in the current context
I am attaching Search.xaml and Search.xaml.cs in this question.
<sdk:Page
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:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:TMTemplate"
xmlns:mocal="Clr-namespace:DynamicNavigation"
DataContext="{Binding RelativeSource={RelativeSource self}}"
xmlns:ContactForm="clr-namespace:ContactForm"
mc:Ignorable="d"
x:Class="TMTemplate.Search"
Title="Search"
d:DesignWidth="640" d:DesignHeight="480" Height="530" Width="700">
<Grid x:Name="LayoutRoot" >
<StackPanel Margin="50,45,25,45">
<Button Name="buttonname" Content="click me!" Click="buttonname_Click" Margin= "0,100,0,0" Height="93" Width="518"></Button>
</StackPanel>
</Grid>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
namespace TMTemplate
{
public partial class Search : Page
{
public Search() {
InitializeComponent();
}
private void buttonname_Click(object sender, RoutedEventArgs e)
{
buttonname.Content = "Do not click on me!";
}
}
}
You could try this instead:
private void buttonname_Click(object sender, RoutedEventArgs e)
{
((Button)sender).Content = "Do not click on me!";
}
But, to run your code as is, you need to specify the name with x:Name instead of Name:
<Button x:Name="buttonname" Content="click me!" Click="buttonname_Click" Margin= "0,100,0,0" Height="93" Width="518"></Button>
<Button x:Name="buttonname" Content="click me!" Click="buttonname_Click" Margin= "0,100,0,0" Height="93" Width="518"></Button>
private void buttonname_Click(object sender, RoutedEventArgs e)
{
this.buttonname.Content = "Do not click on me!";
}
I am having trouble displaying a my data in DataGrid.
I've searched online and found one of the possible solutions or so it seems but still no luck.
I was wondering if you could tell me what's wrong with the code?
FIY: I am loading a .csv into DataTable and then exporting it to DataGrid.
Here is my code:
// DataLoader is a class that loads the data from .csv data file. I've tested it and it works for sure.
// MainWindow.xampl.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Data;
using System.IO;
namespace Splash
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DataLoader dataTable = new DataLoader();
public MainWindow()
{
InitializeComponent();
}
private void Data_Table(object sender, SelectionChangedEventArgs e)
{
string path = #"C:\Users\Lyukshins\Dropbox\PROGRAM_TEST\AUTOMATION\DATA\Database.csv";
FileInfo theFile = new FileInfo(path);
dataTable = new DataLoader();
DataTable table = dataTable.GetDataTableFromCsv(theFile);
dataGrid.ItemsSource = table.DefaultView;
dataGrid.AutoGenerateColumns = true;
}
private void Copy_Files_Click(object sender, RoutedEventArgs e)
{
}
private void Rename_Click(object sender, RoutedEventArgs e)
{
}
}
}
// XAML
<Window x:Class="Splash.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Splash" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="386" d:DesignWidth="1000" SizeToContent="WidthAndHeight" Name="Splash" ResizeMode="CanResizeWithGrip" Background="#FFBFDEBF">
<Grid>
<DataGrid Height="335" HorizontalAlignment="Left" Name="dataGrid" VerticalAlignment="Top" Width="475" SelectionChanged="Data_Table"/>
<Button Content="Copy Files" Height="54" HorizontalAlignment="Left" Margin="504,12,0,0" Name="Copy_Files" VerticalAlignment="Top" Width="301" Click="Copy_Files_Click" Foreground="Black" FontStyle="Normal" Background="LightSteelBlue" />
<Button Content="Rename Files" Height="54" HorizontalAlignment="Left" Margin="833,12,0,0" Name="Rename" VerticalAlignment="Top" Width="114" Click="Rename_Click" Background="LightSteelBlue" />
<Button Content="Distribute Files From Vendors" Height="235" HorizontalAlignment="Left" Margin="504,81,0,0" Name="Distribute_Files_From_Vendors" VerticalAlignment="Top" Width="301" Background="LightSteelBlue" />
<Button Content=" Create Project
Folders" Height="235" HorizontalAlignment="Left" Margin="833,81,0,0" Name="Create_Apollo_File_Structure" VerticalAlignment="Top" Width="114" HorizontalContentAlignment="Center" Background="LightSteelBlue" AllowDrop="False"></Button>
</Grid>
</Window>
It seemed that the problem was solved by setting
dataGrid.ItemsSource = table.DefaultView;
dataGrid.AutoGenerateColumns = true;
but it didn't work in my case.
Could you please help?
I was able to get the solution to the problem elsewhere.
The basic idea is that I failed to write a display function.
By simply adding function private void viewGrid(DataTable table) it solved my problem.
Here is the updated code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Data;
using System.IO;
namespace Splash
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DataLoader dataTable = new DataLoader();
public MainWindow()
{
string path = #"C:\Users\Lyukshins\Dropbox\PROGRAM_TEST\AUTOMATION\DATA\Database.csv";
FileInfo theFile = new FileInfo(path);
dataTable = new DataLoader();
DataTable table = dataTable.GetDataTableFromCsv(theFile);
InitializeComponent();
viewGrid(table);
}
private void viewGrid(DataTable table)
{
if (table.Columns.Count == 0)
MessageBox.Show("Error!");
else
dataGrid.ItemsSource = table.DefaultView;
}
private void Data_Table(object sender, SelectionChangedEventArgs e)
{
string path = #"C:\Users\Lyukshins\Dropbox\PROGRAM_TEST\AUTOMATION\DATA\Database.csv";
FileInfo theFile = new FileInfo(path);
dataTable = new DataLoader();
DataTable table = dataTable.GetDataTableFromCsv(theFile);
}
private void Copy_Files_Click(object sender, RoutedEventArgs e)
{
}
private void Rename_Click(object sender, RoutedEventArgs e)
{
}
}
}
I have been trying to add items to a comboxbox but cannot seem to get the code behind file to recognize the combobox added to the xaml. I am pretty sure I am missing something simple. Basically the xaml here illustrates an empty combobox. The code behind executes the service, waits for json to come back and deserialize it. Unfortunately I cannot get
xaml:
<navigation:Page x:Class="Growing.Views.Room"
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"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="950" d:DesignHeight="480"
Title="Home" Style="{StaticResource PageStyle}" DataContext="{Binding}" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" ShowGridLines="True" Background="#FF631C00">
<Grid.ColumnDefinitions>
</Grid.ColumnDefinitions>
<Rectangle Height="298" HorizontalAlignment="Left" Margin="195,94,0,0" Name="rect" Stroke="Black" StrokeThickness="2" VerticalAlignment="Top" Width="582" Fill="#FFAAAAAA" RadiusY="0.25" RadiusX="0.25" />
<sdk:Label Height="38" HorizontalAlignment="Left" Margin="387,160,0,0" Name="label1" VerticalAlignment="Top" Width="203" Content="Select a Room" FontSize="24" FontWeight="Bold" />
<sdk:Label Height="18" HorizontalAlignment="Left" Margin="312,240,0,0" Name="label2" VerticalAlignment="Top" Width="69" Content="Area:" FontSize="14" />
<ComboBox x:Name="RoomAreas" Height="23" HorizontalAlignment="Left" Margin="418,235,0,0" VerticalAlignment="Top" Width="209" />
</Grid>
The code behind:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Runtime.Serialization.Json;
using System.ServiceModel.Web;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Growing.DataConnectionRef;
namespace Growing.Views
{
public partial class Room : Page
{
public Room()
{
InitializeComponent();
//Asynchronously call the EndReceive Web Service to change the status of an existing open lot record
WebClient GRService = new WebClient();
GRService.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GRService_DownloadStringCompleted);
GRService.DownloadStringAsync(new Uri("/servicestack/GetAreas", UriKind.Relative));
}
static void GRService_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
List<Area> dataList = new List<Area>();
MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
DataContractJsonSerializer ser = new DataContractJsonSerializer(dataList.GetType());
dataList = ser.ReadObject(memoryStream) as List<Area>;
memoryStream.Close();
RoomAreas.ItemSource = dataList;
}
}
}
At RoomAreas.ItemSource I get an error An object reference is required for the non-static field, method, or property 'Growing.Views.Room.RoomAreas'
Sorry if this is hard to follow. Anyone have any ideas what might be going on here?
Thank you in advance!
Make GRService_DownloadStringCompleted method non static:
void GRService_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
List<Area> dataList = new List<Area>();
MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
DataContractJsonSerializer ser = new DataContractJsonSerializer(dataList.GetType());
dataList = ser.ReadObject(memoryStream) as List<Area>;
memoryStream.Close();
RoomAreas.ItemSource = dataList;
}
Try adding ItemsSource="{Binding}" to the combobox property within the Xaml