Q: DevExpress TreeListLookUpEdit - c#

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

Related

How to Control Which Object Used ToolStrip in C#

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?

WPF dynamically add button + function

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!

Is there a way to dynamically generate code to interact with buttons in C#?

I'm trying to find a a way to be able to essentially dynamically generate code based on an input.
For example I could type something like:
int Number = 22;
Button<Number>.Text = "X";
So in this case it would set button22 to have its text be an "X".
And I could change it so that I could input, for example 24 into the program and it would then set button24 to be an "X", instead of setting up a bunch of if statements to cover every potential button press.
For further context I have a Grid of 64 buttons and I need to be able to edit them individually to show to the user which buttons have been pressed, it is possible to do it with a lot of if statements but I thought it might be worth trying to find a more elegant solution.
You could have a list of buttons:
private List<Button> _buttons = new List<Button>();
Populate it like this:
for (int i = 0; i < 10; i++)
{
var b = new Button();
b.Text = $"Button #{i}";
b.Click += HandleButtonClick;
}
And you could even set an event handler on one of its events which doesn't even need to use the list (the sender is the source of the event):
private void HandleButtonClick(object sender, EventArgs e)
{
(sender as Button).Text = "X";
}
Buttons have a Tag property that can be used to hold arbitrary data about a button, this is described for WinForms, WPF and UWP.
Simple usage that is similar to OP's requirement is demonstrated in this SO post
This situation is in a practical sense the very reason that .Tag exists at all in user interface controls pretty much from the birth of c#.
So you do not need to use a custom class for a button, just simply assign your value to the .Tag property on the Button class that you are creating programmatically:
in this example a list is used to create the buttons and separate the creation from the layout, it is not necessary to do this, but may be useful. Instead, you could assign this button to it's parent container and/or set the layout margins or coordinates without keeping a reference to the Button object at all.
If OP updates the post to include implementation examples, we can update this response with more specific and complete code.
private List<Button> _buttons = new List<Button>();
// ... iteration or switching logic
var nextButton = new Button
{
Text = "x",
Tag = 22
};
nextButton.Click += DynamicButton_Click;
_buttons.Add(nextButton);
// ... later push the buttons into the parent container or bind to the UI
Then the button click handler you can access this Tag property:
this is presented from WinForms, the only difference in UWP or WPF is the method signature, change EventArgs to RoutedEventArgs
private void DynamicButton_Click(object sender, EventArgs e)
{
if(int.TryParse((sender as Button).Tag?.ToString(), out int buttonValue))
{
// use buttonValue
Console.Out.WriteLine(buttonValue);
}
else
{
// Otherwise, sender was not a button, or the button did not have an integer tag value
// either way, handle that error state here...
}
}
Using these concepts, once the buttons are created, let's say in some simple grid alignment, you could allow the user to set this Tag value at runtime if you have a TextBox (or other) input field that can be accessed from the code.
I recommend that you use MVVM style bindings for this rather than directly referencing a TextBox control, but this is simply to demonstrate the point.
private void DynamicButton_Click(object sender, EventArgs e)
{
// assign the string value from the ButtonValueTextbox control to this button
string value = this.ButtonValueTextBox.Text;
if(sender is Button button)
{
button.Tag = value;
}
else
{
// Otherwise, sender was not a button
// handle the error state here if you need to...
}
}
Now that each button has a tag, you could easily add logic to maintain unique tag values by iterating through the other buttons and clearing the tag if it was previously assigned to a different button.
Maybe you could keep a List of Button References:
var myButtons = new List<Button>();
myButtons.Add(firstButton);
myButtons.Add(secondButton);
// ... etc
// ... then somewhere else
int number = 3;
myButtons[number].Text = "xxx";

Is there a way to control all radio buttons through one control? Instead of passing a control for each radio button (C#)

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

How to reference/link to a listbox that is dynamically created using a button and tabControl?

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

Categories