now i have the current code o MainUC.cs:
private void tsbNoviRacun_Click(object sender, EventArgs e)
{
if (racunuc == null)
{
racunuc = new RacunUC();
racunuc.Dock = DockStyle.Fill;
Controls.Add(racunuc);
}
racunuc.BringToFront();
The thing i want to do is clean the code from main page/form. I have 2 taskbar and 2 toolbar buttons that are calling the same form (this one above), so i don't want to write the code 4 times. I tried to make new class.cs with properties and do it with return value, but it didn't work. Can someone help me with it, or, is there possiblity to call the same code on current page/form. Something like
private void tsbStariRacuni_Click(object sender, EventArgs e)
{
call tsbNoviRacun();
}
"( this isn't working, i know :p)
EDiT: Oh damn me, thanks guys!
In c# there is no "call" keyword for invoking functions. You just type the name and all required arguments in round brackets.
private void tsbStariRacuni_Click(object sender, EventArgs e)
{
tsbNoviRacun_Click(sender, e);
}
This should do it:
public void tsbNoviRacun()
{
if (racunuc == null)
{
racunuc = new RacunUC();
racunuc.Dock = DockStyle.Fill;
Controls.Add(racunuc);
}
racunuc.BringToFront();
}
private void tsbNoviRacun_Click(object sender, EventArgs e)
{
tsbNoviRacun();
}
You can call that method from all the event handlers you want it to run on. Obviously this function is depended on Controls and DockStyle so you must put it within scope of this.
Related
I am creating a program using WinForms so users can input info into textboxes on one form which then are saved into a Listbox on another form. I would like to be able to edit the items saved in the listbox by opening the original form on a button click. Really struggling with it as I can't think of the code and I can't seem to find a solution.
My Code:
private void btnAdd_Click(object sender, EventArgs e)
{
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.ShowDialog();
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
listBoxRooms.Items.Add(newRoomDisplayForm.value);
}
newRoomDisplayForm.Close();
}
private void btnRemove_Click(object sender, EventArgs e)
{
this.listBoxRooms.Items.RemoveAt(this.listBoxRooms.SelectedIndex);
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
So i've got a Add and Remove button which work perfectly just need a solution to the edit button.
Thanks in advance
I'm guessing newRoomDisplayForm.value is a property or a public member inside the form. You just need to do something like this:
private void btnEdit_Click(object sender, EventArgs e)
{
if(listBoxRooms.SelectedIndex < 0) return;
var tmpValue = listBoxRooms.Items[listBoxRooms.SelectedIndex].ToString();
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.value = tmpValue;
newRoomDisplayForm.ShowDialog();
//TODO: inside "newRoomDisplayForm" set the value to the textbox
// ie.: myValueTextBox.Text = this.value;
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
// replace the selected item with the new value
listBoxRooms.Items[listBoxRooms.SelectedIndex] = newRoomDisplayForm.value;
}
}
Hope it helps!
You can simply remove the listitem in that specific position, create a new item and add it again. it's kind of replacement.
I am looking to simulate a custom tooltip the like of you see in websites using c# .NET 4.5 windows forms.This tooltip will basically show status of some Tasks like how many tasks are pending,tasks in process, completed etc.To do this i am using a borderless win form.This winform will have some texts, images etc.I want it to reveal itself on button's mouseHover event and disappear on MouseLeave event.My problem is that on Mousehover event numerous instances of that tooltip form is getting generated and on MouseLeave they are not getting closed.My code is
private void B_MouseHover(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Close();
}
My code is not working, hence please tell me how to do this the correct way.Thanks
You're generating a new instance of the form class every time you get a hover event, and every time you get a leave event. If you want to continue to use this approach I would recommend you use a variable on your main form object to store the reference to your tooltip form. Secondly, you need to not generate a new instance whenever the event handler is called, but only when necessary. I would create your instance the first time your Hover event is called for a particular control, and then dispose of it when your Leave handler is called -- this is under the assumption that the tooltip dialog's constructor loads up different information for each control being hovered over. Like so:
frmSecQStatToolTipDlg f_tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
f_tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(f_tooltip != null)
{
f_tooltip.Close();
f_tooltip = null;
}
}
You should keep a global field for this form, and should not dispose or close it. Just hide it on some events and show again.
Sample Code:
frmSecQStatToolTipDlg tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg != null)
{
tooltip.Hide();
}
}
With this logic you'll not have to create tooltip instance again and again and it will not take time to popup if you frequently do this activity.
Declare your tooltip once as readonly and use it without asking anytime if it is null or not.
If you need to Dispose it, implement the IDisposable pattern:
https://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx
private readonly frmSecQStatToolTipDlg _tooltip = new frmSecQStatToolTipDlg() ;
private void B_MouseHover(object sender, EventArgs e)
{
_tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
_tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
_tooltip.Hide();
}
i'm new to C# i've been messing around to discover this language so far i've wrote many programs in my quest but now i'm stuck with one thing, i can't explain by words but codes can say what i want so here we go i know it's silly program but it's for education purpose only :D
Private void change()
{
anycontrol.BackColor = Color.Gold; // when this function called the control's BackColor will Change to gold
}
// example
private void TextBox1_Focused(object sender, EventArgs e)
{
Change(); // this suppose to change the color of the controls which is now textbox1 i want it to work on other controls such as buttons progressbars etc
}
now after i explained my problem i may ask you if you can to help it will be appreciated.
You can create a method that takes a Control and a Color as a parameter, and anything that inherits from Control (i.e. TextBox, DropDownList, Label etc.) will work with this:
void SetControlBackgroundColour(Control control, Color colour)
{
if (control != null)
{
control.BackColor = colour;
}
}
In your example, you could use it like this:
private void TextBox1_Focused(object sender, EventArgs e)
{
SetControlBackgroundColour(sender as Control, Color.Gold);
}
In response to the comments, you could then use this method in a recursive method that will set the background colour for each control on the form:
void SetControlBackgroundColourRecursive(Control parentControl, Color colour)
{
if (parentControl != null)
{
foreach (Control childControl in parentControl.Controls)
{
SetControlBackgroundColour(childControl, colour);
SetControlBackgroundColourRecursive(childControl);
}
}
}
And then call this function on your Form object (this) in your Form1_Load method (assuming the form is called Form1):
protected void Form1_Load(object sender, EventArgs e)
{
SetControlBackgroundColourRecursive(this, Color.Gold);
}
I am using YoutubeExtractor's dll.. videoDownloader_ProgressChanged and videoDownloader_DownloadFinished events are working in console application but in winform, it doesnt work.. I dont understand why..
private void btnStart_Click(object sender, EventArgs e)
{
string link = textBox1.Text;
start(link);
}
static void start(string link)
{
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(link);
DownloadVideo(videoInfos);
}
private static void DownloadVideo(IEnumerable<VideoInfo> videoInfos)
{
VideoInfo video = videoInfos
.First(info => info.VideoFormat == VideoFormat.Standard360);
var videoDownloader = new VideoDownloader(video, Path.Combine("C:/Downloads", video.Title + video.VideoExtension));
videoDownloader.DownloadFinished += new EventHandler(videoDownloader_DownloadFinished);
videoDownloader.ProgressChanged += new EventHandler<ProgressEventArgs>(videoDownloader_ProgressChanged);
videoDownloader.Execute();
}
static void videoDownloader_ProgressChanged(object sender, ProgressEventArgs e)
{
//some code..
}
static void videoDownloader_DownloadFinished(object sender, EventArgs e)
{
//some code..
}
my second question is, I want to access a form control in a static videoDownloader_ProgressChanged event. e.ProgressPercentage paramter gives me percent of video downloaded. I want to show it in label. But I cant access label because of static event.. I tried to use delegate but nothing changed..
Please modify both Start() and DownloadVideo() routines to instance methods. Remove 'static' keyword from them and event handlers as well.
Thread off 'videoDownloader.Execute()' and BeginInvoke() in the changed/finished handlers.
Don't call methods that take forever, (in computer terms), in GUI event handlers. If it takes more than about 50ms, thread it off. Any net thingy, eg. something with 'YouTube' in it, will take longer than that just to establish a connection!
I have a class file cls_PurchaseValidae.cs in which i have written a code like below:
public void txtMfgDate_Validating(TextBox txtMfgDate,ErrorProvider errorProvider1,string datetoValidate)
{
//method for validating the text box.
ValidateMfgDate(txtMfgDate, errorProvider1,datetoValidate);
}
and i m calling this event in the code behind frm_Purchase.cs like this:
private void txtMfgDate_Validating(object sender, CancelEventArgs e)
{
objPurchaseValid.txtMfgDate_Validating(txtMfgDate,errorProvider1,datetoValidate);
}
but i m getting the error:
Error 1 The name 'datetoValidate' does
not exist in the current context
How can i resolve it.
Thanks in advance
I am not sure why do you want to pass around both TextBox txtMfgDate, and string datetoValidate? Shouldn’t datetoValidate be always txtMfgDate.Text? Anyway, you can do:
private void txtMfgDate_Validating(object sender, CancelEventArgs e)
{
objPurchaseValid.txtMfgDate_Validating(txtMfgDate,errorProvider1,txtMfgDate.Text);
}
if that is what you want.