C# .net windows forms dialog next/previous buttons - c#

In a C# .net windows forms application, I have a dialog window with the buttons "next" and "previous", and I want to "move to the next or previous page" according to what buttons the user pressed.
How to achieve this ?

Use Panel or GroupBox to wrap your textboxes,labels and buttons.
Then in your Previous Page and Next Page button, call your groupBox name.
groupBox.Hide();
and
groupBox.Show();
will do the trick.
For example if you are calling your first page:
groupBox1.Show();
groupBoxOtherPage1.Hide();
groupBoxOtherPage2.Hide();
PS : You can do Hide() and Show() to panel also, actually all your form element, but grouping your element in container like Panel or GroupBox will be best.

There are couple of tool that you can use for Next and Previous step wizard. for example DevExpress Wizard Control.
But, if you want to go with the simple windows form application you need to follow some trick. Like take some panels and add it to your form. every panel should be placed on form and all panels location and size should be same and named it panel0, panel1, panel2, etc. and take three buttons btnNext, btnPrevious and btnClose and write some code to navigate that panels
int TotalPanelCount = 3; //last index of panel will be 2
int _index = 0;
public int Index
{
get { return _index; }
set {
if (TotalPanelCount < 3)
_index = value;
else
_index = TotalPanelCount - 1;
ChangeIndex();
}
}
private void btnNext_Click(object sender, EventArgs e)
{
Index++;
}
private void btnPrevious_Click(object sender, EventArgs e)
{
Index--;
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void ChangeIndex()
{
string _panelName = "penal" + Index.ToString();
//Hide all visible panel
var panels = (From Control cnt in this.Controls
Where cnt.getType().Name.Equals("Panel") &&
cnt.Name != _panelName && cnt.Visible == true
Select cnt).ToArray();
foreach(Control cnt in panels)
cnt.Visible = false;
//Displaying current panel
Panel pnl = (Panel)this.Controls[_panelName];
pnl.BringToFront();
pnl.Visible = true;
}

Related

How to make a button invisible even when I refresh a page depending on a label content in xamarin forms

When I click on the button it becomes hidden, however when I refresh the page it is still there. How to make it stay hidden depending on a content in a label?
private void Button3_Clicked(object sender, EventArgs e)
{
App.products[Index].Quantity++;
Button btn = (Button)sender;
if (btn.IsVisible)
{ btn.IsVisible = false; }
else
{
btn.IsVisible = false;
}
}
I want this Button to stay hidden when the page is refreshed depending on this value App.products[Index].Quantity. When I click on the Button it becomes from 0 to 1 and I want if it is not 0 the Button to be hidden.
In YourPage.xaml:
<Button IsVisible={Binding ButtonIsVisible} ... />
In YourPage.xaml.cs:
public YourPage()
{
InitializeComponent();
...
BindingContext = this;
}
// NOTE: `=>` not `=`. To be a property; expression evaluated every time it is needed.
public bool ButtonIsVisible => App.products[Index].Quantity == 0;
private void Button3_Clicked(object sender, EventArgs e)
{
App.products[Index].Quantity++;
// So xaml sees the change.
OnPropertyChanged(nameof(ButtonIsVisible));
}
For more information, google xamarin binding.

C# - How to choose which Control User load first?

I'm using WinForm to do my little CRM.
I got MainWindow form and on this form 3 panels ( 1 Top/ 1Left/ the other part is filled up by 7 Control User )
All the Control User are on top of each other, and when i click on some button( attached to the left panel) the called CU is brought to the front.
public void BtnContact_Click(object sender, EventArgs e)
{
contactControl1.Visible = true;
contactControl1.BringToFront();
panelBar.Height = BtnContact.Height;
panelBar.Top = BtnContact.Top;
employe1.Visible = false;
comptaControl1.Visible = false;
histoControl1.Visible = false;
alerteControl1.Visible = false;
voyageursControl1.Visible = false;
parametresControl1.Visible = false;
}
I don't want all the CU to load at the start of the App, but i want them to be launch when i click on the button on the left. And let say if i opened one and now opening a new one it close the one who was opened.
If i have no choice to open everything ( which i doubt ) how can i choose the one i want to open first or second etc ??
Thank you
I don't want all the CU to load at the start of the App
Use properties tab and make visible=false or use formload event to prevent all cu loading at the start.
private void Form1_Load(object sender, System.EventArgs e)
{
//use code to make anything visible or invisible
}
you can put all controls that should be shown or hidden on the panel.
This is called the other part is filled up by 7 Control User in the question, so just use Panel to store all those controls.
Then you can introduce new field on your form to store current control index that is shown and to write button click that will check the index and activate next control
int currentIndex = -1;
public void BtnContact_Click(object sender, EventArgs e)
{
currentIndex++;
if (currentIndex >= panelFor7Control.Controls.Count)
currentIndex = 0;
foreach (Control c in panelFor7Control.Controls)
c.Visible = false;
Control control2show = panelFor7Control.Controls[currentIndex] ;
control2show.Visible = true;
}
Button BtnContact has to be pressed to activate next control on the panel.
PS I suppose that you are trying to load data for the visible control only. You can use Control.VisibleChanged to check is data loaded and to load if control is visible and data was not loaded.
First you need to make instance of your UC, something like this:
public partial class YourUC : UserControl
{
public static YourUC _instance;
public static YourUC Instance
{
get
{
if (_instance == null)
_instance = new YourUC();
return _instance;
}
}
}
Then you need to call and show UC when button is clicked
private void btn_Click(object sender, EventArgs e)
{
if (!pnlControls.Controls.Contains(YourUC.Instance))
{
pnlControls.Controls.Add(YourUC.Instance);
YourUC.Instance.Dock = DockStyle.Fill;
YourUC.Instance.BringToFront();
}
else
{
YourUC.Instance.BringToFront();
}
}
You it sounds like you want to have a "pages" navigation, I'm not use to work with WinForm, but perhaps this post could help you
What is the most efficient way to create a Win form with multiple pages?
It seems that you have to manage what Control you want to display manually.
Note that passing Controls from Visible to Hidden doesn't unload them

Switching to a tab in TabControl using code

I have a tabcontrol in my application that has several tabs in it.
I want to automatically switch to another tab when the "Next" button is pressed.
I cannot figure out how to change which tab is visible programmatically.
private void Next_Click(object sender, EventArgs e)
{
// Change to the next tab
tabControl1.???;
}
Use the TabControl.SelectedTab property. MSDN.
tabControl1.SelectedTab = anotherTab;
But you can also use the TabControl.SelectedIndex property. MSDN.
try
{
tabControl1.SelectedIndex += 1;
}
catch
{
//This prevents the ArgumentOutOfRangeException.
}
For this particular scenario you can use SelectedIndex property of the TabControl. This gives you an integer representing the index of the currently selected tab. Likewise you can set a tab as selected by setting an integer value to this property.
private void btnNext_Click(object sender, EventArgs e)
{
int currentTabIndex = tabControl1.SelectedIndex;
currentTabIndex++;
if (currentTabIndex < tabControl1.TabCount)
{
tabControl1.SelectedIndex = currentTabIndex;
}
else
{
btnNext.Enabled=false;
}
}

How to open a specific tab in windows form

all these tab are created dynamically in windows form . I want to open specific tab page on button click.
For example when clicking on a button(button is not tab page button ,its some other execution button), i want to display tab3 .
I am able to get no of tab pages, but unable to open specific tab..
private void toolStripButton1_Click(object sender, EventArgs e)
{
int tabcount = Main_tab.TabCount;
MessageBox.Show(tabcount.ToString());
}
TabControl.SelectTab Method
this.tabControl1.SelectTab(1); // by index
this.tabControl1.SelectTab("tab3"); // by tabPageName
this.tabControl1.SelectTab(tabPage); // by tab page
Or
TabControl.SelectedIndex Property
this.tabControl1.SelectedIndex = 1; //Selects second tab of the tab control
or
TabControl.SelectedTab Property
this.tabControl1.SelectedTab = tabPage2;
You can access it through its index and call its methods for showing.
Main_tab.GetControl(index_of_your_tab);
You can find the tabs under Controls of the TabControl.
Use Find to find the specific tab by name
Main_tab.SelectedTab = (TabPage)Main_tab.Controls.Find("tab3", searchAllChildren: false).First();
private void toolStripButton1_Click(object sender, EventArgs e)
{
int tabcount = Main_tab.TabCount;
for (int count = 0; count < class_new_tab.tab_count; count++)
{
Main_tab.SelectTab(count);
//perform tab operation
}
}

i want to dock User Control

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.

Categories