Accessing a text box inside a user control - c#

I'm having problems binding a user control. I have a main window, which with a press of a button gets my value. However when i use my user control in the main window i cant set the text box to add my value. Instead of 'text' i want my user control text box? I have tried to declaring the text box but no luck any ideas?
private void button1_Click(object sender, RoutedEventArgs e)
{
text.Clear();
text.AppendText( tc.FTemp + "F");
}
<my:UserControl1 Height="172" HorizontalAlignment="Left" Margin="12,197,0,0" VerticalAlignment="Top" Width="151" Loaded="userControl11_Loaded" />

Two solutions:
Create a public property in your user control
Use 'Controls.Find' (the slowest, but easiest way)
public Form1()
{
InitializeComponent();
Control[] control = userControl1.Controls.Find("textbox1", true);
control.First().Text = "Found!";
}

Related

How to add text box value to hyperlink in C# on windows form?

I need to create a hyperlink that takes the input from textbox and display the web page.
textbox value is dynamically generated. The web page generated depends on the ext box value. Somebody can help Me please
Thanking you in Advance
Bare minimum, as you didn't specify which behavior do you expect, but it may work for you or give you a hind. Also check LinkLabel control description in MSDN
using System.Diagnostics;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
linkLabel.Text = "Click to search!";
linkLabel.LinkClicked += LinkLabel_LinkClicked;
}
private void LinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string googleSearch = #"https://www.google.com/?q=";
Process.Start(googleSearch + textBox.Text);
}
}
}
To check it just add TextBox, name it textBox and LinkLabel named linkLabel. When you enter something into text box and click on label google search with this phrase will start in your default browser.
For WPF,
In xaml code,
<Hyperlink RequestNavigate="Hyperlink_RequestNavigate" NavigateUri="https://www.google.de/search?q=">LINK
</Hyperlink>
</TextBlock>
Implement Hyperlink_RequestNavigate function in xaml.cs
private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri+textBox.Text));
e.Handled = true;
}
//textbox.Text contains the text to be appended

How to find a button by name

So, I have 2 buttons in XAML:
<Button x:Name="but1" />
<Button Click="button_Click" Tag="but1" />
and I want to fiind first button when second button is pressed and add it to a ListBox like this:
private void button_Click(object sender, RoutedEventArgs e)
{
//this is only how I imagine this to be
string buttonName = (sender as Button).Tag.ToString();
Button findedButton = Find(buttonName);
ListBox1.Items.Add(findedButton);
}
you can try something like this. Where Layout Root is your overall layout inside the layout you want to find an item by the name it will most likely return an object and you have to check if it a button then you found what you were looking for.
object item = LayoutRoot.FindName(buttonName);
if(item is Button)
{
Button btn=(Button)item;
}

Moving to the end of the text in a readonly TextBox

I currently have a TextBox in my WPF application that is readonly:
<TextBox x:Name="TextBox_CurrentDirectory" IsReadOnly="True"></TextBox>
And text gets updated in the Code Behind:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var app = Application.Current as App;
TextBox_CurrentDirectory.Text = app.ActiveDirectory;
//Show the end of the text here
}
Is there a way for me to show the end of the text programmatically? If the text in the TextBox is longer than the TextBox, it only shows the start and gets cut off. I'd like to be able to show the end of the text.
I tried using
TextBox_CurrentDirectory.CaretIndex = TextBox_CurrentDirectory.Text.Length;
but nothing happens.
You need to give your TextBox Focus before setting the CaretIndex.
TextBox_CurrentDirectory.Text = app.ActiveDirectory;
TextBox_CurrentDirectory.Focus();
TextBox_CurrentDirectory.CaretIndex = TextBox_CurrentDirectory.Text.Length;

In WPF, what is the best way to make buttons that insert special characters into multiple TextBoxes?

If I have multiple TextBoxes and other text insertion controls and I want to create some buttons that insert special characters into whichever TextBox is in focus, what is the best control for this and what properties should be set?
Requirements for the buttons:
Buttons do not steal focus when clicked.
Buttons can insert text (e.g. special characters) into any control that accepts keyboard input.
The cursor should move as if the user had entered the text on the keyboard.
If #2 is not possible, it will suffice to limit the controls to only TextBoxes.
NOTE: I do not want to make the buttons unfocusable, only such that they do not steal focus when clicked.
I'm not aware of any button that is not stealing focus when it is clicked, but in button click event handle you can return focus to previous owner. If I had to implement this I'd create an behavior that is attached to parent panel of all special textboxes and all buttons that are to insert some text.
<StackPanel>
<i:Interaction.Behaviors>
<local:TextBoxStateTracker/>
</i:Interaction.Behaviors>
<TextBox />
<Button Content="description" Tag="?" />
</StackPanel>
For sample simplicity I've put text that is to be inserted to textbox in Tag property.
public class TextBoxStateTracker : Behavior<Panel>
{
private TextBox _previouslySelectedElement;
private int _selectionStart;
private int _selectionLength;
protected override void OnAttached()
{
//after control and all its children are created find textboxes and buttons
AssociatedObject.Initialized += (x, y) =>
{
var textBoxElements = FindChildren<TextBox>(AssociatedObject);
foreach (var item in textBoxElements)
{
item.LostFocus += new RoutedEventHandler(item_LostFocus);
}
var buttons = FindChildren<Button>(AssociatedObject);
foreach (var item in buttons)
{
item.Click += new RoutedEventHandler(item_Click);
}
};
}
private void item_Click(object sender, RoutedEventArgs e)
{
if (_previouslySelectedElement == null) return;
//simply replace selected text in previously focused textbox with whatever is in tag property
var button = (Button)sender;
var textToInsert = (string)button.Tag;
_previouslySelectedElement.Text = _previouslySelectedElement.Text.Substring(0, _selectionStart)
+ textToInsert +
_previouslySelectedElement.Text.Substring(_selectionStart + _selectionLength);
_previouslySelectedElement.Focus();
_previouslySelectedElement.SelectionStart = _selectionStart + textToInsert.Length;
}
private void item_LostFocus(object sender, RoutedEventArgs e)
{
//this method is fired when textboxes loose their focus note that this
//might not be fired by button click
_previouslySelectedElement = (TextBox)sender;
_selectionStart = _previouslySelectedElement.SelectionStart;
_selectionLength = _previouslySelectedElement.SelectionLength;
}
public List<TChild> FindChildren<TChild>(DependencyObject d)
where TChild : DependencyObject
{
List<TChild> children = new List<TChild>();
int childCount = VisualTreeHelper.GetChildrenCount(d);
for (int i = 0; i < childCount; i++)
{
DependencyObject o = VisualTreeHelper.GetChild(d, i);
if (o is TChild)
children.Add(o as TChild);
foreach (TChild c in FindChildren<TChild>(o))
children.Add(c);
}
return children;
}
}
This does more or less what you described but it is far from perfect I think it is enough to get you started.
You need to do override the template of a Label and a TextBox.
requirements 1 and 2 - can be done inside the template for the Label which will act as a button.
requirement 3 - can be done inside the the template for the Textbox.
It's not easy...
you might need to learn alot of WPF styling, XAML and overriding the Control Template. maybe even creating a custom control.

C# Display a tooltip on disabled textbox (Form)

I am trying to get a tooltip to display on a disabled textbox during a mouse over. I know because the control is disabled the following won't work:
private void textBox5_MouseHover(object sender, EventArgs e)
{
// My tooltip display code here
}
How can I get the tooltip to display on a mouse over of a disabled control?
Many thanks
MouseHover wont fire if control is disabled. Instead you can check in Form MouseMove event whether you hover the textbox
public Form1()
{
InitializeComponent();
textBox1.Enabled = false;
toolTip.InitialDelay = 0;
}
private ToolTip toolTip = new ToolTip();
private bool isShown = false;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(textBox1 == this.GetChildAtPoint(e.Location))
{
if(!isShown)
{
toolTip.Show("MyToolTip", this, e.Location);
isShown = true;
}
}
else
{
toolTip.Hide(textBox1);
isShown = false;
}
}
Late to the party, but had the same problem and found a better solution: you can just wrap your TextBox in another Item and put a ToolTip on it like:
<Grid ToolTip="ToolTip to display">
<TextBox IsEnabled="False" Text="Text to display" />
</Grid>
You can also drag a ToolTip object from the Toolbox in designer onto the form.
Then in the code you just call SetToolTip() and pass in the button or text box etc. you want the tool tip to assign to and the text you want it to show.
myToolTip.SetToolTip(myTextBox, "You just hovered over myTextBox!");

Categories