i want to dock User Control - c#

I have make user control and inside that user control takes two buttons name dock and close respectively.
Now i want to dock my user control to left when i clicks button dock and close my user control when i clicks button close..
(i am trying to use by making object of user control but doesnt helps.....)
void button1_Click(object sender, EventArgs e) {
Container1 obj = new Container1();
if (obj.Dock != DockStyle.None) {
obj.Dock = DockStyle.None;
MessageBox.Show("Dockstyle is None");
}
else {
obj.Dock = DockStyle.Left;
MessageBox.Show("Dockstyle is Left");
}
}

obj needs to be a reference to the instance of your already existing userControl (in your case, the this keyword). You have created a new instead of the Container1 here.
private void button1_Click(object sender, EventArgs e)
{
if (this.Dock != DockStyle.None)
{
this.Dock = DockStyle.None;
MessageBox.Show("Dockstyle is None");
}
else
{
this.Dock = DockStyle.Left;
MessageBox.Show("Dockstyle is Left");
}
}

You don't want to create the container and then set the DockStyle on that container. Instead, you need to set the DockStyle of the UserControl itself.

Related

Allow Mouse Wheel scroll on a TabPage Control

A TabPage of a TabControl is populated with using a XML source. Once the XML contents are loaded into the TabPage, two ScrollBars appear on either side of the TabPage, to allow a user to scroll.
The user cannot scroll with the Mouse Wheel, though. I have checked the properties of the TabPage Control but I cannot find any property to assist with this.
Someone suggested to handle the MouseWheel event or override OnMouseWheel, but I'm not sure how this can be applied.
The gist of this is simple, how do I activate the Mouse wheel scroll on a tab page?
public partial class ModifyTransformerContentsView : Form
{
private readonly ITransformerConfigurationViewModel ViewModel;
public ModifyTransformerContentsView(ITransformerConfigurationViewModel viewModel)
{
InitializeComponent();
this.ViewModel = viewModel;
this.ViewModel.Notify += this.OnNotify;
this.xmlEditExampleStdfOutFile.SetFormateText(File.ReadAllText(this.ViewModel.SampleProcessingFilePath));
this.xmlEditExampleStdfOutFile.ReadOnly = true;
this.rtbXsl.SetFormateText(File.ReadAllText(this.ViewModel.TransformerFilePath));
this.rtbXsl.ReadOnly = false;
this.rtbXsl.RichTextBox.ClearUndo();
this.btnSave.Enabled = false;
this.rtbCheatSheet.Text = File.ReadAllText(this.ViewModel.CheatSheetFilePath);
}
private void OnValidateClick(object sender, System.EventArgs e)
{
this.ViewModel.SetTemporaryTransformerFileContents(this.rtbXsl.Text);
this.ViewModel.ValidateXsl(this.rtbXsl.Text,
validationSuccessful =>
{
this.btnSave.Enabled = validationSuccessful;
this.rtbExampleOutputFileContents.SetFormateText(this.ViewModel.ExampleFileOutputContents);
});
}
private void OnSaveClick(object sender, System.EventArgs e) => this.ViewModel.Save(this.rtbXsl.Text);
private void OnNotify(NotificationEventArgs obj)
{
switch (obj.NotificationType)
{
case NotificationType.Info:
MessageBox.Show(obj.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (obj.Exit)
{
this.Close();
}
break;
case NotificationType.Warning:
MessageBox.Show(obj.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
break;
case NotificationType.Error:
MessageBox.Show(obj.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (obj.Exit)
{
this.Close();
}
break;
}
}
private void ModifyTransformerContentsView_FormClosing(object sender, FormClosingEventArgs e)
=> this.ViewModel.DeleteTemporaryModifiedTransformerFile();
private void OnButtonCheatSheetSaveClick(object sender, System.EventArgs e) =>
this.ViewModel.SaveCheatSheet(rtbCheatSheet.Text);
private void ModifyTransformerContentsView_Load(object sender, System.EventArgs e)
{
}
}
Any assistance would be appreciated.
The TagPage Control class is derived from the Panel class.
This type of Control is not selectable (ControlStyles.Selectable is set to false in its Constructor), so it doesn't receive focus and cannot be selected with a Mouse click.
You can override this behavior in different ways. Three simple methods:
Build a Custom Control derived from TabPage then:
in its Constructor, call SetStyle():
SetStyle(ControlStyles.Selectable |
ControlStyles.UserMouse |
ControlStyles.StandardClick, true);
Create an instance of this Custom Control and add it to the TabPages of a TabControl
Build a Custom Control derived from Panel:
Set the same ControlStyles in its Constructor
Build the Control, find it in the ToolBox and drop it inside a TabPage, then set Dock = DockStyle.Fill and AutoScroll = true (or do this directly in the Custom Control class).
Add all child Controls to this Panel.
Using Reflection, get the non-public SetStyle method of a TabPage and use MethodInfo.Invoke() to change the values. e.g.,:
using System.Reflection;
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
var method = tabPage1.GetType().GetMethod("SetStyle", flags);
var newStyles = ControlStyles.Selectable | ControlStyles.UserMouse;
method.Invoke(tabPage1, new object[] { newStyles, true });
You can do this to all TabPages in a loop.

Saving and loading control values when switching between different User Controls in C#

I have a splitcontainer. In Panel 1 I have 7 toolstripbuttons that opens a User Control in Panel 2 when I click on them. The user control consists of different textboxes that the user can fill in. The code looks like this:
private void toolStripButtonBaseLayers_Click(object sender, EventArgs e)
{
splitContainer1.Panel2.Controls.Clear();
BaseLayers BL = new BaseLayers();
splitContainer1.Panel2.Controls.Add(BL);
}
private void toolStripButtonSpatialCoverage_Click(object sender, EventArgs e)
{
splitContainer1.Panel2.Controls.Clear();
SpatialCoverage SC = new SpatialCoverage();
splitContainer1.Panel2.Controls.Add(SC);
}
private void toolStripButtonFloodMaps_Click(object sender, EventArgs e)
{
splitContainer1.Panel2.Controls.Clear();
FloodMaps FM = new FloodMaps();
splitContainer1.Panel2.Controls.Add(FM);
}
Now everytime I open another user control with the click event the user control will be empty because I clear the panel and open a new User Control. This is not what I want to do!
My question is how it is possible to save everything you filled in in the textboxes? And how it is possible that all the user control values will be still filled in when switching between them? For example I fill the user control BaseLayers in, I switch to the user control Spatial Coverage and Flood Maps and when I swith back to the user control BaseLayers everything still have to be filled in.
Create all your controls and hide all of them(Visible=false). Then in Click-Events just set Visible=true for the appropriate control.
Example:
private void toolStripButtonFloodMaps_Click(object sender, EventArgs e)
{
SetChildVisibleForPanel(typeof(FloodMaps), splitContainer1.Panel2);
}
static void SetChildVisibleForPanel(Type visChildType, Panel pnl)
{
if (pnl.Controls.Count==0)
{
//Create your controls with Visible=false;
}
foreach (Control ctl in pnl.Controls)
{
ctl.Visible = (ctl.GetType() == visChildType);
}
}

unable to bind listbox in winform on tabControl1_Selected event?

MainForm.cs
public MainForm()
{
InitializeComponent();
tabControl1.Selected += new TabControlEventHandler(tabControl1_Selected);
}
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
//bind listbox
MyUserControl obj = new MyUserControl();
obj.LoadListBox();
obj.Refresh();
}
inside MyUserControl.cs
public void LoadListBox()
{
listBox1.Items.AddRange(JsonConvert.DeserializeObject <object[]> (File.ReadAllText("MyData.json")));
}
Why not refreshed or bind? In debug mode it is showing data but not shown on UI Form, any clue?
You should add the user control instance to the winform.
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
//bind listbox
MyUserControl obj = new MyUserControl();
obj.LoadListBox();
obj.Refresh();
this.Controls.Add(obj); //Add control instance to the winform.
}
I suggest that, add a Panel control to the form and add user control to this panel. You need clear the panel before adding the user control. But it depends on your requirement. Maybe you do not need to add control every time. So you should write your codes accordingly.
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
//I guess you have a Panel control named as pnl.
pnl.Controls.Clear(); //Clear all child control from panel.
MyUserControl obj = new MyUserControl();
obj.LoadListBox();
obj.Refresh();
pnl.Controls.Add(obj); //Add control instance to the panel.
}

Expand/Collapse Win Form in C#

I want to create UI like this in Windows form application C#.
First my form should appear like this when the chechBox is not checked.
And if the checkBox checked my form changes to like this
How can I do this?
Change the height of the form dynamically on CheckedChanged event of check box. Don't forget to set anchor of below fields or set visible on expand and invisible on collapse.
EDIT: The most simple way to achieve the results is given below.
private readonly int _collapsedHeight;
public Form1()
{
//Set Anchor of Connect button to Right and Bottom and leave default for others
//Optionally you need to hide controls except Connect button on collapse and vice versa.
//Set Form Border Style to FixedSingle and MaximizeBox to false
InitializeComponent();
_collapsedHeight = Height;
}
private void chkAdvancedOption_CheckedChanged(object sender, EventArgs e)
{
//Set Y value to collapse eg. 140, adjust it as required...
Height = chkAdvancedOption.Checked ? _collapsedHeight + 140 : _collapsedHeight;
}
void Page_Load(Object sender, EventArgs e)
{
// Manually register the event-handling method for the
// CheckedChanged event of the CheckBox control.
checkbox1.CheckedChanged += new EventHandler(this.Check_Clicked);
}
void Check_Clicked(Object sender, EventArgs e)
{
**//This is only sample code**
// do your code
if (panel2.Visible)
{
panel2.Visible = false;
cmdAdvanced.Visible = true;
}
}

How to enable a disabled control on click

I have a DevExpress grid, which is disabled on screen. When I click the control, I want it to become enabled. Right now I have a click event set up for the grid:
private void gridPSR_Click(object sender, EventArgs e)
{
gridPSR.Enabled = true;
}
This isn't working. How should I be going about this?
Disabled controls do not receive windows messages, so you will never get the click message on that control. Assuming this is Winforms, you can listen for the click on the form (or whatever control is hosting this grid) and check if the click location is in the rectangle of the disabled control and then enable the control accordingly:
void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (gridPSR.ClientRectangle.Contains(e.Location))
{
gridPSR.Enabled = true;
}
}
I know it's an old post but for me bounds worked instead of ClientRectangle
private void OnPanelMouseClick(object sender, MouseEventArgs e)
{
if ((e.Button == MouseButtons.Left) &&
myControl.Bounds.Contains(e.Location) &&
!myControl.Enabled)
{
myControl.Enabled = true;
}
}
Where myControl is member variable of your control instance. OnPanelMouseClick handler should be linked with MouseClick event of form or container that holds control.
In this code I am setting an event on a disabled TextBox control called 'txtNumLabels'. I tested this code with the text box both on a Form and also with having it within a GroupBox container.
Set an event in the constructor after the 'InitializeComponent();'
this.txtNumLabels.Parent.MouseClick += new System.Windows.Forms.MouseEventHandler(this.txtNumLabels_Parent_MouseClick);
Here is the event handler -
private void txtNumLabels_Parent_MouseClick(object sender, MouseEventArgs mouseEvent)
{
// The Bounds property of a control returns a rectangle of its Location and Size within its parent control
Rectangle rect = txtNumLabels.Bounds;
// Other method that gets the same rectangle -
// Point t = txtNumLabels.Location;
// Size ts = txtNumLabels.Size;
// Rectangle rect = new Rectangle(t, ts);
if (rect.Contains(mouseEvent.Location))
{
txtNumLabels.Enabled = true;
}
}

Categories