How to convert this code-based WPF tooltip to Silverlight? - c#

The following ToolTip code works in WPF.
I'm trying to get it to work in Silverlight.
But it gives me these errors:
TextBlock does not contain a definition for ToolTip.
Cursors does not contain a definition for Help.
ToolTipService does not contain a definition for SetInitialShowDelay.
How can I get this to work in Silverlight?
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace TestHover29282
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
AddCustomer("Jim Smith");
AddCustomer("Joe Jones");
AddCustomer("Angie Jones");
AddCustomer("Josh Smith");
}
void AddCustomer(string name)
{
TextBlock tb = new TextBlock();
tb.Text = name;
ToolTip tt = new ToolTip();
tt.Content = "This is some info on " + name + ".";
tb.ToolTip = tt;
tt.Cursor = Cursors.Help;
ToolTipService.SetInitialShowDelay(tb, 0);
MainStackPanel.Children.Add(tb);
}
}
}

Tooltips are added to Silverlight controls using an attached property provided by the ToolTipService. There is no SetInitialShowDelay in Silverlight's version nor is there a Help cursor on the Cursors type.
void AddCustomer(string name)
{
TextBlock tb = new TextBlock();
tb.Text = name;
ToolTip tt = new ToolTip();
tt.Content = "This is some info on " + name + ".";
ToolTipService.SetToolTip(tb, tt);
MainStackPanel.Children.Add(tb);
}

Related

Passing value from multiple textbox to string on another form or calling the value/string from textbox from another form [duplicate]

This question already has answers here:
Passing value from a form to another form (C# winforms)
(2 answers)
Closed 5 years ago.
I have searched high and low and I find almost what i need. I need to be able to get the values from form1's textboxes into a string and used in form4 to run something like a copy command or storescp in my case.
example:
Form1:
Public static string port1
port1 = Pt1.text;
dicompath = location.text;
Form4:
port1 = frm1.Port1.Text;
dicompath = frm1.location.text;
finalpath = port1 + " --fork -v -pm -fe .dcm -tn -sp -od " + ((char)34) + dicompath + ((char)34);
Process startInfo2 = new Process();
startInfo2.StartInfo.CreateNoWindow = true;
startInfo2.StartInfo.UseShellExecute = false;
startInfo2.StartInfo.RedirectStandardOutput = true;
startInfo2.StartInfo.RedirectStandardError = true;
startInfo2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo2.StartInfo.FileName = #"C:\dcmtk\bin\storescp-tls.exe";
startInfo2.StartInfo.Arguments = finalpath;
startInfo2.StartInfo.RedirectStandardOutput = true;
For some reason, I keep getting "Object reference not set to an instance of an object."
The way I read your question, your asking how to pass a user entered value from one form to another. So here is a minimal example that shows you the basic concepts of doing so.
Basically, you just need to make a form constructor that will accept parameters. You can then instantiate your extra form with this new constructor to use the value you are passing around.
In this snippet, I'm passing a value from Form1 to Form2 using Form2 form2 = new Form2(theText);
Form1.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace PassValuesBetweenForms_43923548
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CreateTextBox();
CreateSubmitButton();
}
private void CreateSubmitButton()
{
Button btn = new Button();
btn.Text = "click me";
btn.Click += ClickMeGotClicked;
btn.Location = new Point(5, 25);
this.Controls.Add(btn);
}
private void ClickMeGotClicked(object sender, EventArgs e)
{
string theText = ((TextBox)this.Controls["txtbx"]).Text;
Form2 form2 = new Form2(theText);//call the parametized constructor
form2.Show();
}
private void CreateTextBox()
{
TextBox tb = new TextBox();
tb.Location = new Point(5, 5);
tb.Width = 50;
tb.Name = "txtbx";
this.Controls.Add(tb);
}
}
}
Form2.cs
using System.Drawing;
using System.Windows.Forms;
namespace PassValuesBetweenForms_43923548
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//make a parametized constructor
public Form2(string incomingFromForm1)
{
InitializeComponent();
if (!string.IsNullOrEmpty(incomingFromForm1))
{
MakeALabelWithThis(incomingFromForm1);
}
}
private void MakeALabelWithThis(string incomingFromForm1)
{
Label lbl = new Label();
lbl.Text = incomingFromForm1;
lbl.Location = new Point(5,5);
this.Controls.Add(lbl);
}
}
}

How to bind panels with controls in devexpress docklayoutmanager

File Name SampleProject
My xaml file Mainwindow.xaml----------
<Window x:Class="SampleProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxdo="http://schemas.devexpress.com/winfx/2008/xaml/docking" Closing="Window_Closing"
Title="MainWindow" Height="800" Width="800" WindowState="Maximized">
<Grid>
<dxdo:DockLayoutManager x:Name="docklayoutmanager1" dxdo:RestoreLayoutOptions.RemoveOldPanels="False" dxdo:RestoreLayoutOptions.RemoveOldLayoutControlItems="False" Margin="0,50,0,0">
</dxdo:DockLayoutManager>
<Button Width="100" VerticalAlignment="Top" HorizontalAlignment="Center" Height="30" x:Name="ClickMe" Content="Click Me" Click="ClickMe_Click" Margin="323,10,369,729" />
</Grid>
</Window>
`
My xaml.cs file MainWindow.xaml.cs-------------------
using DevExpress.Xpf.Docking;
using DevExpress.Xpf.Layout.Core;
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.Navigation;
using System.Windows.Shapes;
namespace SampleProject
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
if(File.Exists("test.xml"))
{
docklayoutmanager1.RestoreLayoutFromXml("test.xml");
}
}
private void ClickMe_Click(object sender, RoutedEventArgs e)
{
var panel = docklayoutmanager1.DockController.AddPanel(DockType.None);
panel.Caption = "Test Panel";
panel.Name = "myPanel";
panel.Content = "I want to serialize this code using guid Please provide example for creating such binding.";
FloatGroup floatgroup = new FloatGroup();
floatgroup.Name = "myFloatGroup";
floatgroup.FloatSize = new Size(500,500);
floatgroup.FloatLocation = new Point(300, 300);
floatgroup.Add(panel);
docklayoutmanager1.FloatGroups.Add(floatgroup);
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
string xmlFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "test.xml";
if (File.Exists("test.xml"))
{
docklayoutmanager1.SaveLayoutToXml("test.xml");
}
else
{
using (File.Create("test.xml")) { }
docklayoutmanager1.SaveLayoutToXml("test.xml");
}
}
}
}
I have a document layout manager programmatically getting panels and controls which is having panels and controls in them.
While saving, I am achieving this with SaveLayoutToXml() for my panels and Serializing controls using bformatter.Serialize();.
since i have lot of panels and unique controls in them. I want to get the same controls back in same panels as it was before saving and serializing. Please provide me with a code to identify with a unique id for both panels and controls.
And do I have any integer id to which I can assign GUID as BindableName does not work with it.
Thanks
Desh
Create your panels programmatically and and them to your xaml file at run time.
UserC = new UserC();
DocumentPanel dpanel = new DocumentPanel();
dpanel.Name = "OpenUC" + count;
dpanel.Caption = "Open User Control";
dpanel.BindableName = "OpenUC" + count;
var content = UserC;
dpanel.Content = content;
FloatGroup fGroup = new FloatGroup();
fGroup.FloatSize = new Size(500, 500);
fGroup.Items.Add(dpanel);
DockLayoutManager1.FloatGroups.Add(fGroup);
This is how to add a User Control, since nobody seems to be interested in helping, i searched and done something like this:
private FloatGroup CreateDocPanelFloatGroup(DocumentPanel dp)
{
FloatGroup floatGroup = new FloatGroup();
floatGroup.FloatSize = new Size(500, 500);
floatGroup.Items.Add(dp);
return floatGroup;
}
private void OpenUC_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
{
openUC = new OpenUC();
DocumentPanel dp = new DocumentPanel();
dp.Name = "OpenUC" + count;
dp.Caption = "Open User Control";
dp.BindableName = "OpenUC" + count;
var content = openUC;
dp.Content = content;
DockLayoutManager1.FloatGroups.Add(CreateDocPanelFloatGroup(dp));
}

C# "Controls" Does Not Exist in The Current Context

Error
In my application I'm trying to use "Controls.Add", but Visual Studio keeps giving me the error:
"Error 1 The name 'Controls' does not exist in the current context
C:\Users\Admin\Desktop\Demo\Demo\MainWindow.xaml.cs 42 13 Demo"
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 Demo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 window = new Window1();
Button button = new Button();
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
button.Content = "New Button";
button.Visibility = Visibility.Visible;
button.Height = 50;
button.Width = 200;
Controls.add(button); <-- ERROR IS FOUND HERE
window.Show();
}
}
}
I'm using WPF, coming from WinForms. Any advice is appreciated, thanks.
You need to do something along the lines of:
var window = new Window1();
var stackPanel = new StackPanel { Orientation = Orientation.Vertical };
Button button = new Button();
button.Content = "New Button";
button.Visibility = Visibility.Visible;
button.Height = 50;
button.Width = 200;
stackPanel.Children.Add(button);
window.Content = stackPanel;
window.Show();
Although, I'd recommend defining all UI components in the XAML and reading up on the MVVM pattern.
<StackPanel Name=“splMain“>
<Button Name=“btnAddMore“ Click=“btnAddMore_Click“>Add Another</Button>
Also on button click :
System.Windows.Controls.Button newBtn = new Button();
newBtn.Content = “A New Button”;
splMain.Children.Add(newBtn);
Button SaveButton = new Button();
SaveButton.Name = "Save";
SaveButton.Content = "Save";
Grid1.Children.Add(SaveButton);

How can I get a FlowDocument Hyperlink to launch browser and go to URL in a WPF app?

The following code in a WPF app creates a hyperlink that looks and acts like a hyperlink, but doesn't do anything when clicked.
What do I have to change so that when I click it, it opens the default browser and goes to the specified URL?
alt text http://www.deviantsart.com/upload/4fbnq2.png
XAML:
<Window x:Class="TestLink238492.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel Margin="10">
<ContentControl x:Name="MainArea"/>
</StackPanel>
</Window>
Code Behind:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace TestLink238492
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
FlowDocumentScrollViewer fdsv = new FlowDocumentScrollViewer();
FlowDocument doc = new FlowDocument();
fdsv.Document = doc;
fdsv.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
doc.PagePadding = new Thickness(0);
Paragraph paragraph = new Paragraph();
doc.Blocks.Add(paragraph);
Run run = new Run("this is flow document text and ");
paragraph.Inlines.Add(run);
Run run2 = new Run("this is a hyperlink");
Hyperlink hlink = new Hyperlink(run2);
hlink.NavigateUri = new Uri("http://www.google.com");
paragraph.Inlines.Add(hlink);
StackPanel sp = new StackPanel();
TextBlock tb = new TextBlock();
tb.Text = "this is textblock text";
sp.Children.Add(tb);
sp.Children.Add(fdsv);
MainArea.Content = sp;
}
}
}
I found the answer to this one, you have to add RequestNavigate and handle it yourself:
Run run2 = new Run("this is a hyperlink");
Hyperlink hlink = new Hyperlink(run2);
hlink.NavigateUri = new Uri("http://www.google.com");
hlink.RequestNavigate += new System.Windows.Navigation.RequestNavigateEventHandler(hlink_RequestNavigate);
paragraph.Inlines.Add(hlink);
void hlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
Got the solutions for this Poma. The code section below should be added to your class where you need to do this. Or you can put it in a static class somewhere if you need to get to it from multiple files. I've tweaked it slightly for what I'm doing.
#region Activate Hyperlinks in the Rich Text box
//http://stackoverflow.com/questions/5465667/handle-all-hyperlinks-mouseenter-event-in-a-loaded-loose-flowdocument
void SubscribeToAllHyperlinks(FlowDocument flowDocument)
{
var hyperlinks = GetVisuals(flowDocument).OfType<Hyperlink>();
foreach (var link in hyperlinks)
link.RequestNavigate += new System.Windows.Navigation.RequestNavigateEventHandler(link_RequestNavigate);
}
public static IEnumerable<DependencyObject> GetVisuals(DependencyObject root)
{
foreach (var child in LogicalTreeHelper.GetChildren(root).OfType<DependencyObject>())
{
yield return child;
foreach (var descendants in GetVisuals(child))
yield return descendants;
}
}
void link_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
//http://stackoverflow.com/questions/2288999/how-can-i-get-a-flowdocument-hyperlink-to-launch-browser-and-go-to-url-in-a-wpf
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
#endregion Activate Hyperlinks in the Rich Text box
You'll call it in your code like this:
string xaml = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(this.itemControl.NotificationItem.Body, true);
FlowDocument flowDocument = XamlReader.Load(new XmlTextReader(new StringReader(xaml))) as FlowDocument;
SubscribeToAllHyperlinks(flowDocument);
bodyFlowDocument.Document = flowDocument;
All the HTMLConverter stuff can be found at: http://blogs.msdn.com/b/wpfsdk/archive/2006/05/25/606317.aspx
That's if you need to convert HTML to a Flow Document. Although, that's slightly out of the scope of this topic.

How can I make a WPF ToolTip appear faster with ToolTipService.Duration?

How can I make the Tooltip in the following code display faster?
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace TestHover29282
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
TextBlock tb = new TextBlock();
tb.Text = "Jim Smith";
ToolTip tt = new ToolTip();
tt.Content = "This is some info on the customer.";
tb.ToolTip = tt;
//ToolTipService tts = new ToolTipService();
//tts.Duration = 0;
//tb.ToolTipService = tts;
MainStackPanel.Children.Add(tb);
}
}
}
ToolTipService.SetInitialShowDelay(tb, 10);

Categories