So what I normally do to show a dialogue box and get a result in C# is,
MessageBoxResult result = MessageBox.Show("Wrong username or password", "Invalid details", MessageBoxButton.OK, MessageBoxImage.Hand);
string clear = "";
if (result == MessageBoxResult.OK)
{
username.Text = clear;
password.Password = clear;
}
However I've always hated this standard look it gives so I decided to make my own dialogue box in wpf. Problem is I'm not too sure exactly how to return a dialogue result with that. It's just a simple box with an okay button that's supposed to clear the username and password fields.
Is there any way to do this?
I found this on another question here on SO (here Where is Button.DialogResult in WPF?)
public class ButtonHelper
{
// Boilerplate code to register attached property "bool? DialogResult"
public static bool? GetDialogResult(DependencyObject obj)
{
return (bool?)obj.GetValue(DialogResultProperty);
}
public static void SetDialogResult(DependencyObject obj, bool? value)
{
obj.SetValue(DialogResultProperty, value);
}
public static readonly DependencyProperty DialogResultProperty =
DependencyProperty.RegisterAttached(
"DialogResult", typeof(bool?), typeof(ButtonHelper), new UIPropertyMetadata
{
PropertyChangedCallback = (obj, e) =>
{
// Implementation of DialogResult functionality
var button = obj as Button;
if (button == null)
throw new InvalidOperationException("Can only use ButtonHelper.DialogResult on a Button control");
button.Click += (sender, e2) =>
{
Window.GetWindow(button).DialogResult = GetDialogResult(button);
};
}
});
}
Then in the xaml for the "Ok" button
yourNameSpaceForTheButtonHelperClass:ButtonHelper.DialogResult="True"
Using the MVVM pattern, you could do this by exposing a DialogResult on the ViewModel your control is using. I highly recommend making an interface for this, thusly you can retrieve a result by casting to the interface regardless of the actual view model's type.
var control = new MyControl();
control.ShowDialog(); // Assuming your control is a Window
// - Otherwise, you'll have to wrap it in a window and event-bind to close it
result = (control.DataContext as IResultDialogVM).Result;
Alternatively, if you prefer to explicitly set your view models
var vm = new MyViewModel(question);
new MyControl { DataContext = vm }.ShowDialog();
result = vm.Result;
Related
I have a problem.I want to update the main UI in a user control.I tried so many times, but i didn't make it.The test is divided into two categories as follows:
Class 1:
I first assigned the main window control (tbInfo, TextBlock type) directly, unsuccessfully. So I created a textBlockUpdate class (implementing the property change notification interface) and bind its properties (TextMessage) to the Text property of tbInfo,unsuccessfully. Then I used the content control,also unsuccessfully.The code as follows:
//Feature code in user control.
info = string.Format("Adding file{0}", System.IO.Path.GetFileName(FileName));
if (_dataObject.MainWindow != null)
{
_dataObject.MainWindow.ShowInfo(info);
}
//Feature code in main window.
public void ShowInfo(string info)
{
if (Dispatcher.CheckAccess())
{
//tbInfo.Text = info;
// textBlockUpdate.TextMessage = info;
TextBlock textBlock = new TextBlock();
textBlock.Text = info;
tbInfoContainer.Content = textBlock;
}
else
{
Action<string> showInfoDel = (str) =>
{
// tbInfo.Text = info;
//textBlockUpdate.TextMessage = info;
TextBlock textBlock = new TextBlock();
textBlock.Text = info;
tbInfoContainer.Content = textBlock;
};
Dispatcher.BeginInvoke(showInfoDel, info);
}
}
Class 2:
I put the code in the user control into a thread, or did it not succeed.I tried three times, but I never succeeded.
1.
new Thread(()=>{
this.Dispatcher.Invoke(new Action(()=>{
//Add the feature code above here
}));
}).Start();
2.
Application.Current.Dispatcher.Invoke(new Action(() => {
//Add the feature code above here
}));
3.
Task task = new Task(()=> {
//Add the feature code above here
});
task.Start();
task.Wait();
So, can anyone tell me How to do to make it work?
This is not how it is done. Also setting the properties of a class is not called binding. It's a simple assignment.
A Binding connects two or more (MultiBinding) properties (target and source) and updates them automatically, when one of the two changes.
To allow the binding to detect property changes, you have to implement the participating properties either as DependencyProperty (mandatory for binding target - preferable on controls) or let them raise the INotifyPropertyChanged.PropertyChanged event on property changes.
Create the data and binding source
MainWindow.xaml
partial class MainWindow : Window
{
public static readonly DependencyProperty InfoProperty = DependencyProperty.Register(
"Info",
typeof(string),
typeof(MainWindow),
new PropertyMetadata(default(string)));
public string Info
{
get => (string) GetValue(MainWindow.InfoProperty);
set => SetValue(MainWindow.InfoProperty, value);
}
// Update the TextBlock via data binding
public void ShowInfo(string info)
{
this.Info = info;
}
}
Create the UI an set up the data binding
MainWindow.xaml
<Window>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=MainWindow},
Path=Info}" />
</Window>
See Microsoft Docs:
Data binding overview in WPF
How to: Implement a Dependency Property
I found a solution.Just change to the following method.
//Call the render thread update UI from the current thread
int i = 1;
while (i < 10)
{
if (_dataObject.MainWindow != null)
{
_dataObject.MainWindow.ShowInfo(info);
}
Dispatcher.Invoke(new Action(() =>
{
}), System.Windows.Threading.DispatcherPriority.Background);
// Thread.Sleep(100);
i++;
}
//this is a time-consuming process.
accessObj.AddFile(_dataObject.Path, fileInfo, fileContent);
//Call the render thread update UI from the current thread
int j = 1;
while (j < 10)
{
if (j == 1) {
_dataObject.AttachFiles.Add(fileInfo);
}
Dispatcher.Invoke(new Action(() =>
{
}), System.Windows.Threading.DispatcherPriority.Background);
//Thread.Sleep(100);
j++;
}
I am creating an "export to excel" windows form in c#.
The class contains a CheckedListBox and a "check all" button.
When clicking on the button I want to check all the items in the list in case that at least one checkbox is not checked or uncheck all the checkboxes in case they are all already checked.
I added a small complication, the list of the items is a list of custom objects (see private class inside): "ObjectToExport" class.
public partial class ExcelCustomExportForm : Form
{
private class ObjectToExport
{
private readonly IExcelExportable _form;
public ObjectToExport(IExcelExportable form)
{
_form = form;
}
public override string ToString()
{
return $"{_form.FormName} ({_form.CreatedDate.ToShortDateString()} {_form.CreatedDate.ToShortTimeString()})";
}
}
// each form in the list contains a gridview which will be exported to excel
public ExcelCustomExportForm(List<IExcelExportable> forms)
{
InitializeComponent();
Init(forms);
}
private void Init(List<IExcelExportable> forms)
{
foreach (IExcelExportable form in forms)
{
// Checked List Box creation
FormsCheckedListBox.Items.Add(new ObjectToExport(form));
}
}
private void CheckAllButton_Click(object sender, EventArgs e)
{
// checking if all the items in the list are checked
var isAllChecked = FormsCheckedListBox.Items.OfType<CheckBox>().All(c => c.Checked);
CheckItems(!isAllChecked);
}
private void CheckItems(bool checkAll)
{
if (checkAll)
{
CheckAllButton.Text = "Uncheck All";
}
else
{
CheckAllButton.Text = "Check All";
}
FormsCheckedListBox.CheckedItems.OfType<CheckBox>().ToList().ForEach(c => c.Checked = checkAll);
}
}
The problem is that the following line returns true even if not check box is checked:
var isAllChecked = FormsCheckedListBox.Items.OfType<CheckBox>().All(c => c.Checked);
Similar issue with the following line, if checkAll is true, no check box is checked:
FormsCheckedListBox.CheckedItems.OfType<CheckBox>().ToList().ForEach(c => c.Checked = checkAll);
What is the correct way to fix those two lines of code?
Your Problem begins here.
FormsCheckedListBox.Items.Add(new ObjectToExport(form));
and
var isAllChecked = FormsCheckedListBox.Items.OfType<CheckBox>().All(c => c.Checked);
You are adding instances of 'ObjectToExport' to the FormsCheckedListBox, but while filtering, you are checking filtering with CheckBox.
This means, your filtered query always return empty, and query never reaches All. This can be demonstrated with following example.
var list = new [] { 1,2,3,4};
var result = list.OfType<string>().All(x=> {Console.WriteLine("Inside All"); return false;});
The result of above would be True, and it would never print the "Inside All" text. This is what is happening with your queries.
You can find if any of the checkbox is checked using
var ifAnyChecked = checkedListBox1.CheckedItems.Count !=0;
To change state, you could do the following.
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemCheckState(i) == CheckState.Checked)
{
// Do something
}
}
I have created a custom message box with a textbox for input which appears under a certain condition in form1. I want form1 to hold the value of the textbox if the submit btn is clicked. I am not getting the desired result.
This is similar to this however I don't want the processing to happen in the message box because the process requires so many variables that I would have to transfer to the messsagebox.
Form condition
}
else //NOT ALL APPROVE
{
string BtnClicked = DenyEmpRequest.ShowBox(AllDenied, EmpRequestID);
if (BtnClicked == "1") //SUBMIT BTN WAS CLICKED
{
DenyEmpRequest emp = new DenyEmpRequest();
string reason = emp.Reason_Txt.Text;
}
I know that it is because I am creating a new instance of the form that I used in the messagebox when I said "DenyEmpRequest emp = new DenyEmpRequest();". I don't know any other way to access the textbox in the messagebox.
Messagebox code
public static string ShowBox(string DenyEmp, string RequestID)
{
newMessageBox = new DenyEmpRequest();
newMessageBox.EmpToDeny_lbl.Text = DenyEmp;
EmpRequestID = RequestID;
newMessageBox.ShowDialog();
return Button_id;
}
private void SubmitBtn_Click(object sender, EventArgs e)
{
if (Reason_Txt.Text == string.Empty)
{
NoReason_Lbl.Visible = true;
}
else
{
Button_id = "1";
newMessageBox.Dispose();
}
Looks like you're overcomplicating it. If you are just trying to retrieve a string from a custom MessageBox, just make a form with an OK/Cancel button and a text box. Make a public string property that wraps around the value of the text box's "Text" property. And make the form set it's DialogResult to DialogResult.OK if the OK button is clicked, DialogResult.Cancel if the cancel button is clicked.
Then you can call this form with code shown below:
using (CustomMessageBox myMessageBox = new CustomMessageBox())
{
myMessageBox.Text = "Initial text"; // optionally set the initial value of the text box
if (myMessageBox.ShowDialog(this) == DialogResult.OK)
{
someVariable = myMessageBox.Text;
}
}
This is the format you should be using.
EDIT:
In reference to your comment, if you have a form with a text box on it, just write the property like this:
public class CustomMessageBox : Form
{
public string Text
{
get
{
return textBox.Text;
}
set
{
textBox.Text = value;
}
}
}
i wish to create a form at runtime that will read the columns for any datasource and create fields based on the columns and datatype just like a datagridviews insert line
Best regards,
Mark
What you are doing sounds a lot like how PropertyGrid already works, which is essentially:
foreach(PropertyDescriptor prop in TypeDescriptor.GetProperties(obj)) {
object val = prop.GetValue(obj);
string s = prop.Converter.ConvertToString(val);
Control cont = // TODO: create some control and set x/y
cont.Text = s;
this.Controls.Add(cont);
}
To avoid lots of work with alignment, using Dock to set the positions might help:
using(Form form = new Form())
using (PropertyGrid grid = new PropertyGrid())
{
form.Text = obj.ToString(); // why not...
grid.Dock = DockStyle.Fill;
form.Controls.Add(grid);
grid.SelectedObject = obj;
form.ShowDialog(this);
}
I wonder if it is easier to use PropertyGrid in simple circumstances, though. Or there are some 3rd-party versions that work similarly.
Ok so heres what i came up with!
public partial class Form2 : Form
{
private Boolean isBrowsable(PropertyInfo info)
{
return info.GetCustomAttributes(typeof(BrowsableAttribute), false).Length>-1;
}
public Form2()
{
InitializeComponent();
}
public Form2(Boolean showCheckBoxes)
{
InitializeComponent();
_showCheckBoxes = true;
}
private Boolean _showCheckBoxes;
private Object _reflection;
private TableLayoutPanel _table = new TableLayoutPanel{Dock=DockStyle.Fill, CellBorderStyle = TableLayoutPanelCellBorderStyle.Single};
public Object SelectedObject
{
get
{
return _reflection;
}
set
{
//clear all controls from the table
_table.Controls.Clear();
foreach (var property in _reflection.GetType().GetProperties())
{
if (isBrowsable(property))
{
if ((property.PropertyType == typeof(int)) || (property.PropertyType == typeof(string)))
{
var textField = new TextBox { Dock = DockStyle.Fill, AutoSize = true };
textField.DataBindings.Add("Text", _reflection, property.Name);
_table.Controls.Add(textField, 2, _table.RowCount += 1);
var propertyLabel = new Label
{
Text = property.Name,
Dock = DockStyle.Fill,
TextAlign = ContentAlignment.MiddleLeft
};
_table.Controls.Add(propertyLabel, 1, _table.RowCount);
if (_showCheckBoxes)
{
var checkBox = new CheckBox
{
AutoSize = true,
Name = property.Name,
Dock = DockStyle.Left,
CheckAlign = ContentAlignment.TopLeft
};
_table.Controls.Add(checkBox, 0, _table.RowCount);
}
}
}
}
//add one extra row to finish alignment
var panel = new Panel { AutoSize = true };
_table.Controls.Add(panel, 2, _table.RowCount += 1);
_table.Controls.Add(panel, 1, _table.RowCount);
if (_showCheckBoxes)
{
_table.Controls.Add(panel, 0, _table.RowCount);
}
Controls.Add(_table);
if (!Controls.Contains(_table))
Controls.Add(_table);
}
}
public Boolean Execute(Object reflection)
{
SelectedObject = reflection;
return ShowDialog() == DialogResult.OK;
}
}
thanks all!
I don't fully understand your question. Is it correct that you want to create a Windows form which provides input fields (textboxes, checkboxes, etc.) for all fields/properties of an object that you feed to the form as its DataSource?
You might have to use reflection for this (see the System.Reflection namespace). For example, to get a list of all properties:
using System.Reflection;
....
public object DataSource;
...
Debug.Assert( DataSource != null );
var properties = DataSource.GetType().GetProperties();
You would then instantiate one input control per property:
foreach ( var property in properties )
{
// extract some information about each property:
string propertyName = property.Name;
Type propertyType = property.PropertyType;
bool propertyReadOnly = !property.CanWrite;
// create input controls based on this information:
// ...
}
However, it might be fairly tricky to reliably map property types to the correct input control; for example, what are you going to do when you encounter a property with some unknown class as its type, or when a property is a collection of values? You might have to create a sub-form inside your form in some cases; in other cases, a listbox might be enough.
I've recently built a sample project that uses the Dynamic Data assemblies of ASP.NET to do just this for a WPF grid, but I'm sure you could adapt the concept to WinForms. Dynamic Data provides much richer metadata than just reflection or the database, but it does require an Entity Data Model, or a LINQ to SQL data model.
basically, all you need is a reference to System.Web.DymamicData, and maybe you can find something useful in my class:
public class DynamicDataGridBuilder<TContext, TEntity> where TEntity : EntityObject
{
readonly MetaModel model = new MetaModel();
public DynamicDataGridBuilder()
{
model.RegisterContext(typeof(TContext), new ContextConfiguration { ScaffoldAllTables = true });
}
public void BuildColumns(DataGrid targetGrid)
{
MetaTable metaTable = model.GetTable(typeof(TEntity));
// Decision whether to auto-generated columns still rests with the caller.
targetGrid.Columns.Clear();
foreach (var metaColumn in metaTable.Columns.Where(x => x.GetType().Name == "MetaColumn" && x.Scaffold))
{
switch (metaColumn.ColumnType.Name)
{
case "Boolean":
targetGrid.Columns.Add(new DataGridCheckBoxColumn { Binding = new Binding(metaColumn.Name), Header = metaColumn.DisplayName });
break;
default:
targetGrid.Columns.Add(new DynamicDataGridTextColumn { MetaColumn = metaColumn, Binding = new Binding(metaColumn.Name), Header = metaColumn.DisplayName });
break;
}
}
}
}
TContext is the type of your object model, and TEntity the type of the entity / class in that model your want to generate controls for.
Use control data binding. It will do all the work for you.
I'm creating a multi-tabbed .NET application that allows the user to dynamically add and remove tabs at runtime. When a new tab is added, a control is added to it (as a child), in which the contents can be edited (eg. a text box). The user can perform tasks on the currently visible text box using a toolbar/menu bar.
To better explain this, look at the picture below to see an example of what I want to accomplish. It's just a mock-up, so it doesn't actually work that way, but it shows what I want to get done. Essentially, like a multi-tabbed Notepad.
View the image here: http://picasion.com/pic15/324b466729e42a74b9632c1473355d3b.gif
Is this possible in .NET? I'm pretty sure it is, I'm just looking for a way that it can be implemented.
You could use a simple extension method:
public static void PasteIntoCurrentTab(this TabControl tabControl)
{
if (tabControl.SelectedTab == null)
{
// Could throw here.
return;
}
if (tabControl.SelectedTab.Controls.Count == 0)
{
// Could throw here.
return;
}
RichTextBox textBox = tabControl.SelectedTab.Controls[0] as RichTextBox;
if (textBox == null)
{
// Could throw here.
return;
}
textBox.Paste();
}
Usage:
myTabControl.PasteIntoCurrentTab();
I suggest you keep some "current state" variables updated so you always have a pointer to the selected Tab Page, and its child control (in the case of a tabbed-notepad emulation discussed here : a TextBox). My preference would be to keep track of the TabPage<>TextBox connections using a Dictionary to avoid having to cast the TextBoxes if they are accessed using the TabPage.Controls route : the following code assumes you have a TabControl named 'tabControl1 on a Form :
Dictionary<TabPage, TextBox> dct_TabPageToTextBox;
int tabCnt = 1;
TabPage currentTabPage;
TextBox currentTextBox;
So, as you create each new TabPage at run-time you call something like this :
private void AddNewTabPage()
{
if (dct_TabPageToTextBox == null) dct_TabPageToTextBox = new Dictionary<TabPage, TextBox>();
currentTabPage = new TabPage("Page " + tabCnt.ToString());
tabControl1.TabPages.Add(currentTabPage);
currentTextBox = new TextBox();
dct_TabPageToTextBox.Add(currentTabPage, currentTextBox);
currentTabPage.Controls.Add(currentTextBox);
currentTextBox.Dock = DockStyle.Fill;
currentTextBox.Text = "sample text for page " + tabCnt.ToString();
tabControl1.SelectedTab = currentTabPage;
tabCnt++;
}
As the end-user changes the selected TabPage you can simply update your current state variables like this :
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
currentTabPage = tabControl1.SelectedTab;
currentTextBox = dct_TabPageToTextBox[currentTabPage];
MessageBox.Show("text in current Tab Page is : " + currentTextBox.Text);
}
So now have the code that is invoked by your menu choices applied only to the currentTextBox.
best, Bill
I tried this for fun ... I made a form with a ToolStripContainer, and a ToolStrip inside it, with the standard buttons (which includes the paste button). I renamed the paste button to pasteButton, and hooking everything up you get:
public Form2()
{
InitializeComponent();
TabControl tc = new TabControl();
toolStripContainer1.ContentPanel.Controls.Add(tc);
tc.Dock = DockStyle.Fill;
TextBox selectedTextBox = null;
pasteButton.Click += (_, __) => selectedTextBox.Paste(Clipboard.GetText(TextDataFormat.Text));
int pages = 0;
newTabButton.Click += (_,__) => {
TextBox tb = new TextBox { Multiline = true, Dock = DockStyle.Fill, ScrollBars = ScrollBars.Vertical };
TabPage tp = new TabPage("Page " + (++pages).ToString());
tc.Selected += (o, e) => selectedTextBox = e.TabPage == tp ? tb: selectedTextBox;
tp.Controls.Add(tb);
tc.TabPages.Add(tp);
tc.SelectedTab = tp;
selectedTextBox = tb;
};
}