WPF showdialog Textbox with var content - c#

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);

Related

C# WPF refresh listbox from another window

I'm making a WPF project based on an Access database.
The database has two tables:
tblMovies (MovieID: PK, Title, Director, Genre etc)
tblActors (ActorID: PK, MovieID: FK, Firstname, Lastname etc)
I have listbox where I can see all the movies, and if I click on one, it shows a new window with all the details about that movie: the title, director, genre, but also the actors.
In this window I have added a button to create a new actor. This opens a new window where you can enter the MovieID (FK) and the information about the actor.
When I click save changes, it works and the window closes, but my listboxActors needs to be manually refreshed (I have added a button for that) to see the new actor.
Is there a way to refresh my listboxActors after I click "save changes" in my other window?
I first did it by closing my first screen when clicking add new actor, and then if I saved it would reopen the screen, and it'd automatically be refreshed, but I don't want it that way.
My listboxActors:
listBoxActors.ItemsSource = mov.Actors;
Save button (in the other screen)
private void buttonSaveNewActor_Click(object sender, RoutedEventArgs e)
{
Actor act = new Actor();
act.MovieID = Convert.ToInt32(textBoxMovieID.Text);
act.FirstName = textBoxFirstName.Text;
act.LastName = textBoxLastName.Text;
act.Country = textBoxCountry.Text;
act.Born = Convert.ToDateTime(BornDate.SelectedDate);
act.Bio = textBoxBio.Text;
ActorRepository.AddActor(act);
MessageBox.Show("The actor: " + act.FirstName + " " + act.LastName + " has been created");
this.Hide();
}
The refresh button:
private void buttonRefresh_Click(object sender, RoutedEventArgs e)
{
listBoxActors.ItemsSource = null;
listBoxActors.ItemsSource = mov.Actors;
}
Thanks in advance!
Well, Thanks for the explanatory comment...! I have a suggestion for you, please let me know if this helps you to code your scenario:
So you have two forms WindowShowDetails Let it be the main-form and WindowAddActor be the child, the Their will be a button in the main form which opens the child form, and you are doing some tasks in the child and press the Save button, which will save those details and closes that form. And you wanted to refresh the List in the main form associated with this event. For this you can use delegates and events;For this you have to do a number of tasks, in the main-form as well as in the child, Let me show you how it can help you:
Define a delegate in the main form:
public delegate void RefreshList();
Define an event of that delegate type
public event RefreshList RefreshListEvent;
Define a method that will do the action(ie, Refreshing the grid)
private void RefreshListView()
{
// Code to refresh the grid
}
Now need to define a Delegate in the WindowAddActor
Public Delegate UpdateActor;
Now we have to code the button click that opens the WindowAddActor form Let the button be btnAddActor so its Click event will be btnAddActor_click, we have to initialize our delegate-event, the instance of the WindowAddActor and assign the event to Delegate in the WindowAddActor before showing that form. this can be coded as :
private void btnAddActor_click(object sender, EventArgs e)
{
WindowAddActor actorInstance = new WindowAddActor();
RefreshListEvent += new RefreshList(RefreshListView); // event initialization
actorInstance.UpdateActor = RefreshListEvent; // assigning event to the Delegate
actorInstance.Show();
}
Now we have to call the delegate from the SaveButton's click event that is
private void buttonSaveNewActor_Click(object sender, RoutedEventArgs e)
{
// Perform save operation
UpdateActor.DynamicInvoke(); this will call the `RefreshListView` method of mainWindow
this.Close();
}

how to retrieve text of buttons clicked in windows form with one single method

I have a windows form application with multiple buttons. I need to retrieve the text property of any button clicked in order to create a query to the database. the only way I know is to create a button click event and cast the sender as button then do a switch case for each button Id which seems very hectic since I probably will have more than 100 buttons in the entire application. So my question is there a generic key press method I can create which can retrieve the text property of any button pressed/clicked on the form? Please excuse me if the question is not very clear. Any help will be appreciate
Use a single click event handler for all similar kind of buttons. This way there will be click event subscribed for every button but only one method which will be executed for all buttons. You can determine which button was pressed as follows.
Using sender object as follows;
private void button_Click(object sender, EventArgs e)
{
var buttonText = ((Button)sender).Text;
//Query using buttonText
}
Update:
Above answer will still require you to subscribe click event for each button. If you don't want that then have a look at following approach;
You could use (ClickTransparentButton or) disable (Enabled=false) all these buttons and add click event on parent Form. Once you get click event you can get button which was clicked as follows;
private void Form1_Click(object sender, EventArgs e)
{
var p = PointToClient(Cursor.Position);
var control = GetChildAtPoint(p);
if(control is Button)
{
var buttonText = ((Button)control).Text;
//Query using buttonText
}
}
But this has few disadvantages such as, you will not be able to operate these buttons using keyboard.
and more...
Create some function as buttons click handler:
private void buttonClickHandler(object sender, EventArgs e)
{
string buttonName = (sender as Button).Text;
}
2A. Connect Click event of every button to this handler.
2B. To automate connection of button click handler use something like that:
private void connectButtonsHandlers()
{
foreach(var c in this.Controls)
{
if(c is Button)
{
(c as Button).Click += buttonClickHandler;
}
}
}
Add this code to form constructor to perform connection at program start.

Show MessageBox immediately in Windows Forms?

Is there any way to have a messagebox immediately pop up when a form opens? I just want to display a short message about how to use the form when it opens. I tried
private void myForm_Load(object sender, EventArgs e)
{
DialogResult dialogOpen = MessageBox.Show("Use the navigation menu to get started.", "Welcome!", MessageBoxButtons.OK);
}
but it doesn't work.
Showing a MessageBox during Form_Load works just fine for me. I literally copy/pasted the code from your original post, and it worked. I'm on .NET Framework 4.5 on Windows 8.1.
Are you sure your Load event handler is getting called? Perhaps the it's not hooked up to the Load event properly.
I don't see why it wouldn't work in Form_Load. Definitely try doing as others have pointed out by putting it beneath form initialization.
Though, given that you're just showing a message box, I don't think there is any reason to store the result, so a simple MessageBox.Show(message); Should do the trick.
As #s.m. said, from a UX point of view, having a notification thrown in your face as soon as the app starts would be very obnoxious, at least if you have it EVERY time. Personally, I would create a boolean Settings variable, set it to true the first time the message is displayed, and only display it when the setting is false, i.e. the first time the message is displayed.
private boolean splashShown = Properties.Settings.Default.splashShown;
private void Form_Load(object sender, EventArgs e)
{
if (!splashShown)
{
MessageBox.Show("message");
myForm.Properties.Settings.Default.splashShown = true;
myForm.Properties.Settings.Default.Save();
}
}
And set up the splashShown Setting in your form properties.
If the problem is that your Form_Load() method isn't actually attached to your Form.Load() event, you can double click the form window in the designer and it will automatically created the Form_Load() base method for you and attach it to the Form.Load() event
Is there a reason to use the Load method of the form? If not you could to it in the constructor of form. If you want it to show up immediately after your form loads, you should do it in the constructor after the form is initialized. It should look something like this:
public partial class myForm : Form
{
public myForm()
{
InitializeComponent();
DialogResult dialogOpen = MessageBox.Show("Use the navigation menu to get started.", "Welcome!", MessageBoxButtons.OK);
}
}
The constructor (public myForm()) and the InitializeComponent(); should be automatically added to the form by Visual Studio after creating it.
Form_Load event occurs before the form is really visible.
I use:
static private bool splashShown = false;
private void Form1_Activated(object sender, System.EventArgs e)
{
if (!splashShown)
{
MessageBox.Show("message");
splashShown = true;
}
}
I have used this and it works fine. App start brings up messagebox first before all else.
InitializeComponent();
MessageBox.Show("put your message here");

Having problems with TabControls

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.

Re-bind datagrid when window is closed

I have a data grid that displays data from a sql server. I have an add button, that when clicked it opens a new window where the user can put the information for the new item that is being added. When the user clicks save, the data is being saved to the database, but its not showing up the in the grid. Is there a way that I can make the datagrid bind when the add window is closed? Let me know if more info is needed. Thanks.
In my main window, that contains the datagrid code, i have an add button:
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
showAddWindow();
}
And, the showAddWindow method is:
private void showAddWindow()
{
add addWindow = new add(dgDataView);
addWindow.Owner = this;
addWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
addWindow.ShowDialog();
}
If you know the Id of new inserted object, then you can send it to the main form, and call Add.Row on the grid with the new info. This way you'll not make a callback to the database for rebinding.
Assuming this is WinForms:
First, when calling the "Add" window, use ShowDialog() instead of Show()
In the main form with the DataGrid the code would look like
private void btnAdd_Click(Object sender, EventArgs e)
{
DialogResult b = frmAdd.ShowDialog();
if(b == DialogResult.Ok)
{
// code to re-bind the grid here.
}
}
in the frmAdd form, you will need to make your Save button set the DialogResult for the form to be DialogResult.Ok after updating the database.
I'm guessing that what you have tried is along the lines of:
private void btnAdd_Click(Object sender, EventArgs e)
{
frmAdd.ShowDialog();
// code to re-bind the grid here.
}
The difference is that with the ShowDialog() call, the main form will wait until the "add" form is closed to continue executing. In my second code sample, just using Show(), the code to re-bind the grid happens immediately after showing the "Add" form, before the user gets a chance to update the data.
(note, I did that code off the top of my head, not in Visual Studio, so it may have mistakes)

Categories

Resources