How to Find toolstripButton inside toolstrip in a UserControl - c#

I used this code below to Enable/Disable Button Control inside my usercontol on my Form which works perfect.
var btnAdd = this.userControlCommonTask1.Controls.Find("btnAdd", true);
btnAdd[0].Enabled = true;
But when i use toostrip(toolstrip1) with buttons (btnAdd,btnEdit,btndelete etch..) and use my code above
I got:
Index was outside the bounds of the array.
I tried this one but it only works in toolstrip.
var btnAdd = this.userControlCommonTask1.Controls.Find("toolstrip1", true);
btnAdd[0].Enabled = true;
Thanks in Regards

I already Solve my Problem:
var toolstrip1 = this.userControlCommonTask1.Controls.Find("toolstrip1", true);
var toolstrip1Items = toolstrip1[0] as ToolStrip; <-- set to toolstrip control
var btnRead = toolstrip1Items.Items.Find("btnRead", true); <--get BtnRead on toolstrip Item.Find
btnRead[0].Enabled = false; <--disable/Enable btn
This can be a reference for other Developers.
Cheers!

Try a 0 instead of 1 your array is zero based

Toolstrip is another usercontrol. Try make a reference to it and then find it's child controls
ie. ctlTooolStrip.Controls.Find("BtnAdd",true);
Also try toolStrip.Items

Related

Set none selected in Winforms TreeView component

Initially, I want nothing selected on my TreeView. By default, the first node is selected/focused.The code
treeView1.SelectedNode = null;
didn't work.
The tree node is not selected but has tap stop
treeView1.SelectedNode = null;
treeView1.TabStop = false;
Put your line of code
treeView1.SelectedNode = null;
in the Form.Shown event, rather than in the Form.Load event. The default selection occurs after Form.Load, so you want to clear the selection after that.
Put your line of code
TreeView.HideSelection = false;

Clicking on List Item on Dropdown

I'm having a problem in interacting with a custom dropdown control. It works fine the 1st 6 times, but after that, since the screen is resized, it could no longer locate and click the option in the dropdown control, returning an exception - can't click on hidden control. I tried putting in a itemField.DrawHighlight(); on the control I'm looking for, and it finds it, however it can't click on it. I also tried a to scroll down, but it seems to be not working.
bool addItemCheck = false;
int scrollCheck = 0;
while (Check == false)
{
var addItem= new HtmlButton(window);
addItem.SearchProperties.Add(HtmlButton.PropertyNames.Id, "add-new-item");
Mouse.Click(addItem);
scrollCheck = scrollCheck + 1;
if (scrollCheck > 6)
{
Mouse.MoveScrollWheel(window, -100);
}
var itemDropDown = new HtmlSpan(window);
itemDropDown .SearchProperties.Add(HtmlSpan.PropertyNames.Class, "item-dropdown");
itemDropDown .SearchProperties.Add(HtmlSpan.PropertyNames.InnerText, "Select an Item");
Mouse.Click(itemDropDown );
addItemCheck = itemDropDown.Exists;
}
bool itemBoxCheck = false;
HtmlCustom itemBox = null;
while (itemBoxCheck == false)
{
itemBox = new HtmlCustom(window);
itemBox.SearchProperties.Add(HtmlCustom.PropertyNames.Id, "item-listbox");
var itemField = new HtmlCustom(itemBox);
itemField .SearchProperties.Add(HtmlCustom.PropertyNames.InnerText, item);
Mouse.Click(itemField);
itemBoxCheck = itemBox.Exists;
}
I would really appreciate any help. Thank you.
Try calling the method InsureClickable() on the control before attempting to click on it.
for example:
itemDropDown.EnsureClickable();
Mouse.Click(itemDropDown);
Edit:
if this doesn't work you'll have to scroll down to the item.
try using:
Mouse.MoveScrollWheel()
if that doesn't work also you'll have to map the scroll control and click on it.

User Control c# Add Dynamically

I create a user control dynamically in my code
UserControl myobject = new UserControl();
myObject contains a button etc.
when I add this control to my picturebox
picturebox.Controls.Add(myobject);
my picturebox's backgorund image is dissappeared.
Why?
note: the button can be seen however. I want picturebox to be seen also
Set transparent basckground color of your user control. This will make picturebox visible:
UserControlDisplay myobject = new UserControlDisplay();
myobject.BackColor = Color.Transparent;
picturebox.Controls.Add(myobject);
BTW I believe you have different name of user control. And yes, as #samjudson stated, PictureBox should not be used this way: try to use Panel with background image instead (approach will stay same - use transparent color to see parent control):
panel.BackgroundImage = // your image
UserControlDisplay myobject = new UserControlDisplay();
myobject.BackColor = Color.Transparent;
panel.Controls.Add(myobject);
Try this:
UserControl myobject = new UserControl();
Button but = new Button();
but.BackColor = Color.Gray
pic.BackColor = Color.Green;
myobject.Controls.Add(but);
pic.Visible = true;
pic.Controls.Add(myobject);
The PictureBox control is not meant to be used as a container. Try adding a parent Panel or similar and the adding the PictureBox and your custom control to the Panel control.

Setting Active Control on a form to a toolbar

I wanted to set a toolbar to be the active control of the form, did something like this:
this.ActiveControl = this.Controls.Find("toolStrip2", false);
but it is saying it cann't contvert from Control[] to Cnotrol.
So how can I set that toolbar to be the active control?
The method Find returns array of Control instances whose name matches the string you pass to it. The first control in the array, if there is one, should be the one you are looking for.
You can do it using this code:
Control[] controls = this.Controls.Find("toolStrip2", false);
if (controls.Length > 0)
this.ActiveControl = controls[0];

get the control with a certain name provided as a string in c#

i have the name of a control in a string and I want to manipulate the control, how do i turn the string into the current form instance of that control in c#?
e.g.
string controlName = "Button1";
What goes here?
button1.text = "Changed";
Thanks
Button button1 = (Button)this.Controls[controlName];
You need to look up the control in the controls collections, then cast it to the correct type. Is this in WPF , WinForms or ASP.Net?
Inside the form, you could write (c#)
this.Controls["Button1"].Text = "Changed";
I suppose, this could be the syntax in vb.net
Me.Controls("Button1").Text = "Changed"
EDIT: I don't know, if that would compile. #Binary Worrier is right
Button btn1 = this.Controls["Button1"] as Button;
btn1.Text = "Changed";

Categories

Resources