How to find a button by name - c#

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;
}

Related

UWP ListView Drag and Drop

I am going to drag an item from a ListView to drop a box to process something.
In this case, I am not able to get selected ListView item. Selected Index/Items always returns -1/null.
Note: I am able to get selected listview item when using SelectionChanged.
But not able to get drop event. Please advise.
The XAML source:
<ListView x:Name="lvMaster" CanDragItems="True" SelectionChanged="lvMaster_SelectionChanged" />
<Grid AllowDrop="True" Drop="Drop_Event" DragOver="DragOver_Event">
</Grid>
The C# source:
private void Drop_Event(object sender, DragEventArgs e)
{
lvObj = new ListView();
ListView)sender;
}
private void DragOver_Event(object sender, DragEventArgs e)
{
AcceptedOperation = DataPackageOperation.Copy;
DragUIOverride.IsCaptionVisible = true;
DragUIOverride.IsContentVisible = true;
}
You can register DragItemsStarting event or DragItemsCompleted event for your listview, then in its handler method, you could get all items you dragged.
private void SourceListView_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args)
{
var cc = args.Items;
}
Please refer to the official Drag and Drop sample for more details.
Please

Accessing a text box inside a user control

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!";
}

Select multiple items in listbox with button click c#

I have a listbox with multiple items in it (20). I often need to select 4 of them. Instead of clicking on each item in the list box to select it, I would like to just click a button beside the listbox and have it select the 4 items.
<ListBox Name="lbExample" SelectionMode="Multiple">
<ListBoxItem>a</ListBoxItem>
<ListBoxItem>b</ListBoxItem>
<ListBoxItem>c</ListBoxItem>
<ListBoxItem>d</ListBoxItem>
<ListBoxItem>e</ListBoxItem>
<ListBoxItem>f</ListBoxItem>
<ListBoxItem>g</ListBoxItem>
<ListBoxItem>h</ListBoxItem>
<ListBoxItem>i</ListBoxItem>
<ListBoxItem>j</ListBoxItem>
...
</ListBox>
<Button Name="btnSelectGroupOne" Click="btnSelectGroupOne_Click" Content="Group One"></Button>
I have tried the following (trying to select items by Index):
private void btnSelectGroupOne_Click(object sender, RoutedEventArgs e)
{
lbExample.SelectedItems.Add(0);
lbExample.SelectedItems.Add(1);
lbExample.SelectedItems.Add(2);
lbExample.SelectedItems.Add(3);
}
I have also tried by string:
private void btnSelectGroupOne_Click(object sender, RoutedEventArgs e)
{
lbExample.SelectedItems.Add("a");
lbExample.SelectedItems.Add("b");
lbExample.SelectedItems.Add("c");
lbExample.SelectedItems.Add("d");
}
When I try either of these nothing is highlighted in the listbox.
You need to pass the Item of the listbox in Add() method. You can do it like:
lbExample.SelectedItems.Add(lbExample.Items[0]);
lbExample.SelectedItems.Add(lbExample.Items[1]);
lbExample.SelectedItems.Add(lbExample.Items[2]);
lbExample.SelectedItems.Add(lbExample.Items[3]);
Try calling
lbExample.SetSelectedItems(new List<string>{lbExample.Items[0]});

How to find components in listbox item, in Windows Phone?

I need to retrieve components from a listbox item. Not from a Tap or selection changed events. Is there a simple way to achieve this?
If I remember correct, in Android you can just write:
layoutRoot.findViewById(R.id.name);
Is there a similar way to do this on Windows Phone?
Update: Here's what I tried so far, but does not work:
What Daniela said in option 5 seems to work when I have the ListBoxItem. So this works fine when I have for example a Tap event on the ListBoxItem:
private void ListBoxItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
ListBoxItem item = sender as ListBoxItem;
// I can now retrieve a component inside the ListBoxItem
TextBox textBox = item.FindName("myTextBox") as TextBox;
}
But I want to do this when not triggering any events from a ListBoxItem.
What JoonasL said looks like something I could use but I can't get it to work.
// will not compile ( Cannot implicitly convert type 'object' to... )
ListBoxItem item = x.Items[0];
// if cast the value will be null
ListBoxItem item = x.Items[0] as ListBoxItem;
// will return the object used to populate that ListBoxItem, not the actual ListBoxItem.
// item will have a ItemViewModel object.
List<ItemViewModel> list = ....
this.myListBox.ItemsSource = list;
var item = x.Items[0]
When searching on Google I found something that I could use to find a component inside a ListBoxItem but I think there should be a easier way. Here they use a VisualTreeHelper.
Edit:
add a name to your lisbox (x:Name="something")
Cast ALL items as a List or pick out an item and cast the item as the correct type. Example:
private void Button_Click(object sender, RoutedEventArgs e)
{
var list = yourListbox.Items;
var itemCastAsCorrectObjectInstance = (ItemViewModel)list.FirstOrDefault();
textblock.Text = itemCastAsCorrectObjectInstance.LineOne;
}
ItemViewModel is a class we have created, and a list of ItemViewModels are used as itemssource for the listbox.
Here is an app example I've made for you
There are several ways to do that (some of the examples are WPF, but code is quite similar, it's just to give you a general idea)
Create listbox in code and retrieve item as in the example provided my JoonasL
private void GetUserRecommendations()
{
var obj = _helper.GetList<Recommendations>(#"http://localhost:1613/Home/GetAllRecommendations");
_items.Clear();
foreach (var item in obj)
{
_items.Add(item);
}
itemListView.ItemsSource = _items;
}
Retrieve a selected item on a changed event (or other event bound to the listbox)
void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
var itemProperty = ((ListBoxItem) e.ClickedItem).SomeProperty;
}
Provide the listbox a name and access the items by refering to the name in the code
var item = itemListView.SelectedItem as SomeClass;
Access the selected item by binding to another element (XAML only)
<Border Margin="10" BorderBrush="Silver" BorderThickness="3" Padding="8">
<DockPanel>
<TextBlock>Choose a Color:</TextBlock>
<ComboBox Name="myComboBox" SelectedIndex="0">
<ComboBoxItem>Green</ComboBoxItem>
<ComboBoxItem>Blue</ComboBoxItem>
<ComboBoxItem>Red</ComboBoxItem>
</ComboBox>
<Canvas>
<Canvas.Background>
<Binding ElementName="myComboBox" Path="SelectedItem.Content"/>
</Canvas.Background>
</Canvas>
</DockPanel>
Search the layoutroot
var myTextBlock = (TextBlock)this.FindName("myTextBlock");
Or maybe something like:
private void Somepage_Loaded(object sender, RoutedEventArgs e)
{
if (SomeCondition())
{
var children = (sender as Panel).Children;
var child = (from Control child in children
where child.Name == "NameTextBox"
select child).First();
child.Focus();
}
ListBox x = new ListBox();
ListBoxItem item = x.Items[0];
Or do you want something else?
RadDataBoundListBox has an event called ItemTap (not sure about normal ListBox) but my solution went something like this:
private void FocusTextBox(object sender, ListBoxItemTapEventArgs e)
{
txtBox = e.Item.FindChildByType<TextBox>();
var item = itemListView.SelectedItem as PanoramaApp1.//Your Selected Item Type Here;
//Do Something With item Here
itemListView.SelectedItem = null;
}

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