C
C#β€’2y ago
JJ

βœ… [wpf] how to solve autometically closed new window when I create new window..

In StickyNotesView.xaml , I create new Window but If I click '+' that new window not only undisplayed but also programm app is closed too . why? I just fixed my xaml code <Grid IsHitTestVisible="True" > <TextBox Name="SubTextBox" IsHitTestVisible="True" TextChanged="TextBox_Changed"></TextBox> </Grid> if I remve <Grid> </Grid> and just reamin TextBox, then the problem is solved. but I wanna add button. so I should use Grid this panel. First Image is that removed Grid. so new window is displayed. but If i use upper <Grid> </Grid> then New Window is not displayed and program down. I did dbug but when dbug , that new window is displayed.,... it is weird.
32 Replies
JJ
JJOPβ€’2y ago
this is my xaml code for creating new window's content like textBox, button.
HimmDawg
HimmDawgβ€’2y ago
First of all: $code
MODiX
MODiXβ€’2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
HimmDawg
HimmDawgβ€’2y ago
Second, if everything closes out of nowhere, then it seems like your App crashed Can you paste the code that executes after pressing the + Button?
JJ
JJOPβ€’2y ago
First of all I got it error msg
JJ
JJOPβ€’2y ago
JJ
JJOPβ€’2y ago
. OK I try it
HimmDawg
HimmDawgβ€’2y ago
$codegif
HimmDawg
HimmDawgβ€’2y ago
This might make it clearer πŸ™‚
JJ
JJOPβ€’2y ago
BlazeBin - idfkcjivtipf
A tool for sharing your source code with the world!
JJ
JJOPβ€’2y ago
Problem is that StickyNotesView.xaml and cs file
HimmDawg
HimmDawgβ€’2y ago
private void newWindow()
{
Window win = new Window();
_Linq.Add(new Linq { win = win, textBlock = _textBlock });
win.Width = 300;
win.Height = 300;

//linq.Add(new Linq { win = win, textBlock = _textBlock });

SubTextBox.Foreground = new SolidColorBrush(Colors.White);
SubTextBox.Background = new SolidColorBrush(Colors.Black);
SubTextBox.TextWrapping = TextWrapping.Wrap;
SubTextBox.AcceptsReturn = true; // κ°œν–‰λ¬Έμž.
// ==================== PROBLEM HERE =====================
win.Content = SubTextBox;
// =======================================================

win.WindowStyle = WindowStyle.ThreeDBorderWindow;
win.Show();
}
private void newWindow()
{
Window win = new Window();
_Linq.Add(new Linq { win = win, textBlock = _textBlock });
win.Width = 300;
win.Height = 300;

//linq.Add(new Linq { win = win, textBlock = _textBlock });

SubTextBox.Foreground = new SolidColorBrush(Colors.White);
SubTextBox.Background = new SolidColorBrush(Colors.Black);
SubTextBox.TextWrapping = TextWrapping.Wrap;
SubTextBox.AcceptsReturn = true; // κ°œν–‰λ¬Έμž.
// ==================== PROBLEM HERE =====================
win.Content = SubTextBox;
// =======================================================

win.WindowStyle = WindowStyle.ThreeDBorderWindow;
win.Show();
}
So your SubTextBox is already part of a StickyNotesView (as seen in StickyNotesView.xaml) You cant have a control that is child of two different Windows / Controls I suppose you want to create another textbox in that newly created window
JJ
JJOPβ€’2y ago
the control you saying is what? Actully first I made Control wpf page named StickyNotesView.xaml but later I changed Control to Window by writing Window in xaml and cs
JJ
JJOPβ€’2y ago
HimmDawg
HimmDawgβ€’2y ago
<Window x:Class="StickyNotes.App.StickyNotesView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:StickyNotes.App"
mc:Ignorable="d"
Name="myWindow"
d:DesignHeight="200" d:DesignWidth="200">
<Grid>
<TextBox Name="SubTextBox"
IsHitTestVisible="True"
TextChanged="TextBox_Changed">
</TextBox>
</Grid>
</Window>
<Window x:Class="StickyNotes.App.StickyNotesView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:StickyNotes.App"
mc:Ignorable="d"
Name="myWindow"
d:DesignHeight="200" d:DesignWidth="200">
<Grid>
<TextBox Name="SubTextBox"
IsHitTestVisible="True"
TextChanged="TextBox_Changed">
</TextBox>
</Grid>
</Window>
You see that TextBox there? That's in your StickyNotesView
JJ
JJOPβ€’2y ago
yes
HimmDawg
HimmDawgβ€’2y ago
What's also in your StickyNotesView is this
public StickyNotesView(TextBlock textBlock, List<Linq> linq)
{
InitializeComponent();
_textBlock = textBlock;
_Linq = linq; // _Linq references the linq in the Main view. When _Linq element is deleted, linq is also deleted
newWindow();
}
public StickyNotesView(TextBlock textBlock, List<Linq> linq)
{
InitializeComponent();
_textBlock = textBlock;
_Linq = linq; // _Linq references the linq in the Main view. When _Linq element is deleted, linq is also deleted
newWindow();
}
And there, newWindow() is called. at this point we know that we have a Window that contains a textbox with the name "SubTextBox"
JJ
JJOPβ€’2y ago
yes
HimmDawg
HimmDawgβ€’2y ago
Now what happens in newWindow()? Exactly what I pointed out earlier: Since SubTextBox is already part of the Window we are currently at, it cannot be made the child of another Window But this is what you tried with this line of code win.Content = SubTextBox;
JJ
JJOPβ€’2y ago
so win.Content = SubTextBox is that issue right?
HimmDawg
HimmDawgβ€’2y ago
Yes. That's what Visual Studio pointed out as well πŸ™‚
JJ
JJOPβ€’2y ago
and this is point right?
HimmDawg
HimmDawgβ€’2y ago
Pardon?
JJ
JJOPβ€’2y ago
You said " You cant have a control that is child of two different Windows / Controls " so it is point in my code issue
HimmDawg
HimmDawgβ€’2y ago
I think i dont understand what you mean with "it is point" πŸ™
JJ
JJOPβ€’2y ago
Ah , I said main point (important point in this issue) like heart in human , An important issue for resolution. anyway thanks ! I try dbug ! But
JJ
JJOPβ€’2y ago
when I remove Grid panel, then it is work well
JJ
JJOPβ€’2y ago
eventhough I didn't fix problem that you point out
JJ
JJOPβ€’2y ago
JJ
JJOPβ€’2y ago
before I understood and resolved it ! I thought present window's item can use other window if that other window created in the same file like StickyNotesView.xaml.cs, however I wrong, thanks to you
HimmDawg
HimmDawgβ€’2y ago
Nice one goblinnGlow
Want results from more Discord servers?
Add your server