Dynamically adding a web user control to a web user control (asp.net vb) - c#-to-vb.net

I need to convert a C# web site to VB. There is a page that loads a custom web control (WholeLife.ascx) and then within this control, could possibly load multiple web user controls dynamically (WholeLifeChild.ascx).
The C# version works, the VB version just cannot seem to recognize the controls (WholeLifeChild.ascx) that it neess to load within the main control (WholeLife.ascx).
C# Version (works):
private WholeLifeChild _selectedChild;
// Later, looping with a foreach creates loads multiple childChoice:
WholeLifeChilds childChoice = (WholeLifeChilds)LoadControl("~/Controls/WholeLifeChild.ascx");
phChildChoices.Controls.Add(childChoice);
childChoice.ID = "ChildChoices-" + appRate.DependentID;
childChoice.Initialize(appRate, _WholeLifeCoverage);
childChoice.ChildChoicesChanged += new WholeLifeChild.ChangingHandler(ChildChoices_Changed);
VB Version (cannot find custom web user control):
Private _selectedChild As WholeLifeChild **(vb complains here on WholeLifeChild)**
'Later, looping with a foreach creates loads multiple childChoice:
(VB complains on the WholeLifeChild as if it cannot locate it)
Dim childChoice As WholeLifeChild = DirectCast(LoadControl("~/UserControls/WholeLifeChild.ascx"), WholeLifeChild)
phChildChoices.Controls.Add(childChoice)
childChoice.ID = "ChildChoices-" + appRate.DependentID
childChoice.Initialize(appRate, _WholeLifeCoverage)
childChoice.ChildChoicesChanged += New WholeLifeChild.ChangingHandler(AddressOf ChildChoices_Changed)
'This VB Version cannot find the WholeLifeChild.ascx control which is in the same directory as the control it's loading to (WholeLife.ascx).
Any help would be greatly appreciated.

Related

Is there a Geopoint class available for Winforms (or an Analogue thereof)?

In my UWP app, I use a Geopoint class:
using Windows.Devices.Geolocation;
. . .
List<Geopoint> locations;
In a Winforms app, this is not available - Geopoint is not recognized. Is there an analogous class available for Winforms apps?
The same is true for the BasicGeoposition object - not recognized.
UPDATE
I want the GeoPoint and BasicGeoposition classes so I can do things like this:
BasicGeoposition location = new BasicGeoposition();
location.Latitude = 36.59894360222391; // Monterey == 36.6002° N
location.Longitude = -121.8616426604813; // Monterey == 121.8947° W (West is negative)
Geopoint geop = new Geopoint(location);
await map.TrySetSceneAsync(MapScene.CreateFromLocation(geop));
cmbxZoomLevels.SelectedIndex = Convert.ToInt32(map.ZoomLevel - 1);
map.Style = MapStyle.Aerial3DWithRoads;
UPDATE 2
I tried the code provided in the answer:
this.UserControl1.myMap.AnimationLevel = AnimationLevel.Full;
this.userControl11.myMap.Loaded += MyMap_Loaded;
...but it won't compile. I don't have a UserControl11 (which is what the answer's code has), but I do have a UserControl1, yet it is not recognized:
This is the XAML in question (Bing Maps key obfuscated):
<UserControl x:Class="MyMaps.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF">
<Grid>
<m:Map CredentialsProvider="Gr8GooglyMoogly" x:Name="myMap" />
</Grid>
</UserControl>
To set the view of the Bing Maps WPF control, you can use SetView method. The method have different overloads, for example you can pass a Location(which you create based on the latitude and longitude of your desired location) and a zoom-level to the method like this:
var location = new Location(47.604, -122.329);
this.userControl11.myMap.SetView(location, 12);
Same can be achieved by setting Center and ZoomLevel.
Download or Clone the example
You can download or close the working example from here:
Clone r-aghaei/WinFormsWpfBingMaps
Download master.zip
Step by Step Example - Zoom into Seattle as initial view
Follow instructions in this post to create a Windows Forms project which uses WPF Bing Maps Control.
Handle the Load event of the Form and use the following code:
private void Form1_Load(object sender, EventArgs e)
{
this.userControl11.myMap.AnimationLevel = AnimationLevel.Full;
this.userControl11.myMap.Loaded += MyMap_Loaded;
}
private void MyMap_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
var location = new Location(47.604, -122.329);
this.userControl11.myMap.SetView(location, 12);
}
Make sure you use using Microsoft.Maps.MapControl.WPF;.
As a result, the map zooms in Seattle as center location:
More information:
You may want to take a look at the following links for more information:
How can I add a Bing Maps Component to my C# Winforms app?
Bing Maps WPF Control
Developing with the Bing Maps WPF Control
Bing Maps WPF Control API Reference
For those who are looking to use Windows Community Toolkit Map Control which is different from Bing Maps WPF Control, you can follow these steps to use Windows Community Toolkit Map Control for Windows Forms.
Note: Windows 10 (introduced v10.0.17709.0) is a prerequisite.
Create a Windows Forms Application (.NET Framework >=4.6.2 - I tried myself with 4.7.2)
Install Microsoft.Toolkit.Forms.UI.Controls NuGet package.
Add an app.manifest file: Right-click on project → Add New Item → Choose Application Manifest File (Windows Only) which is located under General node.
Open the app.manifest file and uncomment the supportedOS under <!-- Windows 10 -->:
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
Handle the Load event of your form and add the following code:
private void Form1_Load(object sender, EventArgs e)
{
var map = new MapControl();
map.Dock = DockStyle.Fill;
map.MapServiceToken = "YOUR KEY";
map.LoadingStatusChanged += async (obj, args) =>
{
if (map.LoadingStatus == MapLoadingStatus.Loaded)
{
var cityPosition = new BasicGeoposition() {
Latitude = 47.604, Longitude = -122.329 };
var cityCenter = new Geopoint(cityPosition);
await map.TrySetViewAsync(cityCenter, 12);
}
};
this.Controls.Add(map);
}
Also make sure you include required usings:
using Microsoft.Toolkit.Forms.UI.Controls;
using Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT;
Note 1: I was unable to add the control in designer because of an exception on design-time when I tried to drop the control on form, so I decided to use add it at run-time.
Note 2: You need to Get a Key to use map; however for test purpose you may ignore getting the key.
Run your application and see the result:
More information
MapControl for Windows Forms and WPF
Source code: Microsoft.Toolkit.Forms.UI.Controls.MapControl
WinForms control is a wrapper around WPF Windows.UI.Xaml.Controls.Maps.MapControl
Display maps with 2D, 3D, and Streetside views

WPF native windows 10 toasts

Using .NET WPF and Windows 10, is there a way to push a local toast notification onto the action center using c#? I've only seen people making custom dialogs for that but there must be a way to do it through the os.
You can use a NotifyIcon from System.Windows.Forms namespace like this:
class Test
{
private readonly NotifyIcon _notifyIcon;
public Test()
{
_notifyIcon = new NotifyIcon();
// Extracts your app's icon and uses it as notify icon
_notifyIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
// Hides the icon when the notification is closed
_notifyIcon.BalloonTipClosed += (s, e) => _notifyIcon.Visible = false;
}
public void ShowNotification()
{
_notifyIcon.Visible = true;
// Shows a notification with specified message and title
_notifyIcon.ShowBalloonTip(3000, "Title", "Message", ToolTipIcon.Info);
}
}
This should work since .NET Framework 1.1. Refer to this MSDN page for parameters of ShowBalloonTip.
As I found out, the first parameter of ShowBalloonTip (in my example that would be 3000 milliseconds) is generously ignored. Comments are appreciated ;)
I know this is an old post but I thought this might help someone that stumbles on this as I did when attempting to get Toast Notifications to work on Win 10.
This seems to be good outline to follow -
Send a local toast notification from desktop C# apps
I used that link along with this great blog post- Pop a Toast Notification in WPF using Win 10 APIs
to get my WPF app working on Win10. This is a much better solution vs the "old school" notify icon because you can add buttons to complete specific actions within your toasts even after the notification has entered the action center.
Note- the first link mentions "If you are using WiX" but it's really a requirement. You must create and install your Wix setup project before you Toasts will work. As the appUserModelId for your app needs to be registered first. The second link does not mention this unless you read my comments within it.
TIP- Once your app is installed you can verify the AppUserModelId by running this command on the run line shell:appsfolder . Make sure you are in the details view, next click View , Choose Details and ensure AppUserModeId is checked. Compare your AppUserModelId against other installed apps.
Here's a snipit of code that I used. One thing two note here, I did not install the "Notifications library" mentioned in step 7 of the first link because I prefer to use the raw XML.
private const String APP_ID = "YourCompanyName.YourAppName";
public static void CreateToast()
{
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(
ToastTemplateType.ToastImageAndText02);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
stringElements[0].AppendChild(toastXml.CreateTextNode("This is my title!!!!!!!!!!"));
stringElements[1].AppendChild(toastXml.CreateTextNode("This is my message!!!!!!!!!!!!"));
// Specify the absolute path to an image
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + #"\Your Path To File\Your Image Name.png";
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
imageElements[0].Attributes.GetNamedItem("src").NodeValue = filePath;
// Change default audio if desired - ref - https://learn.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-audio
XmlElement audio = toastXml.CreateElement("audio");
//audio.SetAttribute("src", "ms-winsoundevent:Notification.Reminder");
//audio.SetAttribute("src", "ms-winsoundevent:Notification.IM");
//audio.SetAttribute("src", "ms-winsoundevent:Notification.Mail"); // sounds like default
//audio.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Call7");
audio.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Call2");
//audio.SetAttribute("loop", "false");
// Add the audio element
toastXml.DocumentElement.AppendChild(audio);
XmlElement actions = toastXml.CreateElement("actions");
toastXml.DocumentElement.AppendChild(actions);
// Create a simple button to display on the toast
XmlElement action = toastXml.CreateElement("action");
actions.AppendChild(action);
action.SetAttribute("content", "Show details");
action.SetAttribute("arguments", "viewdetails");
// Create the toast
ToastNotification toast = new ToastNotification(toastXml);
// Show the toast. Be sure to specify the AppUserModelId
// on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
}
UPDATE
This seems to be working fine on windows 10
https://msdn.microsoft.com/library/windows/apps/windows.ui.notifications.toastnotificationmanager.aspx
you will need to add these nugets
Install-Package WindowsAPICodePack-Core
Install-Package WindowsAPICodePack-Shell
Add reference to:
C:\Program Files (x86)\Windows Kits\8.1\References\CommonConfiguration\Neutral\Windows.winmd
And
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
And use the following code:
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
for (int i = 0; i < stringElements.Length; i++)
{
stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
}
// Specify the absolute path to an image
string imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier("Toast Sample").Show(toast);
The original code can be found here: https://www.michaelcrump.net/pop-toast-notification-in-wpf/
I managed to gain access to the working API for windows 8 and 10 by referencing
Windows.winmd:
C:\Program Files (x86)\Windows Kits\8.0\References\CommonConfiguration\Neutral
This exposes Windows.UI.Notifications.
You can have a look at this post for creating a COM server that is needed in order to have notifications persisted in the AC with Win32 apps https://blogs.msdn.microsoft.com/tiles_and_toasts/2015/10/16/quickstart-handling-toast-activations-from-win32-apps-in-windows-10/.
A working sample can be found at https://github.com/WindowsNotifications/desktop-toasts

data source for report viewer control in WPF

I have a ReportViewer control in a WindowsFormsHost tag in my WPF application. When I use this code:
rptViewer1.LocalReport.ReportPath = ...
List<ReportParameter> parms = new List<ReportParameter>();
parms.Add(new ReportParameter("regionID", "01"));
rptViewer1.LocalReport.SetParameters(parms);
rptViewer1.RefreshReport();
I get an error about a data source instance not being supplied. I can run the stored procedure manually and then use it to populate a datasource object, like...
var dt = DAL.GetData()
var rds = new ReportDataSource("DataSet1", dt);
rptViewer1.LocalReport.DataSources.Add(rds);
And this will cause the report to display, but then I am passing in my parameters to the GetData() method rather than to the report; this doesn't seem right. In my SSRS project, I am using a shared datasource, and it allows me to pass in the parameters on the report front end as I would expect. What am I doing wrong?
If you are using ProcessingMode = Local, then YOU are responsible for large portions of teh report. You or your application defined which parameters there are, how data is loaded, & what sub-report or drill through events do. You must explicitly code these. If the ProcessingMode = Remote, then all of these elements are handled by the reporting server. Microsoft doesn't spell this out very clearly in MSDN, but I can see their justification being "if you are going to host the report in your app, then you can be responsible for all the details".

Embedding word 2010 editor in a wpf application

How do I use the word editor in a WPF application? Is it possible using windows forms hosting in WPF only? Is there another way to accomplish that?
I found AvalonEdit but it does not have features that I need. So using this way, my problem may not be solved.
Also there is some stuffs out there to host a windows forms control in WPF, but it could not be my answer.
I want to understand that is there a way to use word editor in a native way in a wpf app?
Will all APIs be available in that solution?
Thanks in advance.
You can host MS Word (2007/2010 and probably other versions) from within a WebBrowser control, this works in WinForms and should work in WPF too. A .NET API is provided for automating Word, documented here. The required interop assemblies ship with Office 2010, so deployment is a lot simpler than previous Office versions.
See this Microsoft Support article for more details on hosting Word within a WebBrowser control. The Screenshot below shows Word embedded within a host Winforms application.
Note that this only works reliably for a single hosted instance of Word, so you can't show 2 Word documents side by side in the same application. Also, the Ribbon can sometimes go missing - but Word hasn't ever caused the application to crash.
Administrative rights are required to make the required registry updates as there are potential security issues. One easy method to make the registry updates is to write a script, but the following (revised/untested) code shows how this can be done in c# for Word, Excel and PowerPoint:
using System.Security.AccessControl;
private Dictionary<string,uint> OfficeBrowserRegKeys()
{
string[] officeRegKeyArray = new string[]
{
#"SOFTWARE\Classes\Word.Document.12",
#"SOFTWARE\Classes\Word.DocumentMacroEnabled.12",
#"SOFTWARE\Classes\Excel.Sheet.12",
#"SOFTWARE\Classes\Excel.SheetMacroEnabled.12",
#"SOFTWARE\Classes\Excel.SheetBinaryMacroEnabled.12",
#"SOFTWARE\Classes\PowerPoint.Show.12",
#"SOFTWARE\Classes\PowerPoint.ShowMacroEnabled.12",
#"SOFTWARE\Classes\PowerPoint.SlideShow.12",
#"SOFTWARE\Classes\PowerPoint.SlideShowMacroEnabled.12"
};
Dictionary<string,uint> officeRegKeys = new Dictionary<string, uint>();
uint wrdVal = 0x80000024;
uint excelVal = 0x80000A00;
uint powerPtVal = 0x800000A0;
foreach(string keyName in officeRegKeyArray)
{
if (keyName.Contains("Word"))
{
officeRegKeys.Add(keyName, wrdVal);
}
else if (keyName.Contains("Excel"))
{
officeRegKeys.Add(keyName, excelVal);
}
else
{
officeRegKeys.Add(keyName, powerPtVal);
}
}
return officeRegKeys;
}
private void setNewOfficeKeys()
{
uint editFlag = 0x00010000;
Dictionary<string,uint> officeRegKeys = OfficeBrowserRegKeys();
foreach (KeyValuePair<string, uint> kvp in officeRegKeys)
{
try
{
RegistryKey rKey = Registry.LocalMachine.OpenSubKey(kvp.Key,
RegistryKeyPermissionCheck.ReadWriteSubTree,
System.Security.AccessControl.RegistryRights.SetValue);
rKey.SetValue("BrowserFlags", unchecked((int)kvp.Value),
RegistryValueKind.DWord);
rKey.SetValue("EditFlags", unchecked((int)editFlag),
RegistryValueKind.DWord);
}
catch (Exception e) { string msg = e.Message; }
}
}
Well, Word proper isn't technically designed to be hosted by another app, whether it's WPF, WINFORMS or anything else.
You CAN use api tricks (like SetParent) to move the Main Word window into a WPF hosted window. I've done it before, but it's pretty tricky business and it's very easy to miss things that cause GPFs (both in Word and your app).
Is there any reason why it needs to be "Word in your app"? Why not write a little word addin and then launch Word from your app when necessary. then the Addin can communicate with your app, or your DB or whatever as necessary from within Word.
Users may find that to be a more usable approach in any case.

Com Interop problem Silverlight 4 and MS Access 2010

I am trying to launch an existing MS Access database (Access 2010) from a Silverlight 4 OOB with elevated authorisation set. I keep getting an error. I can create a new Access application using the CreateObject keyword, but when I try to launch an existing one I get an error: "No object was found registered for specified ProgID."
Any help is appreciated. Here is the code I use:
string sMSAccess = "C:\\Users\\storltx\\Documents\\SL4Demo.accdb";
dynamic MSAccess = ComAutomationFactory.GetObject(sMSAccess);
MSAccess.Visible = true;
I think you should pass "Access.Application" string to GetObject call. like this:
dynamic MSAccess = ComAutomationFactory.GetObject("Access.Application");
Try your code like this:-
string sMSAccess = "C:\\Users\\storltx\\Documents\\SL4Demo.accdb";
dynamic app = ComAutomationFactory.CreateObject("Access.Application");
app .Visible = true;
app.OpenCurrentDatabase(sMSAccess);

Resources