I have 100 buttons which created dynamically. Every button has same text and same name. Actually all of them is totally same. I added a property to my toolstrip menu called "color".
This is my function which must be change color.
private void colorfunc(object sender, MouseEventArgs e)
{
//
}
I created the toolstrip like this.
ContextMenuStrip menu= new ContextMenuStrip();
ToolStripMenuItem color= new ToolStripMenuItem("color");
color.Click += colorfunc;
menu.Items.AddRange(new ToolStripItem[] { color});
I want to do is when i right click to my button and after click the color item from toolstrip my button must be green. But i want to do is for only the button which is right clicked. Other 99 button must be stay like before. How to i do that?
Related
I wanted to ask you. I am writing a WPF program where I had a problem and had to ask you for advice. How can I dynamically add a button to a WPF window? Again, how can I assign the value of button_click inside the button by giving it a function. Thanks everyone in advance
You can use this
Button b = new Button();
b.Content = "some text"
//set other properties in a similar way
b.Click += (sender,e) => {/* your click handeler*/}
container.Children.Add(b);
where container is a Grid or something similar that you want to add your button to.
You can follow the steps below.
private void SomeEvent(object sender, RoutedEventArgs e)
{
//Your code here
}
Button newBtn = new Button(); //Initialize the button object
newBtn.Content = "Name"; //Give the poor button some text
newBtn.Click += SomeEvent; //Add a click event handler. It is a function which the button will perform when clicked.
MainGrid.Children.Add(newBtn); //Now add the button to your container. It could be a grid panel, stack panel or any other container.
You can also apply padding or margin to further control how the button appears. But that's optional. Cheers!
i want to add the button like plus beside the dropdown button like the picture
I don't have a problem writing the problematic code. I want to add a button in the outline design of the TreeListLookUpEdi. When I click on this button, which resembles the plus icon in the image, I add code.
Like opening a new window and adding new items to TreeListLookUpEdit
In the Runtime you can doing the following:
//add Plus Button
treeListLookUpEdit1.Properties.Buttons.Add(new EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Plus));
//add ButtonPressed Event
treeListLookUpEdit1.Properties.ButtonPressed += new ButtonPressedEventHandler(this.treeListLookUpEdit1_Properties_ButtonPressed);
private void treeListLookUpEdit1_Properties_ButtonPressed(object sender, ButtonPressedEventArgs e)
{
if (e.Button.Kind == ButtonPredefines.Plus)
{
//here your code
MessageBox.Show("Hello!");
}
}
I am currently creating an online shop on winform in c#.
At the moment I am creating a 'shopping basket' related textbox where if a user clicks on a particular radio button the textbox shows the description of the product in the text box.
I have grouped my radio buttons in a group box and would like to know whether there is anything equivalent to a 'SelectedIndex' command for all radio buttons? Thanks.
Simply subscribe all radio buttons to the same event. Then you can act on which is checked and act accordingly instead of having duplicate code for each button.
Below is a simple example that set a text box's Text property to display which is checked.
Form class
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButtons_CheckedChanged(object sender, EventArgs e)
{
//Do whatever you need to do here. I'm simple setting some text based off
//the name of the checked radio button.
System.Windows.Forms.RadioButton rb = (sender as System.Windows.Forms.RadioButton);
textBox1.Text = $"{rb.Name} is checked!";
}
}
In the .designer.cs file
//Note that the EventHandler for each is the same.
this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
If you want to be able to select more than one radiobutton at a time, I suggest you to use checkboxes instead of radiobuttons. You can assign all their events to the same single event and control which of the checkboxes are checked.
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkBoxControl = (CheckBox) sender; // You can use this variable to see which one of the checkbox is checked.
}
I am working on making a music player in c#. I am making music playlists right now and am stuck. As of right now I am using tabControl and a button that adds a tab with an empty listbox in it. Here is the code for that button:
private void button10_Click(object sender, EventArgs e)
{
TabPage tp = new TabPage("Playlist");
tabControl1.TabPages.Add(tp);
ListBox lb = new ListBox();
lb.Dock = DockStyle.Fill;
tp.Controls.Add(lb);
}
The problem I am running into is that I do not know how to allow the user to add music to these dynamically created listboxes within the tabs. The main list of music is located in a listbox in the first tab and I want the user to be able to select this music and put it in the new listboxes or "playlists" so I need to reference them somehow.
I'll just assume that you have a button (addToPlayListButton), a textBox (playListName) to add the selected song to the entered playList (tab-) name and that your songs listBox is called songList. I'll furthermore assume that every new playlist has a new tab. In that case you'll have to identify them so I'd change the name of the tabs:
TabPage tp = new TabPage($"Playlist {tabControl1.TabPages.Count}");
So you'll have to handle the button click event from addToPlayListButton like that:
private void onAddToPlayListButton_Click (object sender, EventArgs e) =>
(tabControl1.TabPages.Cast<TabPage>()
.FirstOrDefault(page => page.Text == playListName.Text)
?.Controls.Cast<Control>()
.FirstOrDefault(control => control is ListBox) as ListBox)?.Items.Add(songList.SelectedItem);
![enter image description here][1]I am trying to add a combobox to ContextMenu on a datagrid when a user right clicks, the menu will pop up. It should show exit and assign to bankID menu options where the bank ID is a combobox dropdown. I did some online search but couldn't find anything close which makes me wonder, it is even possible to do that. I created a mockup image of what I'm trying to do with some code. If anyone has done something similar and share with me, I would really appreciate it. Thanks.
private ContextMenu menuOpp = new ContextMenu();
private ComboBox cmbAssign = new ComboBox();
private int currentMouseOverRow;
private void dataGridView2_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
menuOpp.Name = "Exit";
menuOpp.MenuItems.Add("Exit", new EventHandler(menuItem_Click));
cmbAssign.Items.Add("assign");
dataGridView2.ContextMenu = menuOpp;
currentMouseOverRow = dataGridView2.HitTest(e.X, e.Y).RowIndex;
menuOpp.Show(dataGridView2, new Point(e.X, e.Y));
}
}
I was able to resolve this myself. I created a ContextMenuStrip, added a MenuItem with sub ComboBox item, populated the ComboBox on ToolStripMenuItem Click event. Then assigned ContextMenuStrip object to its ContextMenuStrip property. After assigning the ContextMenuStrip object to a control, the contextual menu displayed as user right click.
private void InitializeComponent()
{
System.Windows.Forms.ContextMenuStrip Context =
new System.Windows.Forms.ContextMenuStrip();
ToolStripMenuItem mnuAssign = new ToolStripMenuItem("Assign");
Context.Items.Add(mnuAssign);
ContextMenuStrip = context;
}