Tab Control Shares The same buttons Winform - c#

I wonder If it is possible to have fixed number of buttons that is shared by different tab pages. However I don't know how to implement this. Do you guys have any idea.
Heres a screenshot of my gui so that all of you can have a clearer view of what I meant.
I want that that the list of Customers, Reservations, and Check In/out will share the buttons search, edit, delete and refresh.
Is it possible? or should I create diff buttons for every tabpage?
is it correct if i do:
private void buttonSearch_Click(object sender, EventArgs e)
{
if(tabpage.SelectedIndex == 1){ then perform action..}
if(tabpage.SelectedIndex == 2) {then perform action...}
}

You could put the buttons in a User Control, add some events to the User Control (e.g. SearchClicked, EditClicked, etc.). Put the user control outside of the tabcontrol.
Then when you change tabs (TabIndexChanged), remove event handlers from the previous tab, and add event handlers for the new tab:
private void tabControl_TabIndexChanged(object sender, EventArgs e)
{
UserControl1.EditClicked -= OldEventHandler;
UserControl1.EditClicked += NewEventHandler;
}

Yes, you can change the .Parent property of the buttons at runtime - but wouldn't it be better to just move the buttons outside the tab control?

I feel you should create different buttons for every tab page as each one operates on a different entity. If there is only one set of buttons then you will have to first check which tab page is selected and then do the operation. So you will have one big method doing lots of things.
Plus there would be extra UI code that you will have to write to move the buttons when a tab page is selected.
With different buttons you will have highly cohesive and loosely coupled code. Better design. More maintainable and manageable. Less code.

Related

Winforms insert Tab into Tab Control at right place

I'm working with Windows Forms and put a Tab Control into my Form. I have already put some Buttons etc into the first tab and some other code. The problem that I have is the following:
I want to insert a Tab on the first place of the Tab Control but everytime I insert a new Tab he puts it at the end. How can I move the Tab without changing my code? I'm wondering because he made functions like
private void tabPage1_Click(object sender, EventArgs e)
{
}
but my code is beyond this function and I can't even insert it into it.
Use insert function for 0 index.
tabControl1.TabPages.Insert(0, "newTab");
TabControl has collection property TabPages
if you open editor of this property in winforms designer, you will see buttons with arrow up and arrow down in the central part of editor form
click them to change the order of tab pages
you can use this:
myTabCtrl.TabPages.Insert(0, "newTab");

Making a new form window appear in c#

I'm trying to make a card game using Windows Application Forms.
The thing is that I don't know how to do the following- for example if i'd have 3 buttons- one of them named, for example, "Play", if i'd click on it, it would open the actual game, but in the same window, it would only make the buttons dissapear, and when i'd click back, it would open the window with buttons again. I don't really know how to explain my problem better, hopefully someone can tell me how to do that.
You don't have to hide / show the buttons. What you can do instead is to make a new form with the cards on it. That Form will pop up after you click the play button.
private void PlayButton_Click(object sender, EventArgs e)
{
// You other functionality goes here
GameForm GF = new GameForm();
GF.Show();
//Or - try this and see the difference
GF.ShowDialog();
}
Good Luck!
In addition to Leez's answer, in your situation, you should think about using container controls rather than handling the visible states of individual controls.
You could put related controls in a Panel, GroupBox or TabControl and set the visible properties of those containers instead.
you can use Visible property of button to do that as follows.
private void button1_Click(object sender, EventArgs e)
{
// You other functionality goes here
button1.Visible = false;
}

Do I have to define an event in this case?

I've got a form with several controls (to make things simple, say it's a couple of textboxes), which I need to be updated by clicking on two buttons - a Forward button and a Backwards button.
Now, the general idea is that the information to be displayed is stored in some kind of array of objects, and is shown according to the value of some counter. That is, a user clicks on the Forward button -> the counter is incremented -> the corresponding array item is shown on the form. Same goes for the Backwards button.
So, the question is - should I define any specific event in this case? Or is it sufficient to use the standard
private void button1_Click(Object sender, EventArgs e) {}
event which is provided when double-clicking on a button control? I mean, what would be the right thing to do?
I think it's a pretty dumb question, still I appreciate your advice here, thanks in advance!
The click event is an ok place for that logic, however, it's a good practice to extract that forward/backward logic in a separate method (maybe you'll want to go forward by pressing the right arrow?) to something like this:
private void btnForward_Click(Object sender, EventArgs e)
{
GoForward();
}
private void GoForward()
{
// the forwarding code here
}
And, make a habit of naming controls as early as possible, because VS uses the control name to name the event handler, and button1_Click is not very descriptive :)

Best way to swap two .NET controls based on radio buttons

I've got a form where I have two radio buttons and two interchangeable controls (made up of a ListView and a handful of buttons). Based on which radio button is selected I want to display the proper control to the user.
The way I'm doing this now is just loading both controls and setting up an OnRadioButtonSelectionChanged() method which gets called at form load (to set the initial state) and at any time the selection is changed. This method just sets the visible property on each control to the proper value.
This seems to work well enough, but I was curious as to if there was a better or more common way of doing it?
Yep, that's pretty much how I do it. I would set the CheckedChanged event of both radio buttons to point at a single event handler and would place the following code to swap out the visible control.
private void OnRadioButtonCheckedChanged(object sender, EventArgs e)
{
Control1.Visible = RadioButton1.Checked;
Control2.Visible = RadioButton2.Checked;
}
Well you could also use databinding... seems a bit more elegant to me. Suppose you have two radiobuttons "rbA" and "rbB" and two textboxes "txtA" and "txtB". And you want to have txtA visible only when rbA is checked and txtB visible only when rbB is checked. You could do it like so :
private void Form1_Load(object sender, EventArgs e)
{
txtA.DataBindings.Add("Visible", rbA, "Checked");
txtB.DataBindings.Add("Visible", rbB, "Checked");
}
However... I observed that using UserControls instead of TextBoxes breaks the functionality and I should go read on the net why..
LATER EDIT :
The databinding works two-ways! : If you programatically set (from somewhere else) the visibility of the txtA to false the rbA will become unchecked. That's the beauty of Databinding.

How do you return the focus to the last used control after clicking a button in a winform app?

I'm working on a windows forms application (C#) where a user is entering data in a form. At any point while editing the data in the form the user can click one of the buttons on the form to perform certain actions. By default the focus goes to the clicked button so the user has to click back on to the control they want to edit in order to continue modifying the data on the form. What I need to be able to do is return the focus to the last edited control after the button click event has been processed. Here's a sample screenshot that illustrates what I'm talking about:
The user can be entering data in textbox1, textbox2, textbox3, etc and click the button. I need the button to return the focus back to the control that most recently had the focus before the button was clicked.
I'm wondering if anyone has a better way of implementing this functionality than what I've come up with. Here's what I'm doing right now:
public partial class Form1 : Form
{
Control _lastEnteredControl;
private void textBox_Enter(object sender, EventArgs e)
{
_lastEnteredControl = (Control)sender;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Do something here");
_lastEnteredControl.Focus();
}
}
So basically what we have here is a class variable that points to the last entered control. Each textbox on the form is setup so the textBox_Enter method is fired when the control receives the focus. Then, when the button is clicked focus is returned to the control that had the focus before the button was clicked. Anybody have any more elegant solutions for this?
For a bit of 'simplicity' maybe try.
public Form1()
{
InitializeComponent();
foreach (Control ctrl in Controls)
{
if (ctrl is TextBox)
{
ctrl.Enter += delegate(object sender, EventArgs e)
{
_lastEnteredControl = (Control)sender;
};
}
}
}
then you don't have to worry about decorating each textbox manually (or forgetting about one too).
You could do the following
Change the button to a label and make it look like a button. The label will never get focus and you don't have to do all the extra coding.
I think what you're doing is fine. The only thing I could think of to improve it would be to store each control into a stack as they are accessed. That would give you a complete time line of what was accessed.
Your approach looks good. If you want to avoid having to add an the event handler to every control you add, you could create a recursive routine to add a GotFocus listener to every control in your form. This will work for any type of control in your form, however you could adjust it to meet your needs.
private void Form_OnLoad(object obj, EventArgs e)
{
AddGotFocusListener(this);
}
private void AddGotFocusListener(Control ctrl)
{
foreach(Control c in ctrl.Controls)
{
c.GotFocus += new EventHandler(Control_GotFocus);
if(c.Controls.Count > 0)
{
AddGotFocusListener(c);
}
}
}
private void Control_GotFocus(object obj, EventArgs e)
{
// Set focused control here
}
Your implementation looks good enough -- what I do want to know is why you want to do this in the first place? Won't it be preferrable for the focus to cycle back to the first entry? Is the data in the last text box so malleable that once they click the button it is "remembered"? Or do you have some sort of operation that the button does to that specifici text box data -- in that case shouldn't the focus go to a subsequent control instead?
I'm interested in finding out why you want to do this in the first place.
Yeah, I admit the requirement is a bit unusual. Some of the information that the users will be entering into this application exists in scans of old documents that are in a couple of different repositories. The buttons facilitate finding and opening these old docs. It's difficult to predict where the users will be on the form when they decide to pull up a document with more information to enter on the form. The intent is to make the UI flow well in spite of these funky circumstances.
Create a class called CustomTextBox that inherits from TextBox. It has a static variable called stack. When the textbox loses focus push onto the stack. When you want to find the last focused control then just pop the first item from the stack. Make sure to clear the static Stack variable.

Categories