How to set skin on XtraUserControl in Windows Form? - c#

I have a simple Windows Form. In it, I've embedded a ChildXtraUserControl which derives from XtraUserControl (DevEx v10.1).
I'd like to skin the ChildXtraUserControl with 'Office 2010 Blue', and I'm expecting it to look bluish when I run the form. I've tried this two different ways but am unable to get it to work.
Attempt 1: Set the LookAndFeel in the ChildXtraUserControl, set the ChildXtraUserControl into a Windows Form Panel in the form
When I run this, I see only the Panel, which I've colored pale yellow.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var devExUserControl = new DevExpressUserControl {Dock = DockStyle.Fill};
panel1.Controls.Add(devExUserControl);
}
}
public partial class ChildXtraUserControl : XtraUserControl
{
public ChildXtraUserControl()
{
InitializeComponent();
IntializeSkin();
}
private void IntializeSkin()
{
LookAndFeel.UseDefaultLookAndFeel = false;
LookAndFeel.UseWindowsXPTheme = false;
LookAndFeel.Style = LookAndFeelStyle.Skin;
LookAndFeel.SkinName = "Office 2010 Blue";
}
}
Attempt 2: I read on the DevEx Support Center that the ChildXtraUserControl could be in a DevExpress PanelControl, and the LookAndFeel set on the PanelControl
As before, I only see the pale yellow PanelControl. The post does seem like it was for a different DevEx version, but I thought it was worth a shot.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
panelControl1.LookAndFeel.UseDefaultLookAndFeel = false;
panelControl1.LookAndFeel.UseWindowsXPTheme = false;
panelControl1.LookAndFeel.Style = LookAndFeelStyle.Skin;
panelControl1.LookAndFeel.SkinName = "Office 2010 Blue";
var devExUserControl = new ChildXtraUserControl { Dock = DockStyle.Fill };
panelControl1.Controls.Add(devExUserControl);
}
}
public partial class ChildXtraUserControl : XtraUserControl
{
public ChildXtraUserControl()
{
InitializeComponent();
}
}
Does anyone have any ideas what I'm doing wrong? Thanks in advance.

I managed to get this to work by using the second approach and modifying the Form1 constructor as shown.
public Form1()
{
InitializeComponent();
// add this line
DevExpress.UserSkins.OfficeSkins.Register();
panelControl1.LookAndFeel.UseDefaultLookAndFeel = false;
panelControl1.LookAndFeel.UseWindowsXPTheme = false;
panelControl1.LookAndFeel.Style = LookAndFeelStyle.Skin;
panelControl1.LookAndFeel.SkinName = "Office 2010 Blue";
var childXtraUserControl = new ChildXtraUserControl {Dock = DockStyle.Fill};
panelControl1.Controls.Add(childXtraUserControl);
}

Related

Return a button object by its name in C# WinForm

I’m developing an application in C# which works with several forms and I would like to implement a method to set the properties of these forms. For each one of these forms, two buttons named “okBtn” and “cancelBtn” were created through Designer. I would like to set the form AcceptButton property as “okBtn” and the form CancelButton property as ”cancelBtn”, but I don’t know how to return the buttons objects by their names. Follow a sample that what I’m trying to do:
static public void SetDialogAppearance(Form form)
{
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.Icon = PresentationLayer.Properties.Resources.embraer;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = form.Controls["okBtn"]; //this returns an error
form.CancelButton = form.Controls["cancelBtn"]; //this returns an error
}
I managed to solve this issue doing this:
I created a class that inherit the Form class and with the buttons declared as public fields:
public class OkCancelForm : Form
{
public IButtonControl AcceptBtn { get; set; }
public IButtonControl CancelBtn { get; set; }
}
In the forms, this class is inherited and in the constructor the form buttons are assigned to the fields:
public partial class MyForm : OkCancelForm
{
public MyForm()
{
InitializeComponent();
AcceptBtn = this.okBtn;
CancelBtn = this.cancelBtn;
}
Finally, I used these fields in the SetDialogAppearance class:
static public void SetDialogAppearance(OkCancelForm form)
{
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.Icon = PresentationLayer.Properties.Resources.embraer;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = form.AcceptBtn;
form.CancelButton = form.CancelBtn;
form.KeyPreview = true;
}
I don’t know if it’s the best solution (neither the most elegant) but it does work. If someone has a better suggestion I’d really appreciate to know.

Re-using same method in multiple windows forms

I would like to ask for your help. Will you help captain newbie once again? :)
I have several windows forms where I use datagridview. I would like to format the datagridviews in the same way on all the forms (e.g. AllowUserToAddRows = false;).
To do this I created a class MYFormatting and method as below. I am going to use composition to re-use this method on multiple forms. I would be grateful if you could let tell me if my approach is correct?
public class MyFormating
{
public void FormatDGV(DataGridView dgv)
{
dgv.AllowUserToAddRows = false;
dgv.AllowUserToDeleteRows = false;
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgv.ColumnHeadersVisible = false;
dgv.RowHeadersVisible = false;
dgv.MultiSelect = false;
}
}
When initializing new form
Form1 frmForm1 = new Form1(new MyFormating());
Then in each form I am going to call myFormat method and pass datagridview.
public partial class Form1 : Form
{
private readonly MyFormating _myFormat;
public Managers(MyFormating myFormat)
{
InitializeComponent();
_myFormat = myFormat;
_myFormat.FormatDGV(dgvManagers);
Leaving MyFormatting class as it is and then changing my Form1 code to below would give me same result. Is this still composition? Should I do something like this or convention rather say to follow the way I described above?
When initializing new form
Managers frmManagers = new Managers();
Then in each form I am going to create new MyFormatting instance and pass datagridview to it's method
public partial class Form1: Form
{
private readonly MyFormating _myFormat;
public Form1()
{
InitializeComponent();
myFormat = new MyFormating();
myFormat.FormatDGV(dgvManagers);
I understand your purpose. "Extension" is a more practical method;
public static class UIExtensions
{
public static void FormatDGV(this DataGridView dgv)
{
dgv.AllowUserToAddRows = false;
dgv.AllowUserToDeleteRows = false;
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgv.ColumnHeadersVisible = false;
dgv.RowHeadersVisible = false;
dgv.MultiSelect = false;
}
}
and in your form code(onload or constructor);
dgvManagers.FormatDGV();
Look for Extension Methods: https://learn.microsoft.com/tr-tr/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
I suggest you make a new class which inherits from DataGridView. In this class just set the propertys in the constructor as you wish. Rebuild the project and your own DataGridView should show up in the Toolbox. Just place it on the Form like you would do with the standard DataGridView.
Example for the DataGridView with predefined values:
public class MyDataGridView: DataGridView
{
public MyDataGridView()
{
AllowUserToAddRows = false;
AllowUserToDeleteRows = false;
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
ColumnHeadersVisible = false;
RowHeadersVisible = false;
MultiSelect = false;
}
}
And also a screenshot of the custom DataGridView in Action:

Why are my form1 elements inaccessible? [duplicate]

This question already has answers here:
Why is the control inaccessible due to its protection level?
(4 answers)
Closed 5 years ago.
In my project, I'm dynamically generating a lot of elements on a form, like buttons, picture boxes etc.
To separate the code, I'll be making new classes. I've made an instance of form1 in my new class, but all the elements are inaccessible -- why is this?
Edit: below is the relevant code. I'm trying to create buttons in CreateParty, but when I reference an element in form1 (in this case gameScrollBar), I get an error
Inaccesible due to its protection level
My code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CreateParty createparty = new CreateParty(this);
generateIcons();
}
}
public class CreateParty
{
private Form1 mainForm;
public CreateParty(Form1 form1)
{
mainForm = form1;
testVoid();
}
public void testVoid()
{
Button NAButton = new Button();
NAButton.Height = 100;
NAButton.Width = 100;
NAButton.Location = new Point(190, 100);
NAButton.Text = "NA";
mainForm.gameScrollBar.Controls.Add(NAButton);
}
}
Any insight would be greatly appreciated.
You have declared the Form1 variable in your CreateParty class as private. Update your code to reflect:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CreateParty createparty = new CreateParty(this);
generateIcons();
}
public class CreateParty
{
public Form1 mainForm; //This line was updated
public CreateParty(Form1 form1)
{
mainForm = form1;
testVoid();
}
public void testVoid()
{
Button NAButton = new Button();
NAButton.Height = 100;
NAButton.Width = 100;
NAButton.Location = new Point(190, 100);
NAButton.Text = "NA";
mainForm.gameScrollBar.Controls.Add(NAButton);
}

c# constructor issues (object is null)

i been trying to link constructors but i been having issues as i have a lot of different classes witch share a lot of info with each other as so as it stands currently this is what i want to do
get one of the classes to reverence form1 so i can run a certain method
so this is what i have tryied so far
gscadding.cs
public SoundAlis sounds;
public addingweapons dlc3gsc;
public Form1 elfgsc;
public gscadding elfy;
private gscadding elfy1;
public gscadding(addingweapons dlc3)
{
dlc3gsc = dlc3;
}
public gscadding(gscadding elfy1)
{
// TODO: Complete member initialization
this.elfy1 = elfy1;
}
this is the class i want to get into form one to use a function i made in form 1
this is what i have in form1
elfenlied_program_settings elf;
addingweapons elf_weap;
Parser elfenliedl;
gscadding elfy;
//Parser parser = new Parser("model1887_sp");
public Form1()
{
InitializeComponent();
updater1.CheckForUpdates();
listBoxAdv1.Visible = false;
elf = new elfenlied_program_settings(listBoxAdv1,elfenliedl);
elf_weap = new addingweapons(richTextBoxEx1);
elfenliedl = new Parser("model1887_sp");
elfy = new gscadding(elfy);
timer1.Start();
elf.buttonX2 = buttonX2;
elf.elfenform = this;
// elfe.elfgsc = this;
buttonX2.Visible = false;
buttonX3.Enabled = true;
textBoxX6.Enabled = false;
elfenliedl.buttonX1 = buttonX1;
basicly what im trying to do is form gscadding.cs to call a function called updatesettings();
witch when the form loads and i select a path it updates all string = paths
but i keep getting the
elfgsc is null you have to set it.
public gscadding(addingweapons dlc3, Form1 form)
{
dlc3gsc = dlc3;
elfgsc = form;
}
In your form constructor
elfy = new gscadding(elfy, this);
Now you can call methods in your form from your gscadding class like so:
elfgsc.updatesettings();
As to your second error, passing elfy to itself is useless. Remove elfy and replace with elfy_weap

Accessing Form's Controls from another class

I have a windows forms application with some controls added to the designer. When I want to change something (LIKE) enabling a text box from inside the Form1.cs, I simply use:
textBox1.Enabled = true;
but now I have a separated class called class1.cs.
How could I enable textBox1 from a static function class1.cs?
NOTE: I did not try any code because I am totally clueless about doing this.
EDIT: Lot of edit.
public partial class Form1 : Form
{
// Static form. Null if no form created yet.
private static Form1 form = null;
private delegate void EnableDelegate(bool enable);
public Form1()
{
InitializeComponent();
form = this;
}
// Static method, call the non-static version if the form exist.
public static void EnableStaticTextBox(bool enable)
{
if (form != null)
form.EnableTextBox(enable);
}
private void EnableTextBox(bool enable)
{
// If this returns true, it means it was called from an external thread.
if (InvokeRequired)
{
// Create a delegate of this method and let the form run it.
this.Invoke(new EnableDelegate(EnableTextBox), new object[] { enable });
return; // Important
}
// Set textBox
textBox1.Enabled = enable;
}
}
This is just another method:
TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox;
You shouldn't really change UI controls in your Form from your class1, but instead create a method or a property in class1 that would tell if the textbox should be enabled or not.
Example:
// I changed the name class1 to MySettings
public class MySettings
{
public bool ShouldTextBoxBeEnabled()
{
// Do some logic here.
return true;
}
// More generic
public static bool SetTextBoxState(TextBox textBox)
{
// Do some logic here.
textBox.Enabled = true;
}
// Or static property (method if you like)
public static StaticShouldTextBoxBeEnabled { get { return true; } }
}
Then in your form:
MySettings settings = new MySettings();
textBox1.Enabled = settings.ShouldTextBoxBeEnabled();
// Or static way
textBox1.Enabled = MySettings.StaticShouldTextBoxBeEnabled;
// Or this way you can send in all textboxes you want to do the logic on.
MySettings.SetTextBoxState(textBox1);
You can pass the instance of your Form to the class
MyForm frm = new MyForm();
MyClass c = new MyClass(frm);
Then your class can take that instance and access the textbox
public class MyClass
{
public MyClass(MyForm f)
{
f.TextBox1.Enabled = false;
}
}
The design does not look OK
It is better to call the class in your form and based on the value returned, manipulate the textbox
//MyForm Class
MyClass c = new MyClass();
c.DoSomethings();
if(c.getResult() == requiredValue)
textBox1.enabled = true;
else
textBox1.enabled = false;
//MyForm Class ends here
UPDATE
public class Class1
{
public static int SomeFunction()
{
int result = 1;
return result;
}
public static void SomeFunction(out int result)
{
result = 1;
}
}
Usage
if(Class1.SomeFunction() == 1)
textBox1.Enabled = true;
else
textBox1.Enabled = false;
OR
int result = 0;
Class1.SomeFunction(out result);
if(result == 1)
textBox1.Enabled = true;
else
textBox1.Enabled = false;
You could let your class1 have an event to enable the Textbox.
public class Class1
{
public event Action<object, EventArgs> subscribe ;
private void raiseEvent()
{
var handler = subscribe ;
if(handler!=null)
{
handler(this,EventArgs.Empty);//Raise the enable event.
}
}
}
Let the class containing the TextBox subscribe to it somehow. In TextBox wrapper class
public class TextBoxWrapper
public void EnablePropertyNotification(object sender, EventArgs args)
{
TextBox1.Enabled = true ; //Enables textbox when event is raised.
}
public TextBoxWrapper()
{
class1Instance.subscribe+=EnablePropertyNotification ;
}
To access/modify a Form Element property, just write this in your outside Class.
Form1.ActiveForm.Controls["textBox1"].Enabled = true;
Where textBox1 is the variable name of TextBox.
What this actually does: Gets the active Form object's control specified by the name in string.
WARNING: Active form means the form which is currently open and focused on. If you do something else on your computer, with your minimized WindowsForm application, the Form1.ActiveForm will not get the form, instead, it will give null, which can lead to errors later. Be careful!
based on the answer from #vr_driver you can do that to avoid problems with other containers like groupbox, panels...
TextBox t = Application.OpenForms["Form1"].Controls.Find("textBox1", true)[0] as TextBox;
In this example you have a form called Main.cs and a class called MyClass:
In MyClass (Note: the name of my Form Class = 'Main'):
Main ui = new Main();
ui.toolStripProgressBarStickers.PerformStep();
In (FormName).Designer.cs so in my case Main.designer.cs change the appropriate control from 'private' to 'public':
public System.Windows.Forms.ToolStripProgressBar toolStripProgressBarStickers;
This solved it for me.
Thanks, Ensai Tankado
I had to do this at work and didn't find that any of these answers matched what I ended up doing, so I'm showing how I made it work.
First, initialize a copy of your class in your load event.
NameOfClass newNameofClass;
Then you want to bind to your class (in the load event):
textBox1.DataBindings.Add(new Binding("Enabled", newNameofClass, "textBox1Enabled"));
In your class, do the following:
private bool textBox1Enabled = false;
public bool TextBox1Enabled
{
get
{
return textBox1Enabled;
}
set
{
textBox1Enabled = value;
}
}
The false setting will initialize your textbox to being disabled
Set textBox1Enabled to true if you want to enable by default.
If you have other logic to enable/disable the textbox, simply modify the value of textBox1Enabled accordingly.
Very easy:
Create an Instance of your Form Object where want to access the Elements from.
Form1 ui = new Form1();
and now change the Form Elements to "public" - like this in the Designer Code:
...
public System.Windows.Forms.TextBox textBox6;
...
Now you can access them like this in your Code:
ui.textBox6 ...
I had the same problem. I used windows forms & Visual Studio to generate a UI in a utility with textbox, checkbox, and button controls but ALL the code was in the same class.
I'm rewriting the utility now that I know "more" OOP concepts and created actual objects and separate classes. I too had problems getting the separate classes to be able to access the form controls and any shared methods that are in the form class. I tried the various suggestions in this thread as well as other threads but none of those solutions worked for me.
What worked for me (not sure if its the right thing to do or not) was I had each class that needed to access the controls and forms methods inherit from the Form.
Here is the relevant part of the Form.cs file:
namespace Utility
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void WriteNote(string noteText, bool asHeading = false)
{
//Writes Messages to the Message box in the UI
Font font1 = new Font(this.ResultsTB.Font, FontStyle.Bold);
Font font2 = new Font(this.ResultsTB.Font, FontStyle.Regular);
if (asHeading)
this.ResultsTB.Font = font1;
else
this.ResultsTB.Font = font2;
this.ResultsTB.AppendText(noteText + "\r\n");
}
My Form contains a textbox called DirTB and a method called "WriteNote" that writes info to another textbox called ResultsTB. Here is the class (at least as far down as the first successful call of the WriteNote method from the Form:
namespace Utility
{
public class AppServerDTO : Form1
{
#region App Server attributes
//attributes listed here
#endregion App Server attributes
#region AppServerDTO Constructor
public AppServerDTO()
{
//These methods verify and set all the attributes
VerifyInstallFolder();
}//end of constructor AppServer
#endregion AppServerDTO Constructor
#region AppServerDTO class methods
public void VerifyInstallFolder()
{
string keypath = string.Empty;
string locationVerification = DirTB.Text + #"\SomeText";
for (int i = 0; i < 4; i++) //allows 3 attempts to get the install folder right
{
if (Directory.Exists(locationVerification))
{
i = 4;//Kills the loop
}
else if (!Directory.Exists(locationVerification))
{
locationVerification = DirTB.Text + #"\SomeMoreText";
}
else if (!Directory.Exists(locationVerification))
{
WriteNote("The directory listed in the Install Directoy box is not reachable.");
WriteNote("Please select the correct directory.");
WriteNote("The correct directory is the folder that contains the ApplicationUpdates & UpdateManager folders.");
WriteNote(#"i.e. C:\Somewhere or D:\Someplace\Somewhere");
var folderpath = FolderPrompt(#"C:\");
DirTB.Text = folderpath; //updates the install folder textbox to the new location
keypath = folderpath;
i++;
}
}//end for loop
}//end VerifyInstallFolder
As long as you are very careful with what you mark as public vs private, it should be ok.
This is how you should do :
I wrote the code below in my form class :
public static Form1 form = null;
private delegate void SetImageDelegate(Image image);
public Form1()
{
InitializeComponent();
form = this;
}
public static void SetStaticImage(Image image)
{
if (form != null)
form.pic1.Image = image;
}
private void setImage(Image img)
{
// If this returns true, it means it was called from an external thread.
if (InvokeRequired)
{
// Create a delegate of this method and let the form run it.
this.Invoke(new SetImageDelegate(setImage), new object[] { img });
return; // Important
}
// Set textBox
pic1.Image = img;
}
and the code below should be in anouther class :
Form1 frm= Form1.form;
frm.pic1.Image = image;
Note that i changed private static Form1 form = null; to public static Form1 form = null;
Good Luck ... Written by Hassan Eskandari :)

Categories