I want to assign a specific richtextbox contextmenustrip through mdi parent form code in a mdi child form but it does not work.
How can I set richtextbox1 to use contextMenuStrip2?
The mdi child contains two contextmenustrip already created called contextMenuStrip1 and contextMenuStrip2.
The default value of the richtextbox1 is contextMenuStrip1.
Using the following piece of code,
the richTextBox1 text attribute is changed as expected however trying to change the contextmenustrip does nothing. It keeps the contextMenuStrip1 displayed meanwhile contextMenuStrip2 would be expected because of the line childForm.Controls["richTextBox1"].ContextMenuStrip.Name = "contextMenuStrip2";.
Mdiparent.cs:
Form childForm = new Form1("contextMenuStrip2");
childForm.Show();
Form1.cs:
public Form1(String correctcontextmenu)
{
InitializeComponent();
richTextBox1.ContextMenuStrip = correctcontextmenu;
}
Error list contains:
Error 1 Cannot implicitly convert type 'string' to 'System.Windows.Forms.ContextMenuStrip'
As you found out, a string is not a ContextMenuStrip. Since the ContextMenuStrips are private to the ChildForm, you should maybe just pass a flag to tell the form which menu it should use:
public Form1(bool useOtherMenu)
{
InitializeComponent();
if (useOtherMenu)
{
richTextBox1.ContextMenuStrip = contextMenuStrip2;
}
}
Then to call it:
Form childForm = new Form1(true);
childForm.MdiParent = this;
childForm.Show();
Related
I have created ribbon form (XtraMain)and I set IsMdiContainer Property to true,i also add documentManager controle i set MdiParent to XtraMain I have add this code to open child forms
public void ViewChildForm(XtraForm _form)
{
if (!IsFormActived(_form))
{
_form.MdiParent = this;
_form.Show();
}
}
private bool IsFormActived(XtraForm form)
{
bool IsOpenend = false;
if (MdiChildren.Count() > 0)
{
foreach (var item in MdiChildren)
{
if (form.Name == item.Name)
{
tabbedView1.ActivateDocument(item);
IsOpenend = true;
}
}
}
return IsOpenend;
}
and i use this code in click of button to open the child form
private void bbtnEmployee_ItemClick(object sender, ItemClickEventArgs e)
{
FrmEmployee frme = new FrmEmployee();
frme.Name = "FrmEmployee";
ViewChildForm(frme);
}
my problem start when the form contain a LayoutControl for example i have this code that open on click of button
private void btnBonLivraison_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
LayoutControl lc = new LayoutControl();
lc.Dock = DockStyle.Top;
LookUpEdit OrderNumber = new LookUpEdit();
OrderNumber.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
OrderNumber.Properties.DataSource = shippProdu.GetOrderNumber();
OrderNumber.Properties.DisplayMember = "N° Bon de livraison";
OrderNumber.Properties.ValueMember = "N° Bon de livraison";
lc.AddItem(Resources.selectOrderNumber, OrderNumber).TextVisible = true;
lc.Height = 70;
this.Controls.Add(lc);
this.Dock = DockStyle.Top;
lc.BestFit();
the second I click on a button the tabHeader disappears,what cause this problem?and how can I solve it.before I use documentManager I used XtraTabControl and if i click a button to open LayoutControl and after that try to open another form the focus remaining in the first form even when the form two is already opened and if I want to go to form two I must first click on a tab of the first form and then click on tab of the second form , thanks in advance .
this is my main form
and this is when the eader disappears
If DocumentManager resides within the same form to which you add LayoutControl, it is the expected behavior. DocumentManager places a special documents' host onto the main form and set its Dock property to Fill. That is why it is incorrect to place LayoutControl onto the same form and dock it to form edges.
If you need to show tabbed documents and LayoutControl on the same form simultaneously, do not use the MDI mode. Consider the use of a separate UserControl. Place your DocumentManager there. Then, put this UserControl onto your form. Note that in this case UserControl's Dock property should be set to Top or Bottom since LayoutControl should fill all available area or vice versa.
I tried to create a new form inside the Parent. I set FormBorderStyle to none.
When I am adjusting MDIParent to the myForm, it gave me a sick looking error like this:
System.ArgumentException : The given Form is not being recalled as a MdiContainer.
This is my code for creating a new Windows Form.
FrmHome myForm = new FrmHome ();
myForm.TopLevel = false;
pnlContainer.Controls.Add(myForm);
myForm.Show();
The Mdi parent must have it's IsMdiContainer property set to True.
You can set this property at design time in your main form or runtime :-
Form1 f1 = new Form1();
f1.MdiParent = this;
f1.Show();
Form1 is the name of the form that you want to show.
Form.IsMdiContainer Property
Property Value
Boolean
true if the form is a container for MDI child forms; otherwise, false.
The default is false.
I have a datagridview called logDataGridView in my AnalyzerForm project.
In order to access to it's DataSource property from the other from, called Form2, in the project, below access field has been defined into the AnalyzerForm.Designer.cs:
Public System.Windows.Forms.DataGridView _DGV
{
get {return this.logDataGridView;}
set {logDataGridView.DataSource = value;}
}
And finally, i try to use a filled DataTable called t from the Form2:
AnalyzerForm AZ = new AnalyzerForm();
AZ._DGV.DataSource = t;
Nothing will be shown into the logDataGridView!!!
Does anybody have any idea about the wrong part?
Actually the wrong part of the progress is just reinstantiation of the parent form, as below:
AnalyzerForm AZ = new AnalyzerForm();
One must use the very parent form reference, is which responsible for launching the child form. It is possible to define a secondary constructor for the child parent and feed a parent form object just inside of it:
ParentForm pForm;
public childForm(ParentForm FRM)
{
pForm = FRM;
// Then component initializing...
}
Finally, the required component of the parent form (is which a datagridview, in my case), is possible:
pForm._DVG.DataSource = t;
This is simplest possible thing, but I cant update text on status bar. I just started working in c# but cannot find solution. I tried below code:
Mdiparent
public void StutasText(string text)
{
toolStripStatusLabel.Text = text;
}
Child form
MDIParent1 obj = new MDIParent1();
obj.StutasText("Hello world");
obj.Refresh();
Its not showing status text in the status bar.
Where did I go wrong?
In the MDI Parent form, I assume you have toolStripStatusLabel1. If you dont have, you can add this by clicking on the little black arrow in the menuStrip control.
Option 1
In your MDI Parent (let's assume, frmMain is the MDI Parent form) form where you have the StatusStrip, goto frmMain.Designer.cs file and find the place
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
make this,
public System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
Then from your child pages you can access like below.
ToolStripStatusLabel statusStrip=((frmMain)(frmMdiChild.MdiParent)).toolStripStatusLabel1
Option 2
Declare a public property which will return the toolStripStatusLabel1 control or method where you can set the text property of the toolStripStatusLabel1 in the MDI Parent form. If you return the menuStrip1 itself then you have access to all the properties of that control. If you declare a method, which will set the text property of the toolStripStatusLabel1 then you can only set the text. Decide on what you want, based upon your requirement.
An implementation which returns the menuStrip1 control.
public ToolStripStatusLabel GetStatusBar
{
get
{
return this.toolStripStatusLabel1;
}
}
then from your child pages, you can use,
ToolStripStatusLabel statusStrip=((frmMain)(frmMdiChild.MdiParent)).GetStatusBar;
Option 3
To make it little bit more prettier, you can declare a method in a common class. Then you can reuse this in other child forms.
public void ShowStatusbarMessage(Form frmMdiChild, string message, NotifierType notificationType)
{
ToolStripStatusLabel statusStrip=((frmMain)(frmMdiChild.MdiParent)).GetStatusBar;
statusStrip.Text = message;
if (notificationType == NotifierType.SuccessInfo)
{
statusStrip.ForeColor = System.Drawing.Color.Green;
}
else if (notificationType == NotifierType.Warning)
{
statusStrip.ForeColor = System.Drawing.Color.Orange;
}
else
{
statusStrip.ForeColor = System.Drawing.Color.Red;
}
}
Here, NotifierType is an enum
((mdiMain)MdiParent).toolStripStatusLabel.Text = "My Text";
//but you must change the modifier property of toolStripStatusLabel to public etc
You're creating a new instance of MDIParent1, not using the instance that is shown/the instance your child form belongs to.
You could try using
this.MdiParent
instead of
new MDIParent1()
((frmMDI)this.MdiParent).yourcontrol.yourproperty=yourvalue;
frmMDI is unique name of MDI form.
First
In "mdi parent name".Designer.cs, change the type or member private to public
Second In your code add the next code
(("mdi parent name")MdiParent).toolStripStatusLabel.Text = "your text";
I have 2 forms in my project, form1 and form2. When I click in a button in form1 I run this code:
Form tempform = new Form2();
tempform.Show();
In my code for Form2 I have a label which I now need to change the text.
How can I access the label?
I tried:
tempform.label1.value = "new text"
And that didn't work, I even tried to access using the Controls collection but I think I messed that up. Is there any way I can access the label? OR is there any way I can pass a value to that new form and then have that form alter the label text.
Thanks
If the label value should only be set once, when the form is created, then use a constructor for Form2 like this:
public Form2(string labelValue)
{
_labelValue = labelValue;
}
and then call that constructor when you create the form.
Alternatively, if the label changes over the lifetime of the form, make a public property:
public string LabelValue
{
get { return label1.Text; }
set { label1.Text = value; }
}
Also I would recommend naming the parameters and/or properties to reflect the meaning of the value, for example "titleText" instead of "labelValue". That way Form2 can decide how it wants to display the information (in the title bar, a label, a textbox, etc), and Form1 doesn't have to worry about that.
Edit: Consume the LabelValue property like this:
Form2 newForm = new Form2(); // Assign object to a Form2 instead of Form
newForm.LabelValue = "new text";
newForm.Show();
Controls have protected access by default. You can change that to public, or you can add a method/property to your form2 class to set the label and call that (latter method is generally preferred to preserve encapsulation and because the designer may want to overwrite your public change.).