C# equivalent of Delphi's DisableControls/EnableControls - c#

What is the C# equivalent of Delphi's DisableControls/EnableControls methods (used to disable updating of databound controls while iterating through the underlying dataset)? I have googled for half an hour and did not find an answer...
I have a list box and a rich edit box bound to a binding source, but I need to do an operation that iterates through the entire dataset, and both controls get updated as I move through the underlying dataset. In Delphi this is easy enough: enclose the block that does the iteration between DisableControls and EnableControls. I can't find the C#/.NET equivalent, and I have looked really hard!

IIRC, setting Enabled to false does not prevent the controls from reacting to data changes in WinForms.
Collection-bound controls like the ListBox typically have methods BeginUpdate() and EndUpdate() which temporarily disable visual updates.
Also, the property mentioned by DarkSquirrel might be worth a look

I don't have access to Visual Studio right now, so I can't test this, but look through the methods for the control instance. Code such as:
// set the Enabled property of
// the controls to False; this should
// disable the controls for user access
listBox.Enabled = False;
richEditBox.Enabled = False;
// perform iteration
// and other operations
// set the Enabled property back
// to True
listBox.Enabled = True;
richEditBox.Enabled = True;
The exact name of the property may differ slightly, but I'm pretty sure that this is what it is.

I assume you are using WinForms, in that case you can try using the methods SuspendLayout/ResumeLayout.
Code sample from MSDN:
private void AddButtons()
{
// Suspend the form layout and add two buttons.
this.SuspendLayout();
Button buttonOK = new Button();
buttonOK.Location = new Point(10, 10);
buttonOK.Size = new Size(75, 25);
buttonOK.Text = "OK";
Button buttonCancel = new Button();
buttonCancel.Location = new Point(90, 10);
buttonCancel.Size = new Size(75, 25);
buttonCancel.Text = "Cancel";
this.Controls.AddRange(new Control[]{buttonOK, buttonCancel});
this.ResumeLayout();
}

So far I know, you don't need to Disiable/EnableControls in C#, since this type of DataSet doesn't work with a current cursor, like Delphi TDataSets.

Related

WinForms : Dynamically add / remove AnchorStyles to / of control

I am currently working on a Editor, which lets the user design his own WinForm overlay, at least to a certain point.
Therefore I want the user to decide, which AnchorStyles the current selected Control should have. I would like it to be handled by checkboxes. Here's how I had it in mind:
As you can see, the user has currently selected a dynamically added Panel, called Grid. Handled by the CheckBoxes to the right, he should now be able to set the selected Controls AnchorStyles.
Here's my problem: I can't seem to find a usable solution, to dynamically add a specific AnchorStyle to the already existing ones, or the opposite, remove the AnchorStyle, but keep the other ones as they are.
I was trying to get it to work with...
SelectedControl.Anchor += AnchorStyles.Top;
which doesen't work at all. So i thought of this...
SelectedControl.Anchor = SelectedControl.Anchor | AnchorStyles.Top
which I imagine could work, but I haven't even tested it, since I wouldn't know how to remove ones unchecked AnchorStyle.
Building a gigantic if(){} else if(){}... doesen't seem to be a good Idea :)
I'm open for any ideas / solutions.
Thanks in advance!
Assuming you have four check box controls named top, bottom, left and right, you can handle CheckedChange event of them using a single method and set the anchor of the desired control based on the checked value of the controls. For example:
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
var values = new[] { top.Checked, bottom.Checked, left.Checked, right.Checked };
byte[] data = new byte[1];
new BitArray(values).CopyTo(data, 0);
selectedControl.Anchor = (AnchorStyles)data[0];
}
Note: AnchorStyles is a flag enum having top=1, bottom=2, left=4 and right=8. Using above code I've mixed those flags to create the AnchorStyles and have assigned to the Anchor property of control.

Show a Winform 'Form' as if it were a usercontrol, within another form

Edit
The answer to This Question, though an obvious solution, is insufficient for this case. I've bolded the reason in my original question explaining why I wanted it 'literally answered' - thanks LarsTech!
My Original Question:
I have a library with 100 WinForms Form screens in it that is 'Beyond My Control' and there are a few I can share in my app. However, my app uses a tab control and I need the content of the forms to appear within the tabs instead of as popups.
When I try to new up one of the forms and add it to the controls collection of a tab, I get this error message:
Top-level control cannot be added to a control.
Anybody know how to get around this? I know there are other ways involving better architecture, but deadlines, backlogs, and rich new customers await. [sigh/]
private void button1_Click(object sender, EventArgs e)
{
//panel1.Controls.Add(new Button());
Form f = new Form();
f.Controls.Add(new CheckBox());
//f.ShowDialog();
panel1.Controls.Add(f);
}
Try turning the TopLevel off:
f.TopLevel = false;
f.FormBorderStyle = FormBorderStyle.None;
f.Visiible = true;
Now it is essentially a UserControl. A word of caution though, since it's a form, it will show up in the Application.OpenForms collection, which is probably not your intention.
You need to set the form's TopLevel property to false, like this:
f.TopLevel = false;
You may also want to set the FormBorderStyle property to None.
f.FormBorderStyle = FormBorderStyle.None;
As mentioned in the comments above, it really would be best, when possible, to make it a UserControl.

Object does not match target type

I have a TableLayoutPanel with a grid of PictureBox controls within it. I'm trying to find a shortcut way to change them all to Label controls instead of manually deleting each one and placing new controls in each cell.
I thought I could go into the designer code and find/replace PictureBox with Label, but now I get an
"Object does not match target type"
error in Visual Studio's error list. I can't view the designer page now either. Is this not allowed? If it is allowed, what's the right way to do it?
If you take a closer look at the generated code:
label1:
this.label1 = new System.Windows.Forms.Label();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(134, 163);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
pictureBox1:
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(97, 75);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(100, 50);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
My guess is that the
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
is changed by you into something like:
((System.ComponentModel.ISupportInitialize)(this.label1)).BeginInit();
which doesn't work, and results in designer issues. Object does not match target type.
so, apply the changes you already did, remove the lines like:
((System.ComponentModel.ISupportInitialize)(this.label1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.label1)).EndInit();
and I think you're good to go.
Don't change designer code. That stuff is automatically generated. Not only can your changes cause unexpected behavior, but they can get over-written as well.
I would attempt to make a change or 2 to your form, or whatever your designer is behind, and hope it regenerates all it's code.
You can delete all the picture boxes in the designer, then add all the labels in the _load event (or another convenient event). That way it will be easier to change next time.
As Haxx illustrated, you will have to clean-up the extra initialization PictureBox requires as well. The error you received is a interface casting error. In your case, as Haxx guessed, the Label control doesn't implement the ISupportInitialize interface.
Unlike most, I am not afraid of changing designer code in the interest of expediency, for what you are doing, it is ok to do so. Just know your objects, check-in prior to doing so, and don't put custom code in there!

gtk#, TreeView, CellRendererSpin: Spin buttons not visible & Custom CellRenderers

I am trying to use a spin button within my TreeView.
It as added like this:
Gtk.TreeViewColumn adjustColumn = new Gtk.TreeViewColumn();
adjustColumn.Title = "";
Gtk.CellRendererSpin adjustCell = new Gtk.CellRendererSpin();
adjustCell.Digits = 0;
adjustCell.Adjustment = new Adjustment(0, 0, 99, 1, 99, 99);
adjustCell.Editable = true;
adjustCell.Visible = true;
adjustCell.Sensitive = true;
adjustCell.Mode = CellRendererMode.Editable;
adjustColumn.PackEnd(adjustCell, true);
adjustColumn.SetCellDataFunc(adjustCell, new Gtk.TreeCellDataFunc(RenderAdjuster));
treeView.AppendColumn(adjustColumn);
And inside the rendering function the following is done:
(cell as Gtk.CellRendererSpin).Width = 30;
(cell as Gtk.CellRendererSpin).Height = 20;
(cell as Gtk.CellRendererSpin).Text = "" + (cell as Gtk.CellRendererSpin).Adjustment.Value;
This does render the value, but the spin buttons are not visible! They only become visible once the user clicks the value. That is absolutely ridiculous, as there is no way the user will know that the cell is editable other than randomly clicking in it, especially since there are other values in the row that are not editable.
So my first question is, how can I make those spin buttons always visible?
Could this be done with a custom CellRenderer?
I would be thankful for an example of how to create my own, actually working, spin button cell renderer. Unfortunately, custom cell renderers are an undocumented mystery (as almost anything in gtk# ;) ). And all you can find to them are examples that draw Cairo rectangles, etc. themselves. There must be an easier way where can you simply put any widget into that renderer, and listen to clicks, etc. and simply render the widget when the cell is rendered.
An example for a custom CellRenderer that simply displays a button (that can listen to clicks) would also be fine as I could at least "fake" a SpinButton, by having three columns with a text, a "+"-Button and a "-"-Button.

control to show steps to do something in winforms

i want to show steps on how to cook something in winform c# .net as steps. Something like a set of text area would be nice but:
-> list box considers the whole string of one step as one item so user needs to scroll horizontally to view the whole step.
-> datagridview is also not suitable as i want the text to word wrapped.
i also want the user to be able to edit the step.
any suggestions of custom control would be nice.
Maybe a wizard like app would be suitable for you. AFAIK there's no native wizard control in C# but you could implement one using tabs or using one of many in the web.
A multi line text box will do the job great. just take a simple text box and do the following to it, and it will turn to a text area:
TextBox listBoxNewInput = new TextBox();
//Initialize label's property
listBoxNewInput.Multiline = true;
// Add vertical scroll bars to the TextBox control.
listBoxNewInput.ScrollBars = ScrollBars.Vertical;
// Allow the RETURN key in the TextBox control.
listBoxNewInput.AcceptsReturn = true;
// Allow the TAB key to be entered in the TextBox control.
listBoxNewInput.AcceptsTab = true;
// Set WordWrap to true to allow text to wrap to the next line.
listBoxNewInput.WordWrap = true;
listBoxNewInput.Width = 315;
listBoxNewInput.Height = 150;
listBoxNewInput.DoubleClick += new EventHandler(listBoxNewInput_DoubleClick);
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.Controls.Add(labelInput);
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.Controls.Add(list
BoxNewInput);

Categories