Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an app in c# where there is a dialog with components like buttons and comboBox..I want to add item to combobox but I don't know how I can find name of comboBox to add items in this way:

C#
mybox.Items.Add(230);
mybox.Items.Add(231);
mybox.Items.Add(232);
mybox.Items.Add(233);
mybox.Items.Add(234);;


this is Maindow.g.i.cs of my app:


C#
#pragma checksum "..\..\..\Views\MainWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "672C0BA5C0622AB0E48A7C1162F67D388C4AE635"
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;
using WPFSDKSample;
using WPFSDKSample.ViewModels;


namespace WPFSDKSample {
    
    
    /// <summary>
    /// MainWindow
    /// </summary>
    public partial class MainWindow : System.Windows.Window, System.Windows.Markup.IComponentConnector {
        
        private bool _contentLoaded;
        
        /// <summary>
        /// InitializeComponent
        /// </summary>
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
        public void InitializeComponent() {
            if (_contentLoaded) {
                return;
            }
            _contentLoaded = true;
            System.Uri resourceLocater = new System.Uri("/WPFSDKSample;component/views/mainwindow.xaml", System.UriKind.Relative);
            
            #line 1 "..\..\..\Views\MainWindow.xaml"
            System.Windows.Application.LoadComponent(this, resourceLocater);
            
            #line default
            #line hidden
        }
        
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
            switch (connectionId)
            {
            case 1:
            
            #line 58 "..\..\..\Views\MainWindow.xaml"
            ((System.Windows.Controls.Button)(target)).Click += new System.Windows.RoutedEventHandler(this.Button_Click);
            
            #line default
            #line hidden
            return;
            case 2:
            
            #line 93 "..\..\..\Views\MainWindow.xaml"
            ((System.Windows.Controls.ComboBox)(target)).SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(this.ComboBox_SelectionChanged);
            
            #line default
            #line hidden
            return;
            }
            this._contentLoaded = true;
        }
    }
}



source code:

<pre> <ComboBox  SelectedItem="{Binding SelectedPrinter, Mode=OneWayToSource}"  DisplayMemberPath="Name"  MaxHeight="100" Width="250" SelectionChanged="ComboBox_SelectionChanged"/>
                        </StackPanel>
                        <StackPanel>
                            <Label FontWeight="Bold" FontSize="12">Roll selection (Twin turbo 450 printer)</Label>
                            <ComboBox ItemsSource="{Binding TwinTurboRolls}"  SelectedItem="{Binding SelectedRoll, Mode=OneWayToSource}"></ComboBox>
                            <Label FontSize="11">*Only for Twin Turbo 450 printer</Label>
                        </StackPanel>
                        <StackPanel>


I changed:

<pre>      <StackPanel>
                            <Label FontWeight="Bold" FontSize="12">Select printer</Label>
                            <ComboBox x:Name="ComboBox1"  SelectedItem="{Binding SelectedPrinter, Mode=OneWayToSource}"  DisplayMemberPath="Name"  MaxHeight="100" Width="250" SelectionChanged="ComboBox_SelectionChanged" IsEditable="True" IsSynchronizedWithCurrentItem="False"/>





What I have tried:

I tried to click with right button of mouse on combobox in dialog but I don't see its name
Posted
Updated 9-Feb-23 6:27am
v4
Comments
Dave Kreskowiak 8-Feb-23 11:56am    
Click once on the combobox and look in the Properties window. The name of the combo will be at the top.

If you don't see the properties window, right-click the combo and pick Properties. That will open the Properties window.
Member 14594285 9-Feb-23 4:06am    
I added name with properties window and I wrote:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//ComboBox1.
ComboBox1.Items.Add(230);
ComboBox1.Items.Add(231);
ComboBox1.Items.Add(232);
ComboBox1.Items.Add(233);
ComboBox1.Items.Add(234);
}

but items don't add
Graeme_Grant 9-Feb-23 4:17am    
WPF app ... no name set
Member 14594285 9-Feb-23 5:33am    
I added name..I tried this method on the link but items are added out of comboBox and not Inside
Graeme_Grant 9-Feb-23 5:40am    
The issue is not the tutorial... That website will guide you on many control. Please learn from it.

You have not given the ComboBox a name:
XML
<ComboBox  SelectedItem="{Binding SelectedPrinter, Mode=OneWayToSource}"  DisplayMemberPath="Name"  MaxHeight="100" Width="250" SelectionChanged="ComboBox_SelectionChanged"/>

To give it a name of "MyComboBox":
XML
<ComboBox x:Name="MyComboBox"
...
 />


UPDATE

Here is a greeat tutorial for you: The ComboBox control - The complete WPF tutorial[^]
 
Share this answer
 
v2
Comments
Member 14594285 9-Feb-23 4:28am    
ok thanks, I solved but if I write:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//ComboBox1.
ComboBox1.Items.Add("230");
ComboBox1.Items.Add("231");
}
items don't compare in comboBox
Graeme_Grant 9-Feb-23 4:31am    
that is a different problem. See my update.
Member 14594285 9-Feb-23 4:44am    
yes I saw, but with this I can modify source code and not generated code
Graeme_Grant 9-Feb-23 4:50am    
it is a tutorial, learn from it.
Don't put initialization code for "child controls" in the parent constructor; the children (probably) don't exist at that point. Use the Main Window "Loaded" event to access (named) child controls of the parent container.

You could also use the ComboBox's own "Loaded" event to initialize itself ... being aware that "Loaded" events can fire again based on circumstances (e.g. Tabs).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900