Using xaml states to change window size programatically - c#

I have created 2 states for the main window of the program (big and small) and i have added a tabcontrol with 2 tab items. I want to use the big state to change the size of the window when i am in tab1 and small state to change the size of window when i am in tab2. I used the following code but an unable to change the window size.
private void Changes(object sender, SelectionChangedEventArgs e)
{
if (tab1.IsSelected)
{
VisualStateManager.GoToElementState(window, "Big", true);
b1.Text = Convert.ToString(i);
}
else
{
VisualStateManager.GoToElementState(window, "Small", true);
b1.Text = Convert.ToString(i);
}
}
the method Changes is the tabcontrol's SelectionChanged event.
I cannot find what i did wrong here.

Related

Devexpress TabHeader Disappears

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.

Container UserControl - Handle and Modify Added Controls

I'm trying to create a custom container as UserControl.
My Goal: I want to be able to drag controls inside the designer and handle incoming controls inside the code of my usercontrol.
Example: I place my container somewhere and then add a button. In this momemt I want my usercontrol to automatically adjust the width and position of this button. Thats the point where Im stuck.
My code:
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public partial class ContactList : UserControl
{
public ContactList()
{
InitializeComponent();
}
private void ContactList_ControlAdded(object sender, ControlEventArgs e)
{
e.Control.Width = 200; // Nothing happens
e.Control.Height = 100; // Nothing happens
MessageBox.Show("Test"); // Firing when adding a control
}
}
The MessageBox is working well. The set width and height is ignored.
The question is just "why?".
EDIT
I've just noticed, when placing the button and recompiling with F6 the button gets resized to 200x100. Why isnt this working when placing?
I mean... the FlowLayoutPanel handles added controls right when you place it. Thats the exact behaviour im looking for.
Using OnControlAdded
To fix your code, when you drop a control on container and you want to set some properties in OnControlAdded you should set properties using BeginInvoke, this way the size of control will change but the size handles don't update. Then to update the designer, you should notify the designer about changing size of the control, using IComponentChangeService.OnComponentChanged.
Following code executes only when you add a control to the container. After that, it respects to the size which you set for the control using size grab handles. It's suitable for initialization at design-time.
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
if (this.IsHandleCreated)
{
base.BeginInvoke(new Action(() =>
{
e.Control.Size = new Size(100, 100);
var svc = this.GetService(typeof(IComponentChangeService))
as IComponentChangeService;
if (svc != null)
svc.OnComponentChanged(e.Control,
TypeDescriptor.GetProperties(e.Control)["Size"], null, null);
}));
}
}

C# TreeView DrawNode : How to add the badge images in the TreeNodes?

I want to add just the badge images in the right of the TreeNodes.
The expand/Collapse buttons, icons and texts shouldn't be changed.
I used the following code :
public void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
e.DrawDefault = true;
e.Graphics.DrawImage(image1, e.Bounds.Right - 30, e.Bounds.Y);
}
However, if the text of the treenode was long, it was over the image. The badge is invisible.
I think that e.DrawDefault = true; may affect after the function returns.
How can I show the badge images without redrawing the texts and icons?

Setting ToolStripMenuItem.Visible to true doesn't work

I have a TreeView control for which each node in it I want to share a ContextMenuStrip which has two ToolStripMenuItems ie:
this.BuildTree = new MyApp.MainForm.TreeView();
this.ItemMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
this.DeleteMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ShowLogMenuItem = new System.Windows.Forms.ToolStripMenuItem();
...
this.ItemMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.DeleteMenuItem,
this.ShowLogMenuItem});
So I show and hide these to items according to certain criteria on a right click in a MouseUp event. When both are hidden I hide the ContextMenuStrip itself. Problem is when I hide the ContextMenuStrip it seems the next time I want to show one of the menu items I have to click twice on the node. The strange thing is on the first click to reshow one or both of the the items I have the following code:
ItemMenuStrip.Visible = true;
ShowLogMenuItem.Visible = true;
The two lines above don't seem to do anything ie both remain false in the debugger view after stepping over each line.
I don't think I've got any events on these values being set at least I don't have any events attached.
What am I doing wrong?
I suggest you to set:
this.BuildTree.ContextMenuStrip = this.ItemMenuStrip;
to make the menu automatically open on tree right-click.
Then subscribe ItemMenuStrip.Opening event to change the visibility of items and the contextmenu itself:
void ItemMenuStrip_Opening(object sender, CancelEventArgs e)
{
if (something)
{
e.Cancel = true; // don't show the menu
}
else
{
// show/hide the items...
}
}
If you need to know the current position of the clicked point (e.g. to check if a tree node is clicked), you can use Control.MousePosition property. Note that MousePosition is a point in screen coordinates, so you need to call treeView1.PointToClient(position) to get the tree coordinates e.g. :
private void ItemMenuStrip_Opening(object sender, CancelEventArgs e)
{
var pointClicked = this.BuildTree.PointToClient(Control.MousePosition);
var nodeClicked = this.BuildTree.GetNodeAt(pointClicked);
if (nodeClicked == null)
{
// no tree-node is clicked --> don't show the context menu
e.Cancel = true;
}
else
{
// nodeClicked variable is the clicked node;
// show/hide the context menu items accordingly
}
}
So figured out what was going wrong I was setting Visible on this.ItemMenuStrip rather than the this.BuildTree.ContextMenuStrip.
This seems rather strange to me as I would have thought BuildTree.ContextMenuStrip was just a direct reference to the ItemMenuStrip but apparently not.

Combobox how to get the selected item to show a theme

I'm doing a tic tac toe game and I am trying to add a combo box that will change the applications background based on what the person selects right now I have summer, spring, fall, winter and the images are in the bin/debug folder how can I get this to work I don't know where to start and the tutorials are a bit confusing. Could you please help me
It isn't exactly clear what you are asking. Assuming you've got bitmap files with names like "spring.png" etc in your bin\Debug folder, this ought to work:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
comboBox1.Items.AddRange(new string[] { "Spring", "Summer", "Fall", "Winter" });
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
string folder = Application.StartupPath;
string theme = (string)comboBox1.Items[comboBox1.SelectedIndex];
string path = System.IO.Path.Combine(folder, theme + ".png");
Image newImage = new Bitmap(path);
if (this.BackgroundImage != null) this.BackgroundImage.Dispose();
this.BackgroundImage = newImage;
}
}
There are many ways to do this. This is probably the simplest:
Set your main form's BackgroundImageLayout to Stretch.
Place 4 PictureBox controls on your form, and set their Visible properties to false. Name them pbWinter, pbSpring etc. Set the Image property of each by browsing to the image file for each season.
Add a ComboBox to your form. Add the items "Winter", "Spring", "Summer" and "Fall".
In the combo box's SelectedIndexChanged event handler, check the box's Text property with a switch statement, and set the appropriate back image with code like this:
this.BackgroundImage = pbWinter.Image; // etc. ...
Update: Here's how to do the switch statement:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.Text)
{
case "Winter":
this.BackgroundImage = pbWinter.Image;
break;
case "Spring":
this.BackgroundImage = pbSpring.Image;
break;
// etc...
}
}

Categories

Resources