In form1 I created simple form called formhaslo.
I created in formhaslo control called listBoxhaslo
Now, I want to create MouseDoubleClick Event to listBoxhaslo.
I have problem with getting listBoxhaslo from formhaslo to form1.
Can you take a look at this code, please (check out comments):
public partial class Form1 : Form
{
Form formhaslo = new Form();
...
...
...
public void buttonLoadPassForBAKFile_Click(object sender, EventArgs e)
{
int i = 0;
string path = #"Backups";
formhaslo.StartPosition = FormStartPosition.CenterScreen;
ListBox listBoxhaslo = new ListBox();
listBoxhaslo.Location = new System.Drawing.Point(0, 30);
listBoxhaslo.Left = (formhaslo.ClientSize.Width - listBoxhaslo.Width) / 2;
using (FileStream fsSbHaslo = new FileStream(path + #"\BAKPass._pass", FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (StreamReader srhaslo = new StreamReader(fsSbHaslo, Encoding.Default))
{
string line;
while ((line = srhaslo.ReadLine()) != null)
{
listBoxhaslo.Items.Add(line);
i++;
}
srhaslo.Close();
}
formhaslo.Controls.Add(listBoxhaslo);
formhaslo.Controls.Add(label1);
listBoxhaslo.MouseDoubleClick += new MouseEventHandler(listBoxhaslo_MouseDoubleClick); // <---here is EventHandler
formhaslo.Show();
}
}
void listBoxhaslo_MouseDoubleClick(object sender, MouseEventArgs e)
{
if ((listBoxhaslo.SelectedItem) != null) // <--listBoxhaslo does not exist in current context
{
PassForBakFile = (listBoxhaslo.SelectedItem.ToString());
formhaslo.Hide();
}
}
I know that this error must be there because im doing it wrong but I don't know how to do it.
The listBoxhaslo doesn't exists because it was declare within the scope of first function which is not visible to the second function of your event listBoxhaslo_MouseDoubleClick. For you to make it work you need to declare listBoxhaslo variable outside of your function. You could declare it after formhaslo near top. Or another way to accomplish this is to cast sender to ListBox within your event.
void listBoxhaslo_MouseDoubleClick(object sender, MouseEventArgs e)
{
var listBoxhaslo = (sender as ListBox);
if (listBoxhaslo.SelectedItem != null)
{
PassForBakFile = (listBoxhaslo.SelectedItem.ToString());
formhaslo.Hide();
}
}
I haven't tried the code but I think it will do.
Related
private void OnCarSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Car tmpCar = (Car)CarLST.SelectedItem;
VinNumberTB.Text = tmpCar.VinNumber;
CarMakeTB.Text = tmpCar.CarMake;
CarTypeCB.SelectedIndex = (int)tmpCar.Type;
PurchasePriceTB.Text = tmpCar.PurchasePrice.ToString();
ModelYearCB.SelectedItem = tmpCar.ModelYear;
MileageTB.Text = tmpCar.Mileage.ToString();
CarIMG.Source = new BitmapImage(new Uri(String.Format("ms-appx:///Assets/CarImages/{0}.png", tmpCar.Type.ToString())));
}
This corresponds to a list box's selectionChanged event handler. Is there a way I can just use this in my code as if it were a method. e.g.
SelectionChanged(foo, bar);
Basically, is there a way of using this block of code again without creating another method to hold this stuff.
Since you don't use any of the event arguments, you can extract the code into a separate method:
private void OnCarSelectionChanged(object sender, SelectionChangedEventArgs e)
{
UpdateDisplay();
}
private void UpdateDisplay()
{
Car tmpCar = (Car)CarLST.SelectedItem;
VinNumberTB.Text = tmpCar.VinNumber;
CarMakeTB.Text = tmpCar.CarMake;
CarTypeCB.SelectedIndex = (int)tmpCar.Type;
PurchasePriceTB.Text = tmpCar.PurchasePrice.ToString();
ModelYearCB.SelectedItem = tmpCar.ModelYear;
MileageTB.Text = tmpCar.Mileage.ToString();
CarIMG.Source = new BitmapImage(new Uri(String.Format("ms-appx:///Assets/CarImages/{0}.png", tmpCar.Type.ToString())));
}
Then to call it separately all you need to do is:
this.UpdateDisplay();
The button requires two clicks to fire up the event. Here is an image and the code.There is a combobox which triggers the button with different items, but when I click the button to show an item in a panel on the page, I have to click it twice so it can trigger the event. After selecting an item once by twice-clicking it, every next time i click it works with one click, just like it should.
Here is the image of the combobox which triggers the button
And there is the code :
namespace Carbon
{
public partial class ucAnaliza : MetroFramework.Controls.MetroUserControl
{
static ucAnaliza _instance;
public static ucAnaliza Instance3
{
get
{
if (_instance == null)
_instance = new ucAnaliza();
return _instance;
}
}
public MetroFramework.Controls.MetroPanel MetroAnaliza
{
get { return mPanelAnaliza; }
set { mPanelAnaliza = value; }
}
public ucAnaliza()
{
InitializeComponent();
}
private void ucAnaliza_Load(object sender, EventArgs e)
{
}
private void mPotvrdiElementi_Click(object sender, EventArgs e)
{
switch (((ComboBox)mDropAnaliza).SelectedItem.ToString())
{
case "Главна рамка":
_instance = this;
ucGlavna uc = new ucGlavna();
uc.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc);
break;
case "Челна рамка":
_instance = this;
ucCelna uc2 = new ucCelna();
uc2.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc2);
break;
case "Подолжна рамка":
_instance = this;
ucPodolzna uc3 = new ucPodolzna();
uc3.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc3);
break;
}
}
}
}
Here is the code from the designer for the button :
// mPotvrdiElementi
//
this.mPotvrdiElementi.BackColor = System.Drawing.Color.Transparent;
this.mPotvrdiElementi.CausesValidation = false;
this.mPotvrdiElementi.Cursor = System.Windows.Forms.Cursors.Hand;
this.mPotvrdiElementi.ForeColor = System.Drawing.SystemColors.MenuBar;
this.mPotvrdiElementi.Image = global::Carbon.Properties.Resources.Checked_Checkbox_24px;
this.mPotvrdiElementi.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.mPotvrdiElementi.ImageSize = 24;
this.mPotvrdiElementi.Location = new System.Drawing.Point(758, 34);
this.mPotvrdiElementi.Name = "mPotvrdiElementi";
this.mPotvrdiElementi.Size = new System.Drawing.Size(80, 25);
this.mPotvrdiElementi.Style = MetroFramework.MetroColorStyle.Orange;
this.mPotvrdiElementi.TabIndex = 4;
this.mPotvrdiElementi.TabStop = false;
this.mPotvrdiElementi.Text = "Потврди";
this.mPotvrdiElementi.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.mPotvrdiElementi.UseCustomBackColor = true;
this.mPotvrdiElementi.UseCustomForeColor = true;
this.mPotvrdiElementi.UseSelectable = true;
this.mPotvrdiElementi.UseStyleColors = true;
this.mPotvrdiElementi.Click += new System.EventHandler(this.mPotvrdiElementi_Click);
I know it is a long time ago but I was having the same problem...
But I found a solution to the problem and is working every time and not killing the usability.
private int focusFlag = 0;
private void MainForm_MouseEnter(object sender, EventArgs e)
{
if (focusFlag < 1)
{
this.FocusMe();
++focusFlag;
}
}
This will not always try to focus on that form when trying to go to other forms or something else, it will just focus once and that is enough... after that it will behave normally :)
It seems the MetroForm doesn´t get Focus until you click within the form and it is just a bug from the developers of the MetroFramework when using certain Metro Controls within the Form.
I have seen others posting the same problem when they are using the MetroFramework.
Hopefully this will help.
The problem I'm having is where I can successfully use the RoomID of the Room object I've created, but not the panel. Here is the function where I set the label to the name of the room. (This is my Form1.cs)
public partial class Form1 : Form
{
Room ThisRoom = new Room();
public Form1()
{
InitializeComponent();
this.Text = "Aquinas College Master Controller";
}
private void roomDesignerToolStripMenuItem_Click(object sender, EventArgs e)
{
new RoomDesigner().Show();
}
private void button1_Click(object sender, EventArgs e)
{
roomsToolStripMenuItem.DropDownItems.Clear();
foreach (var Room in Global.Aquinas.Aquinas)
{
ToolStripMenuItem NewItem = new ToolStripMenuItem(Room.RoomID);
NewItem.Name = Room.RoomID;
NewItem.Click += new EventHandler(ItemClick);
roomsToolStripMenuItem.DropDownItems.Add(NewItem);
}
}
void ItemClick(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
label2.Text = item.Name;
foreach (var Room in Global.Aquinas.Aquinas)
{
if (Room.RoomID == item.Name)
{
ThisRoom = Room;
break;
}
}
Panel RoomPanel = ThisRoom.Panel;
RoomPanel.Size = new Size(607, 304);
RoomPanel.Location = new Point(144, 27);
RoomPanel.BackColor = Color.White;
this.Controls.Add(RoomPanel);
}
}
This is some of my code for the room object. (This is in Room.cs)
public class Room
{
private List<CtrlComputer> _Computers;
public List<CtrlComputer> Computers
{
get { return _Computers; }
set { _Computers = value; }
}
private string _RoomID;
public Room()
{
_Computers=new List<CtrlComputer>();
}
public string RoomID
{
get
{
return _RoomID;
}
set
{
_RoomID = value;
}
}
public Panel Panel
{
get
{
return _Panel;
}
set
{
_Panel = value;
}
}
private Panel _Panel;
}
And here is where I register the panel which I have just put my designs on, to a new room. (This is some of my RoomDesigner.cs)
public partial class RoomDesigner : Form
{
Room NewRoom = new Room();
Panel RoomDesignerPanel = new Panel();
public RoomDesigner()
{
InitializeComponent();
RoomDesignerPanel.Size = new Size(607, 304);
RoomDesignerPanel.Location = new Point(144, 27);
RoomDesignerPanel.BackColor = Color.White;
this.Controls.Add(RoomDesignerPanel);
textBox1.ForeColor = SystemColors.GrayText;
textBox1.Text = "Enter Computer ID Here";
this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);
textBox2.ForeColor = SystemColors.GrayText;
textBox2.Text = "Enter Room ID Here";
this.textBox2.Leave += new System.EventHandler(this.textBox2_Leave);
this.textBox2.Enter += new System.EventHandler(this.textBox2_Enter);
}
private void textBox1_Leave(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
textBox1.Text = "Enter Computer ID Here";
textBox1.ForeColor = SystemColors.GrayText;
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
if (textBox1.Text == "Enter Computer ID Here")
{
textBox1.Text = "";
textBox1.ForeColor = SystemColors.WindowText;
}
}
private void textBox2_Leave(object sender, EventArgs e)
{
if (textBox2.Text.Length == 0)
{
textBox2.Text = "Enter Room ID Here";
textBox2.ForeColor = SystemColors.GrayText;
}
}
private void textBox2_Enter(object sender, EventArgs e)
{
if (textBox2.Text == "Enter Room ID Here")
{
textBox2.Text = "";
textBox2.ForeColor = SystemColors.WindowText;
}
}
private void button1_Click(object sender, EventArgs e)
{
CtrlComputer NewComputer = new CtrlComputer();
NewComputer.ComputerID = textBox1.Text;
NewComputer.Text = NewComputer.ComputerID;
NewComputer.Parent = RoomDesignerPanel;
RoomDesignerPanel.Controls.Add(NewComputer);
NewRoom.Panel = RoomDesignerPanel;
NewRoom.Add(NewComputer);
}
When I try to reload that panel, the RoomID of that object is returned fine, yet the panel is not. Any ideas?
Edit: Sorry for the lack of clarity in my post, first posts aren't always easy.
The panel is created in the designer, but I tried private Panel Panel1 = new Panel(); with no luck. My current problem is that in Form1.cs, the program will throw "Cannot access a disposed object", but I followed the guide here https://msdn.microsoft.com/en-us/library/82785s1h(v=vs.110).aspx .
What you are looking to do is swap between a collection of Panel objects (kind of like your own version of TabPages). To do this you need to create each of the Panel objects (You do this in RoomDesigner, which is fine, but there is a catch: see * below). With that collection in hand, when you want to show it on the form, you need to remove the panel1 the designer code created (Controls.Remove(panel1)) and insert your new one (Controls.Add(RoomPanel)).
Since Controls utilize system resources, make sure to Dispose any control you will no longer use. Here is an example method:
//Initial case where no room has been displayed yet and panel1 is still valid
private Panel CurrentPanel = null;
public void SwapPanel(Panel p)
{
//If no panel has been placed yet, get rid of the default one
if (panel1 != null)
{
//we should never need the designer panel again, so dispose it
panel1.Dispose();
this.Controls.Remove(panel1);
}
else
{
//we may return to this panel later, so don't dispose it
this.Controls.Remove(CurrentPanel);
}
CurrentPanel = p;
this.Controls.Add(p);
}
When you finally close out and no longer need your Panels stored in your Room collection, make sure to Dispose them. (However, if your whole program is exiting at that point then it doesn't matter, but do it anyway as good practice)
*When you close any RoomDesigner form (or any form), all controls it contains in 'Controls' will be disposed, even if you are using them elsewhere. To prevent this, remove the controls you want to preserve from the RoomDesigner Controls collection before it is closed.
I have a button that opens a Window.
If the button is pressed again, it opens a duplicate of the same window.
info = new Info();
info.Owner = Window.GetWindow(this);
info.Show();
How do you check if the Window is already open, and deny a duplicate from being opened again?
I can't use info.ShowDialog() because it disables the Main Window.
Solutions that have not worked:
Info info = new Info();
if (!info.IsActive)
{
info = new Info();
info.Owner = Window.GetWindow(this);
info.Show();
}
Info info = new Info();
if (info.Visibility != Visibility.Visible)
{
info.Owner = Window.GetWindow(this);
info.Show();
}
public static bool IsWindowOpen<T>(string name = "") where T : Window
{
return string.IsNullOrEmpty(name)
? Application.Current.Windows.OfType<T>().Any()
: Application.Current.Windows.OfType<T>().Any(w => w.Name.Equals(name));
}
private void buttonInfo_Click(object sender, RoutedEventArgs e)
{
if (!IsWindowOpen<Window>("Info"))
{
Info info = new Info();
info.Owner = Window.GetWindow(this);
info.Show();
}
}
Create a form only when value is not null.
If the form was closed put the value back to null with the FormClosed event.
public static Info info;
if(info == null){
info = new Info();
info.Show();
}
put an event form close on the info form
private void info_FormClosed(object sender, FormClosedEventArgs e)
{
MainForm1.info = null;
}
It works for me
The sensible approach is to just keep track of the Window instance so you don't have to find it back later. Add a field:
private Info infoWindow;
If it is null then you know that the window doesn't exist yet, so you'll want to create it. Use the Closed event to set the variable back to null. If it is not null then you want to make sure that the window gets restored. So:
private void button_Click(object sender, RoutedEventArgs e) {
if (infoWindow == null) {
infoWindow = new Info();
infoWindow.Closed += (s, ea) => infoWindow = null;
infoWindow.Owner = this; // optional
infoWindow.Show();
}
else {
if (infoWindow.WindowState == WindowState.Minimized) {
infoWindow.WindowState = WindowState.Normal;
}
infoWindow.Activate();
}
}
And you probably also want to close the window automatically when the window that contains the button is closed:
private void Window_Closed(object sender, EventArgs e) {
if (infoWindow != null) infoWindow.Close();
}
You could use .IsLoaded field or bind the .ContentRendered event
Edit 1 -
Window1:
public class Window1 : Window
{
private Info info = null;
private Boolean IsInfoOpened = false;
protected void OpenInfo()
{
if (this.IsInfoOpened) return;
this.info = new Info();
this.info.ContentRendered += delegate { this.IsInfoOpened = true; };
this.info.Closed += delegate { this.IsInfoOpened = false; }
this.info.Show();
}
}
In Form1 i have label2 in the designer and i added a code:
public void lbl2(string text)
{
label2.Text = text;
}
In the new class top i added:
private static AnimationEditor.Form1 fr1 = new AnimationEditor.Form1();
And in the new class event im updating the label2.Text like this:
int ISampleGrabberCB.BufferCB(double sampleTime, IntPtr pBuffer, int bufferLen)
{
using (var bitmap = new Bitmap(_width, _height, _width * 3, PixelFormat.Format24bppRgb, pBuffer))
{
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
if (SaveToDisc)
{
String tempFile = _outFolder + _frameId + ".bmp";
if (File.Exists(tempFile))
{
fr1.lbl2(_frameId.ToString();
}
else
{
bitmap.Save(Path.Combine(_outFolder, _frameId + ".bmp"));
}
_frameId++;
}
else
{
if (Images == null)
Images = new List<Bitmap>();
Images.Add((Bitmap)bitmap.Clone());
}
}
return 0;
}
Thel ine that is doing the updating is:
fr1.lbl2(_frameId.ToString();
Now i used breakpoint on this line in the new class and also in Form1 on the label2.Text in the public function and i saw that the label2 text is changing first its 0 then 1 then 2 and so on.
But in fact in realtime when im running the application the label2 dosent change its all the time the text saty as label2
This is the Form1 button click event when i click on it its doing the new class code:
private void button5_Click(object sender, EventArgs e)
{
wmv = new Polkan.DataSource.WmvAdapter(#"d:\VIDEO0040.3gp", sf);
wmv.SaveToDisc = true;
wmv.Start();
wmv.WaitUntilDone();
}
I think the quick answer is to pass in the reference of the label to the class:
private Label lbl;
public WmvAdapter(string file, string outFolder, Label label) {
// yada-yada-yada
lbl = label;
}
Your routine would change to:
if (File.Exists(tempFile))
{
lbl.Text = _frameId.ToString();
}
Your click event:
private void button5_Click(object sender, EventArgs e)
{
wmv = new Polkan.DataSource.WmvAdapter(#"d:\VIDEO0040.3gp", sf, this.Label2);
wmv.SaveToDisc = true;
wmv.Start();
wmv.WaitUntilDone();
}
The longer answer is to have your class raise an event and have your form listen for it.
Having your class be aware of the form is not the best coding practice.