private void btnTuyaux_MouseDown(object sender, MouseEventArgs e)
{
cboTuyaux.Enabled = true;
cboTuyaux.Visible = true;
cboTuyaux.BringToFront();
cboTuyaux.DroppedDown = true;
cboTuyaux.Focus();
}
private void btnLigne_MouseDown(object sender, MouseEventArgs e)
{
cboLigne.Enabled = true;
cboLigne.Visible = true;
cboLigne.BringToFront();
cboLigne.DroppedDown = true;
cboLigne.Focus();
}
private void btnBloc_MouseDown(object sender, MouseEventArgs e)
{
cboBloc.Enabled = true;
cboBloc.Visible = true;
cboBloc.BringToFront();
cboBloc.DroppedDown = true;
cboBloc.Focus();
}
private void btnRaccord_MouseDown(object sender, MouseEventArgs e)
{
cboRaccord.Enabled = true;
cboRaccord.Visible = true;
cboRaccord.BringToFront();
cboRaccord.DroppedDown = true;
cboRaccord.Focus();
}
private void btnALL_MouseDown(object sender, MouseEventArgs e)
{
cboAll.Enabled = true;
cboAll.Visible = true;
cboAll.BringToFront();
cboAll.DroppedDown = true;
cboAll.Focus();
}
Thank you all ! I will check how to make a custom class, I would use it often.
Thanks Micaletti for your code, I'm goint to use this solution meanwhile.
Thank you all ! I will check how to make a custom class, I would use it often.
Thanks Micaletti for your code, I'm goint to use this solution meanwhile.
The VB code shown above has to do a lot of work needlessly each time a button is clicked.
While a custom class in WinForms, or data-binding and triggers would be the best way to solve your problem, a quick/dirty solution in WinForms would be to store the ComboBox in the Tag property of one or more buttons (when the form loads), and when a button is clicked, it's Tag property is the associated ComboBox, which you can cast to that type and use without having to look for it. That allows you to use a single event handler for all the buttons, since it isn't coupled to any specific Button or ComboBox (by using the event handler's Sender parameter to access the button that was clicked).
I quickly threw together Tony's idea. Adding handlers and setting tags like this is not optimal either if you have other controls, but you get the idea.Code - vb.net: [Select]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each Control In Me.Controls If TypeOf Control Is Button Then Dim Button As Button = Control AddHandler Button.Click, AddressOf Button_Click For Each Item In Me.Controls If TypeOf Item Is ComboBox Then Dim ComboBox As ComboBox = Item If ComboBox.Name.Contains(Button.Name) Then Button.Tag = ComboBox Exit For End If End If Next End If Next End Sub Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) sender.Tag.Enabled = True sender.Tag.Visible = True sender.Tag.BringToFront() sender.Tag.DroppedDown = True sender.Tag.Focus() End Sub
I would try to make a custom class that holds the pair together. Then they are very easily linked as the button's parent is your custom object and it has another child - your combo box
If you don't mind, let's call the above your interpretation of how my idea would be implemented, but it's nothing like how I would do it, because it's wasteful to iterate over every control on the form many times, once for each button. Your interpretation also tests to see if the entire name of a button is contained in the name of a ComboBox, which according to the OP's description, won't work because the button names start with 'btn', and the combobox names start with 'cb'. You also have to cast the Tag property to a ComboBox (and not assume that the value is assigned to a ComboBox) before you can use it as one.
QuoteIf you don't mind, let's call the above your interpretation of how my idea would be implemented, but it's nothing like how I would do it, because it's wasteful to iterate over every control on the form many times, once for each button. Your interpretation also tests to see if the entire name of a button is contained in the name of a ComboBox, which according to the OP's description, won't work because the button names start with 'btn', and the combobox names start with 'cb'. You also have to cast the Tag property to a ComboBox (and not assume that the value is assigned to a ComboBox) before you can use it as one.
Call it whatever you want. Really all I was trying to convey to the OP about your idea was Button.Tag = Combobox... Button.Tag.Visible = True, which was your idea but you provided no code. Also you are assuming the tag on a button could be someting other than a combo box. You most certainly do not "have to" cast it to a combo box if thats all youre using the tags for. Furthermore I stated that he would have to use an appropriate naming theme for my code to work. He gets the point. I trust he can use string functions to suit his specific needs.
I predicted you wouldn't like either example which is why I specifically stated that they were not optimal. Clearly the OP is novice (so am I) or he wouldnt be asking such a question. He just needed some code to get his gears turning. I also pointed out that a custom class would be the "correct" way to do things.
Pointing out the obvious (restating what I have already said even), can be misinterpreted as something else. There is no need to check if the combobox is null in your last example, because you wouldnt create an instance of the class if it was. Anyone can nitpick example code, but it's not always welcome or necessary.
:lol: I think "thanks for pointing that out, my algorithms frequently use needless iteration to support my novice understanding of programming. I will keep this in mind moving forward" would have sufficed.
BTW if the combo box happens to be null the form will crash. After reading Tony's code for a little over a year I'm getting used to the way he makes sure that background tasks are bullet proof.
This is not optimal but it gets the job done quickly.
I would try to make a custom class that holds the pair together. Then they are very easily linked as the button's parent is your custom object and it has another child - your combo box
Here's an example:Code - C#: [Select]
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.ComponentModel; namespace MyControls { /// A Button that has a reference to a ComboBox, that /// can be set in the designer, eliminating the need /// to search for implied associations between the /// linked controls using their names: /// Using this on a form, select the LinkedButton, /// and in the Properties pane you will see a property /// named "LinkedComboBox", which can be set to an /// existing ComboBox on the same form. public class LinkedButton : Button { public LinkedButton() { } [DesignerSerializationVisibility( DesignerSerializationVisibility.Visible )] public ComboBox LinkedComboBox { get;set; } protected override void OnClick( EventArgs e ) { if( LinkedComboBox != null ) { LinkedComboBox.Visible = true; LinkedComboBox.BringToFront(); LinkedComboBox.Focus(); /// blah blah blah.... } base.OnClick( e ); } } }
Members that do not have a DesignerSerializationVisibilityAttribute will be treated as though they have a DesignerSerializationVisibilityAttribute with a value of Visible. The values of a property marked as Visible will be serialized, if possible, by a serializer for the type. To specify custom serialization for a particular type or property, use the DesignerSerializerAttribute.
What is the purpose of [DesignerSerializationVisibility( DesignerSerializationVisibility.Visible )] (http://msdn.microsoft.com/en-us/library/system.componentmodel.designerserializationvisibilityattribute.aspx)? I've never seen it before, and it appears that you've set it to the default value.Quote from: msdnMembers that do not have a DesignerSerializationVisibilityAttribute will be treated as though they have a DesignerSerializationVisibilityAttribute with a value of Visible. The values of a property marked as Visible will be serialized, if possible, by a serializer for the type. To specify custom serialization for a particular type or property, use the DesignerSerializerAttribute.
Thanks