How to get selected index of toolstripbutton in c# - c#

I have a toolstrip control on a form and i programatically add buttons to the toolstrip control using the below code
toolStrip1.Visible = true;
ToolStripItem t = new ToolStripButton();
t.Text = client.EndPoint.ToString();
t.TextImageRelation = TextImageRelation.ImageAboveText;
t.BackgroundImage = Image.FromFile("" + Application.StartupPath + "ps1_new.PNG");
t.AutoSize = false;
t.Height = 67;
t.Width = 70;
t.BackgroundImageLayout = ImageLayout.Stretch;
t.TextAlign = ContentAlignment.BottomCenter;
toolStrip1.Items.Add(t);
Now i m trying to get the index of the toolstrip button when i click on it
note i can get the text of the clicked toolstripbutton using
e.ClickedItem.Text;

There isn't an index property on the toolstripitem click but you could do something like this
private void ToolStrip1_ItemClicked(object sender, EventArgs e)
{
MessageBox.Show(e.ClickedItem.Tag)
}
Where the Tag property is something you set as the index.

Related

Change text of a TextBox clicking a Button with Controls created at run-time

I'm trying to change the text of a TextBox when I click a Button: both Controls are dynamically created as run-time.
The Buttons and the TextBoxes are created every time I click on another Button.
The Name Property for each control is specified by the User, using a TextBox.
For example, the user inputs "Test1", then the Button is named btn_Test1, and the TextBox is named txt_Test1.
The Button should open a FolderBrowserDialog and after a selection has been made, the TextBox shows the path selected.
I'm using the following code:
protected void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
folderBrowserDialog.ShowDialog();
string TextName = button.Name.Replace("btn_", "txt_");
TextBox selectText = new TextBox();
selectText = this.Controls[TextName] as TextBox;
selectText.Text = folderBrowserDialog.SelectedPath;
}
however this part gives me null:
selectText = this.Controls[TextName] as TextBox;
I did check with the debugger when I create the controls, so TextName is setting the correct Name.
The Buttons and TextBoxes are inserted in a TabControls, the Tab Name is set to the value the user inputs, so the main TabControl gets 2 controls.
I'm using a hidden TabControl named "TabFolders" that will be the main reference for creating tab clones
I'm using this code:
private void CreateDynamicPathButtons(string TabName)
{
TabPage MyNewTab = new TabPage(TabName);
TabPage TabCopy1;
tabControlEmpresas.TabPages.Add(MyNewTab);
TabControl tc = new TabControl();
tc.Location = new System.Drawing.Point(6, 6);
tc.Size = TabFolders.Size;
for (int i = 0; i < TabFolders.TabCount; i++) {
TabFolders.SelectTab(i);
TabCopy1 = new TabPage(TabFolders.SelectedTab.Text);
foreach (Control c in TabFolders.SelectedTab.Controls) {
Control cNew = (Control)Activator.CreateInstance(c.GetType());
cNew.Text = c.Text;
cNew.Size = c.Size;
cNew.Location = new System.Drawing.Point(c.Location.X, c.Location.Y);
cNew.Visible = true;
if (cNew is TextBox) {
cNew.Name = "txt_" + MyNewTab.Text + "_" + TabFolders.SelectedTab.Text;
}
if (cNew is Button) {
cNew.Name = "btn_" + MyNewTab.Text + "_" + TabFolders.SelectedTab.Text;
cNew.Click += new EventHandler(button_Click);
}
TabCopy1.Controls.Add(cNew);
}
tc.TabPages.Add(TabCopy1);
}
MyNewTab.Controls.Add(tc);
}
After many attempts I did find a very simple solution.
TextBox selectText = new TextBox();
selectText = button.Parent.Controls[TextName] as TextBox;
The button parent hast all the controls.
Assuming that button is the Button control you're creating at run-time you mentioned, you're creating a TextBox control but you're not adding it to the Form.Controls collection (this.Controls.Add([Control])).
Also, you should assign a Location, using a logic that fits your current Layout, to position the newly created Controls. Otherwise, all new controls will be positioned one on top of the other. In the example, the new Control position is determined using a field (int ControlsAdded) that keeps track of the number of Controls created at run-time and add some basic layout logic.
But, if you want to keep a reference of these new Controls, you should add them to a List<Control> or some other collection that allows to select them if/when required.
int ControlsAdded = 0;
protected void button_Click(object sender, EventArgs e)
{
TextBox selectedText = new TextBox();
selectedText.Size = new Size(300, this.Font.Height);
selectedText.Location = new Point(100, ControlsAdded * selectedText.Height + 30);
ControlsAdded += 1;
this.Controls.Add(selectedText);
selectedText.BringToFront();
using (var fBD = new FolderBrowserDialog()) {
if (fBD.ShowDialog() == DialogResult.OK)
selectedText.Text = fBD.SelectedPath;
}
}
with selectText = this.Controls[TextName] as TextBox;, you are trying to find button with replaced name which is not available in this case, and hence it returns null. This is logical mistake.
also string TextName = button.Name.Replace("btn_", "txt_"); does not replace button name, it just assigns replaced string to TextName.
The proper implementation would be
protected void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
folderBrowserDialog.ShowDialog();
button.Name = button.Name.Replace("btn_", "txt_");
TextBox selectText = new TextBox();
selectText = this.Controls[button.Name] as TextBox;
selectText.Text = folderBrowserDialog.SelectedPath;
}

Opening in RichTextBox rather than Tab name

// add a module tab
private void add_mod_Click(object sender, EventArgs e)
{
int TabCount = 0;
int? index = searchIndex(mod_add_textbox.Text);
if (index == null)
{
RichTextBox new_rich = new RichTextBox();
new_rich.Dock = DockStyle.Fill;
TabPage NewPage = new TabPage();
TabCount += 1;
string DocumentText = mod_add_textbox.Text;
NewPage.Name = DocumentText;
NewPage.Text = DocumentText;
NewPage.Controls.Add(new_rich);
mod_tab.TabPages.Add(NewPage);
}
else
{
mod_tab.SelectedIndex = Convert.ToInt32(index);
}
}
private async void btn_file_note_Click(object sender, EventArgs e)
{
using(OpenFileDialog ofd = new OpenFileDialog() { Filter="Text Documents|*.txt", ValidateNames = true, Multiselect = false })
{
if(ofd.ShowDialog() == DialogResult.OK)
{
using (StreamReader sr = new StreamReader(ofd.FileName))
{
mod_tab.SelectedTab.Text = await sr.ReadToEndAsync();
}
}
}
}
The problem I am getting is that when I try to open a document it is opening it into the tab name rather than the rich text box inside the tab. I have changed the "mod_tab.SelectedTab" part to the name of the rich text box within the tab however I want it so whichever tab the user has selected it opens in there. Any suggestions? thank you.
You assigned the value to the Text property of selected tab. Instead you should assign the value to Text property of RichTextBox or use Load method of RichTextBox to load content. for example:
this.richTextBox1.Text = ....
Also when you create the tab and RichTextBox dynamically like you ar doing in your code, you can find it this way:
//It means: Find all RichTextBox control which are children of mod_tab.SelectedTab
//And return first of them.
var rtb = this.mod_tab.SelectedTab.Controls.OfType<RichTextBox>().FirstOrDefault();
rtb.Text = ...
Also this way:
//It means get the first child control of mod_tab.SelectedTab
//And convert it to RichTextBox.
var rtb = this.mod_tab.SelectedTab.Controls[0] as RichTextBox;
rtb.Text = ...

FlowLayoutPanel inside a tabControl

I have a tabControl and a flowLayoutPanel inside each tab.. When I drag and drop a file onto a tab it creates a button with the icon of the file dropped. But i have the option to create more tabs and I want to be able to drag files into the selected tab.. but the problem is the flowLayoutPanel when adding the button..
My code so far:
public Process myProcess = new Process();
FlowLayoutPanel fl_panel = new FlowLayoutPanel();
string path_app;
public Form1()
{
InitializeComponent();
//add the flowLayoutPanel on the first tab
fl_panel.Dock = DockStyle.Fill;
fl_panel.BringToFront();
tabPage1.Controls.Add(fl_panel);
this.DragEnter += new DragEventHandler(Form1_DragEnter);
this.DragDrop += new DragEventHandler(Form1_DragDrop);
}
void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
e.Effect = DragDropEffects.All;
}
void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
foreach (string s in fileList)
{
Button button = new Button();
button.Click += new EventHandler(this.button_Click);
fl_panel.Controls.Add(button);
path_app = String.Format("{0}", s);
button.Tag = path_app;
string filename = path_app;
Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(filename);
Bitmap bmp = icon.ToBitmap();
button.BackgroundImage = bmp;
button.Width = 60;
button.Height = 75;
button.FlatStyle = FlatStyle.Flat;
button.BackgroundImageLayout = ImageLayout.Stretch;
}
}
private void button_Click(object sender, System.EventArgs e)
{
String path_app = ((sender as Button).Tag as String);
myProcess.StartInfo.FileName = path_app;
myProcess.Start();
}
private void add_tab_btn_Click(object sender, EventArgs e)
{
//Create new tab with FLP inside
string title = Convert.ToString(textBox1.Text);
TabPage new_TabPage = new TabPage(title);
fl_panel.Dock = DockStyle.Fill;
fl_panel.BringToFront();
new_TabPage.Controls.Add(fl_panel);
tabControl1.TabPages.Add(new_TabPage);
}
}
If I use fl_panel.Controls.Add(button); it adds the buttons fine, on the first tab, but if I create a new tab I don't know how to use tabControl.SelectedTab with the fl_panel.Controls.Add(button) to add the buttons correctly on the selected tab.
You have to create a new FlowLayoutPanel for every tab:
FlowLayoutPanel fl_panel = new FlowLayoutPanel();
...
new_TabPage.Controls.Add(fl_panel);
And then you can cast the first element of the TabPage to the FlowLayoutPanel and access the Controls from there:
FlowLayoutPanel selectedFLP = (FlowLayoutPanel)tabControl.SelectedTab.Controls[0];
...

WPF / C# - Adding functionality to a button dynamically created inside a listbox

I have a button that adds this StackPanel to the listbox everytime it's clicked. In it is a button. I'm trying to figure out how to add code to this button that it's adding. Ideally I want the button to be a delete button, so it would delete that element (itself) in the list. I'm just trying to figure out how to add functionality to the button I'm dynamically creating. hope that makes sense
thanks for any help!
private void Button_Click_1(object sender, RoutedEventArgs e)
{
StackPanel stackPanel = new StackPanel();
stackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
CheckBox checkBox = new CheckBox();
checkBox.IsChecked = true;
TextBox textBox = new TextBox();
textBox.Width = 100;
textBox.Text = textBox1.Text;
Button button = new Button(); //HOW DO I ADD CODE TO THIS BUTTON?
stackPanel.Children.Add(checkBox);
stackPanel.Children.Add(textBox);
stackPanel.Children.Add(button); //HOW DO I ADD CODE TO THIS BUTTON?
listBox1.Items.Add(stackPanel);
}
You can programatically add a click handler to the button like this:
Button button = new Button(); //HOW DO I ADD CODE TO THIS BUTTON?
button.Click += btn_Click;
stackPanel.Children.Add(checkBox);
stackPanel.Children.Add(textBox);
stackPanel.Children.Add(button); //HOW DO I ADD CODE TO THIS BUTTON?
and then you need the click event handler
void btn_Click(object sender, System.Windows.RoutedEventArgs e)
{
// your code to execute when the button is clicked.
stackPanel.Items.Remove(button);
}
Try This.
Add Stackpanel that have textblock and Button
private void OnSaveClick(object sender, RoutedEventArgs e)
{
StackPanel stp = new StackPanel();
stp.Orientation = Orientation.Horizontal;
stp.Children.Add(new TextBlock()
{
Text = string.Format("Item {0}", lstitems.Items.Count),
HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch
});
Button btn = new Button();
btn.Content = string.Format("Delete Item {0}", lstitems.Items.Count);
btn.Height = 25;
btn.Width = 100;
btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
btn.Click += btnDeleteClick;
stp.Children.Add(btn);
lstitems.Items.Add(stp);
}
Delete Button Click handler
void btnDeleteClick(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
if (btn != null)
{
var st = FindParent<StackPanel> (btn); //stackpanel as we have added item as stackpanel.
if (st != null)
lstitems.Items.Remove(st);
}
}
To Find the Type to Object in the Visual Tree.
public T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindParent<T>(parent);
}
That is the simplest setup. Ideally you want more error handling etc.
Button button = new Button();
button.Click += (s, args) => { listBox1.Items.Remove(stackPanel); };

How to retrieve the name of a control created programmatically

in an app where it is possible to create a serie of buttons programmatically, how can I retrieve the name of one of these buttons once I click its context menu?
This is the piece of code:
private void addButton_Click(object sender, System.EventArgs e)
{
int y = (buttonIndex * 80) + 5;
btn.Name = "btn" + buttonIndex.ToString();
btn.Content = "button " + buttonIndex.ToString();
btn.Width = 440;
btn.Height = 100;
Thickness margin = new Thickness(0, y, 0, 0);
btn.Margin = margin;
// .. all other properties..
pivot1Grid.Children.Add(btn);
buttonIndex++;
AddContextMenuItems(btn);
}
private void AddContextMenuWithMenuItems(Button btn)
{
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItem1 = new MenuItem() { Header = "Edit", Tag = "Edit" };
MenuItem menuItem2 = new MenuItem() { Header = "Remove", Tag = "Remove" };
menuItem1.Click += new RoutedEventHandler(menuItem1_Click);
menuItem2.Click += new RoutedEventHandler(menuItem2_Click);
contextMenu.Items.Add(menuItem1);
contextMenu.Items.Add(menuItem2);
ContextMenuService.SetContextMenu(btn, contextMenu);
}
Now in the click event I should be able to get the name of the button clicked (it works in case the context menu is created directly in XAML):
void menuItem1_Click(object sender, RoutedEventArgs e)
{
string btnName = ((sender as MenuItem).Parent as ContextMenu).Name;
..
}
but it gives always an empty string.
Could you please tell me where is the mistake?
Thanks
In your code, you're casting sender.Parent to ContextMenu. So you know you are manipulating the ContextMenu object. Then why are you expecting to magically get the name of the button by querying the name of the context menu?
The easiest way in your case is to store your information in the Tag property of the context menu:
private void AddContextMenuWithMenuItems(Button btn)
{
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItem1 = new MenuItem() { Header = "Edit", Tag = "Edit" };
MenuItem menuItem2 = new MenuItem() { Header = "Remove", Tag = "Remove" };
menuItem1.Click += new RoutedEventHandler(menuItem1_Click);
menuItem2.Click += new RoutedEventHandler(menuItem2_Click);
contextMenu.Items.Add(menuItem1);
contextMenu.Items.Add(menuItem2);
// Store the name of the button in the Tag property of the context menu
contextMenu.Tag = btn.Name;
ContextMenuService.SetContextMenu(btn, contextMenu);
}
Then, in the event handler, you just have to retrieve the value you've set before:
void menuItem1_Click(object sender, RoutedEventArgs e)
{
var contextMenu = (ContextMenu)((MenuItem)sender).Parent;
string btnName = (string)contextMenu.Tag;
..
}

Categories

Resources