private void PushButton_Click(object sender, EventArgs e) { // This is how we get data from a CommonFileDialogTextBox // (CFDTextBox) while the dialog is still showing. This is the // only time you can retrieve data from a CFDTextBox. int forIdx = 0; int numberOfControls = openFileDialog.Controls.Count; CommonFileDialogControl cfdc = null; StringBuilder textBoxes = new StringBuilder( "TextBox control(s) and values:" + Environment.NewLine); const string itemTemplate = "Name: {0}; Text: \"{1}\"."; // find the control(s) you want to get data from for (forIdx = 0; forIdx < numberOfControls; forIdx++) { cfdc = openFileDialog.Controls[forIdx]; if (cfdc is CommonFileDialogTextBox) { textBoxes.Append(string.Format(itemTemplate, cfdc.Name, cfdc.Text)); } } MessageBox.Show(textBoxes.ToString(), "Data from the CommonFileDialogTextBoxs", MessageBoxButton.OK, MessageBoxImage.Information); }
/// <summary> /// "Open File Dialog Customization 2" button click event handler. /// </summary> /// /// <param name="sender">object</param> /// <param name="e">RoutedEventArgs</param> /// private void OpenFileDialogCustomizationXamlClicked(object sender, RoutedEventArgs e) { openFileDialog = FindOpenFileDialog("simpleOFD"); // Name access to Xaml resource control work-around. /* * Resources can also be referenced by code from within the * collection, but be aware that resources created in XAML will * definitely not be accessible until after Loaded is raised by * the element that declares the dictionary. In fact, resources * are parsed asynchronously and not even the Loaded event is an * assurance that you can reference a XAML defined resource. For * this reason you should generally only access XAML defined * resources as part of run-time code, or through other XAML * techniques such as styles or resource extension references for * attribute values. */ int forIdx = 0; int controlIndex = -1; int numberOfControls = openFileDialog.Controls.Count; CommonFileDialogControl cfdc = null; // find the control you want to initialize for (forIdx = 0; forIdx < numberOfControls; forIdx++) { cfdc = openFileDialog.Controls[forIdx]; // un-comment the .Equals test if looking for a specific // control. if (cfdc is CommonFileDialogTextBox /*&& * cfdc.Name.Equals("textName", StringComparison.InvariantCulture)*/) { // save the index of the CommonFileDialogTextBox (or // otherwise perform what ever other operation is deemed // necessary / useful here). controlIndex = forIdx; break; } } // init the control's text cfdc.Text = "enter text here."; CommonFileDialogResult result = openFileDialog.ShowDialog(); if (!result.Canceled) { MessageBox.Show("File Selected: " + openFileDialog.FileName, "Open File Dialog Customization 2 Result"); } }
/// <summary> /// Called when a control currently in the collection /// has a property changed. /// </summary> /// <param name="propertyName">The name of the property changed.</param> /// <param name="control">The control whose property has changed.</param> public virtual void ApplyControlPropertyChange(string propertyName, DialogControl control) { if (control == null) { throw new ArgumentNullException("control"); } CommonFileDialogControl dialogControl = null; if (propertyName == "Text") { CommonFileDialogTextBox textBox = control as CommonFileDialogTextBox; if (textBox != null) { customize.SetEditBoxText(control.Id, textBox.Text); } else { customize.SetControlLabel(control.Id, textBox.Text); } } else if (propertyName == "Visible" && (dialogControl = control as CommonFileDialogControl) != null) { ShellNativeMethods.ControlState state; customize.GetControlState(control.Id, out state); if (dialogControl.Visible == true) { state |= ShellNativeMethods.ControlState.Visible; } else if (dialogControl.Visible == false) { state &= ~ShellNativeMethods.ControlState.Visible; } customize.SetControlState(control.Id, state); } else if (propertyName == "Enabled" && dialogControl != null) { ShellNativeMethods.ControlState state; customize.GetControlState(control.Id, out state); if (dialogControl.Enabled == true) { state |= ShellNativeMethods.ControlState.Enable; } else if (dialogControl.Enabled == false) { state &= ~ShellNativeMethods.ControlState.Enable; } customize.SetControlState(control.Id, state); } else if (propertyName == "SelectedIndex") { CommonFileDialogRadioButtonList list; CommonFileDialogComboBox box; if ((list = control as CommonFileDialogRadioButtonList) != null) { customize.SetSelectedControlItem(list.Id, list.SelectedIndex); } else if ((box = control as CommonFileDialogComboBox) != null) { customize.SetSelectedControlItem(box.Id, box.SelectedIndex); } } else if (propertyName == "IsChecked") { CommonFileDialogCheckBox checkBox = control as CommonFileDialogCheckBox; if (checkBox != null) { customize.SetCheckButtonState(checkBox.Id, checkBox.IsChecked); } } }