I am trying to change a tabpage name on a parent form to what a user types in a textbox on a child form when a strip menu button is clicked. I have everything working in that I can pull the correct information between both forms but every time it goes to get the currently selected tabpage it always returns "0".
Function to set new tabpage name on Forum1 (The message boxes are from trying to debug)
public void setNewTabName(string TextBoxText)
{
MessageBox.Show("Called");
MessageBox.Show(TextBoxText);
int CurrentSelectedTab = tabControl1.SelectedIndex;
MessageBox.Show(CurrentSelectedTab.ToString());
tabControl1.TabPages[CurrentSelectedTab].Text = TextBoxText;
}
Function (Form2) for getting the textbox info and passing it to Form1
private void button1_Click(object sender, EventArgs e)
{
BT frm1 = new BT();
frm1.setNewTabName(getTextBoxInfo());
}
public string getTextBoxInfo()
{
return textBox1.Text;
}
Any help would be greatly appreciated. I think I posted all the relevant code but if you need anything else I can post the whole thing. The only thing that is really left out is that it creates a new tabpage on a button click.
Edit: The same method works fine when it is taken out of the child GUI.
I think that the problem is that you create a new form (class BT) each time the button is clicked. I suggest you to move the form creation from button click event to parent form load function.
Related
I am currently making an rpg using Winforms for a school project. However my knowledge on classes is so limited that I'm having trouble making a proper class that takes in data from 1 form, is used in the second form, then sent back to the first form.
The process I'm trying to accomplish is like this:
main form opens a second form that displays items in a listbox.
1
when you select an item and press a button to use it, the items effects are applied.
2
The data for the effect is in the first form where many other calculations are made with the same data.
3
I keep running into the problem of making a new object of a class and the data from the first form is reset. How would I go about either using an existing object from the first form, or creating a reference class maybe?
This is the function I want to run on the first form when the button on the second form is clicked.
public void SmallPot()
{
currentPHP += pHP * .25;
if (newPHP > pHP)
{
newPHP = pHP;
}
pHPBarUpdate = (int)(newPHP / pHP * 377);
pnlCurrentPHP.Width = pHPBarUpdate;
newPHP = currentPHP;
}
Expected:
When I click the use button on the popup form it closes and the items effects are displayed on the Main form.
What Happens:
Since I create a new object of form one in form two, all my variables are reset to 0 before the calculation, resulting in nothing happening after the second form closes.
I will give you a general guideline to implement a solution based on event definition and raising
Let's start from your second form where you need to communicate to the first form the event
public class Form2 : Form
{
// start creating the delegate type
public delegate void OnItemSelected(string itemName);
// declare the public event that this form will raise
public event OnItemSelected ItemSelected;
protected void cmdItemUse_Click(object sender, EventArgs e)
{
// When the user clicks to select an item....
string itemName = GetItemSelectedFromList();
// Check if someone is interested in this item selection
if(ItemSelected != null)
ItemSelected.Invoke(itemName);
}
}
Now we change something in the first Form. We need to create the second form and before displaying it we subscribe to the event exposed by the second form
public class Form1 : Form
{
... other stuff....
protected void cmdOpenSelection_Click(object sender, EventArgs e)
{
using(Form2 frm = new Form2())
{
// Subscribe the event giving it a method inside this class
// that doesn't return anything and receives a string
// as required by the delegate type of the event
frm.ItemSelected += handleItemSelection;
frm.ShowDialog(); // frm.Show();
}
}
private void handlerItemSelection(string itemName)
{
// This method is a custom Event handler and inside Form1
// will be called by Form2 through the Invoke on the event variable
}
}
In the example above I choose to pass a simple string, but of course you could pass anything including a reference type like an instance of a class containing all the info
required by the Item selection.
Just a quick one!
I've had to add a second form to my windows form application due to not physically having the space for more textboxes.
Some of the textboxes have ended up being the same as on the original form (I know this isn't ideal, but the two forms each write to separate text files, so it works out easier overall)
With this being the case, I'd like the values in the textboxes from the original form to be copied into their duplicate textboxes on the second form (trying to prevent double data entry and reduce risk of errors).
So, I have a button click on the first (Form1) form that calls the .Show() function to load a new version of the second form (PreAnaestheticChecklist).
public void btnPreOpChecklist_Click(object sender, EventArgs e)
{
//create secondary form for pre-anaesthetic checklist
PreAnaestheticChecklist checklistForm = new PreAnaestheticChecklist();
//load pre-anaesthetic checklist form to screen
checklistForm.Show();
}
This works fine, and the form loads up as blank. I wrote a load of small string functions that return strings that are comprised of the text in the textboxes from form1. These are called in the PreAnaestheticChecklist_Load event. An example is shown below using one of the transfers as an example.
public string getProcedure()
{
//load value from textbox in IOconsole
string proc = main.txtProcedure.Text;
//return this to textbox on Checklist
return proc;
}
public void PreAnaestheticChecklist_Load(object sender, EventArgs e)
{
//load any values already on main form into respective textboxes
txtProcName.Text = getProcedure();
txtPlannedProc.Text = getProcedure();
}
This is done for another few textboxes, but even with all this, the second form loads as blank.
I read up and was advised to try putting all of the textbox assignments from the _Load event into the button click event that loads form2, and still nothing.
I also changed the Modifiers property for all forms envolved to 'Public', and still nothing!
Not sure where to look next, so any help with the matter is very much appreciated!
Thanks in advance,
Mark
Pass in Form1 as the Owner when you call Show():
public void btnPreOpChecklist_Click(object sender, EventArgs e)
{
//create secondary form for pre-anaesthetic checklist
PreAnaestheticChecklist checklistForm = new PreAnaestheticChecklist();
//load pre-anaesthetic checklist form to screen
checklistForm.Show(this); // <-- passing in the Owner
}
Now, in the Load() event of your PreAnaestheticChecklist Form, cast the .Owner property to Form1 and store it in your "main" variable:
public void PreAnaestheticChecklist_Load(object sender, EventArgs e)
{
this.main = (Form1)this.Owner;
//load any values already on main form into respective textboxes
txtProcName.Text = getProcedure();
txtPlannedProc.Text = getProcedure();
}
I try to get the following done:
A WPF application where i have multiple buttons where you can set a notification message.
Depending on the button, you can set different messages.
What i did, was on the message button i have put this code:
private void button1_Click(object sender, RoutedEventArgs e)
{
CounterMessage msgOne = new CounterMessage();
msgOne.ShowDialog();
}
This will open op a new WPF window here only is a textbox and an exit button.
On exit in this message window, it will save the message to a parameter.
But here is the trick.
I want to use this message window for multiple notifications, and it will display in the textbox any text content if there is already any on a string in the application.
So for example:
In the main app i have button A and B to set the notification on.
I click on button A, the showdialog pops up and in the textbox already have "you clicked button A"
If it was button B that has been clicked, it should display "you clicked button B"
So i should sent some extra info with the ShowDialog, so i can use the messagewindow for each one.
Could someone help me out a bit herE?
I must say i find it a bit hard do decently discribe what i want, so i hope i made myself clear enough.
EDIT
So hat i want is showing the content of a string parameter (to be exact: Properties.Settings.Default.XXX) into the textbox that is in the Countermessage window
I am not entirely sure what you are asking, but it sounds like you want something like this. I am assuming that CounterMessage is a Window, and that there is some binding mechanism or property that displays what the message is.
public class CounterMessage : Window
{
public CounterMessage(string message)
{
this.Message = message;
}
public string Message
{
get;
set;
}
}
Your button event would then be something along the lines of:
private void button1_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
CounterMessage msgOne = new CounterMessage(btn.Text);
msgOne.ShowDialog();
}
The point being that you don't send something to the ShowDialog method, but rather to the class that is the dialog itself. I also assume that the dialog does more than just displaying the message - otherwise, you would just use MessageBox.Show(....)
Button btn = (Button)sender;
Debug.WriteLine(btn.Name);
I am learning windows forms and can create a one form with textboxes and stuff, but I was wondering how can I change the form upon let's say clicking a button?, so for instance my initial form has a textbox and a button, if the button is clicked I want to show a form with a dropdown and a button. SO the question should be:
1) How do I change the form upon clicking a button but without creating a new instance of the form.
2) If I wanted, how can I add a form when the button is clicked showing the same drop down and button as a pop up form?
In reality I would like to know both cases, changing the form via using the same form and via popping a new form on top.
Should the questions not be clear, I am willing to further explain
Thank you
I'm assuming you already know how to add controls in the form designer and how to implement event handlers.
Question 1
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Visible)
{
comboBox1.Visible = false;
textBox1.Visible = true;
}
else
{
comboBox1.Visible = true;
textBox1.Visible = false;
}
}
The button click handler simply toggles the visibility of the two controls.
Question 2
private void button2_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.ShowDialog();
}
This time the button handler instantiates a new form an then shows it as a modal dialog. Call Show() if you don't want to show modally.
I have a checkbox in form1, when it is checked it makes a PictureBox in form2 visible but when I uncheck I want to refresh form2 so that the PictureBox is not visible. This code is in form1. It is a button that opens up the form if one if not open but if a form is open it refreshes it. The problem is that it is not refreshing. Can anyone tell me what is wrong?
private tuesday _FavoritesForm;
public void startbutton_Click(object sender, EventArgs e)
{
if (_FavoritesForm == null)
{
_FavoritesForm = new tuesday();
_FavoritesForm.Closed += new EventHandler(_FavoritesForm_Closed);
_FavoritesForm.Show();
}
else
{
_FavoritesForm.Refresh();
}
}
Calling Refresh on a form merely forces it to be repainted. There isn't any reason to assume that it will repaint differently. You would have to override the OnPaint() method in that form. Clearly you are not using OnPaint to draw an image, you are using a PictureBox. Setting that control's Visible property to false will make the image disappear, no additional help is needed.
I would add a public method on the secondary form to Show/Hide the picture because it appears the second form has no idea of the first form. Then the click / checkbox setting on the first form to instead of doing a "REFRESH" on the second, create the form if its not already done so. Once created, call whatever method you expose on the secondary form to specifically make visible or not as needed.
EDIT FOR CLARIFICATION
#a13xy, actually the reverse... The second form has no idea of the first, but yes, have a method that is public on the 2nd. Then on the FIRST form, in the click / value changed event of your checkbox, you just call the method from that... such as your sample code...
public void startbutton_Click(object sender, EventArgs e)
{
if (_FavoritesForm == null)
{ _FavoritesForm = new tuesday();
_FavoritesForm.Closed += new EventHandler(_FavoritesForm_Closed);
_FavoritesForm.Show();
}
else
{ _FavoritesForm.Refresh();
}
_FavoritesForm.ShowHide( IsCurrentForms.CheckBox.IsCheckedValue );
}
Not positive of your checkbox controls name, or its Checked value property, just call the second form's method directly with whatever your forms value is and the method in the SECOND form could be something like...
public void ShowHide( Boolean ShowTheImage )
{
// value provided as a direct parameter from the first form,
// THISform knows about its own Picture property and can directly
// set the visibility within its scoped control.
this.YourPicture.Visible = ShowTheImage
}